edgedesign/texy-bundle

此包已被废弃且不再维护。没有推荐替代包。

Edgedesign Texy! bundle

v1.0.0 2015-03-30 07:53 UTC

This package is not auto-updated.

Last update: 2020-01-19 16:21:00 UTC


README

这是一个将Texy!文本处理器(http://texy.info/en)封装为Symfony包的简单bundle。

设置

  1. composer require edgedesign/texy-bundle
  2. new Edge\TexyBundle\EdgeTexyBundle()添加到您的AppKernel中。

配置

您可以根据需要覆盖Resources/config/config.yml中的指令。如果您对我们的实现不满意,只需实现正确的接口即可。

### 自定义属性

edge_texy_bundle.custom_attributes部分中,您可以定义将添加到每个HTML元素的自定义HTML属性。这对于允许HTML5属性(如downloaddata-something等)很有用。

过滤器

edge_texy_bundle.filters部分中,您可以设置您的过滤器。第一级键决定了过滤器的名称。

然后,有三种类型的设置。

  • 'allowed'用于通过调用如$texy->allowed[$moduleName] = true/false来设置启用的模块。
  • 'module'用于将设置传递给模块,它将在类似的调用中翻译:$texy->$moduleName.'Module'->$parameter = $settings
  • 'variables'是最强大的设置,它几乎可以做到任何事情。它被翻译成以下调用:$texy->$variableName = $variableValue

在值中,您可以使用星号(*),它将被翻译为常量Texy::ALL,以及破折号(-),它被翻译为Texy::NONE。

如果这些设置不够,您可以扩展Texy,在构造函数中设置它,并将实例名称设置为class属性。

edge_texy_bundle.filters:
  filter_from_my_class:
    # Name of class which will be used to instantiate Texy instance.
    class: MyClassExtendingTexy   
    # equivalent to $texy->allowed['block/code'] = FALSE;
    allowed:
        "block/code": false
    # equivalent to $texy->allowedTags = array('a' => array('href', 'target'), 'em' => Texy::NONE, 'img' => Texy::ALL)
    variables:
        allowedTags:
            a:
               - href
               - target
            em: '-'
            img: '*'
    # equivalent to $texy->{name}Module->variable = value
    modules:
      link:
          forceNoFollow: true; 			

在这种情况下,允许、模块和变量设置也将传递给扩展的类。

用法

使用此bundle有两种方法。

服务

您可以通过获取名为edge_texy.processor的TexyProcessor服务并调用方法来获取。

$processor->multiLineText($text, $filterId);
$processor->singleLineText($text, $filterId);

这将通过配置中指定的id为$filterId的过滤器处理您的$text

Twig

第二种方法是使用注册的Twig宏'texy_process'和'texy_process_line'。

{{ variableThatINeedToPassThroughTexy|texy_process(filterId)}}

当您没有在变量中存储内容,而是在模板本身中存储内容时,这种替代的Twig语法很有用

{% filter texy_process %}
- Lorem
- Ipsum

Foo **Bar** Baz.
{% endfilter %}

(感谢OndraM指出这一点[链接])

这种方法,给定的变量将通过名为filterId的过滤器传递。如果没有提供过滤器名称,宏将尝试使用名为"默认"的过滤器。

宏之间的区别在于,当使用texy_process_line时,Texy不会将给定的代码包装在块标签中

.

示例config.yml

用于清理HTML输出的设置示例(将其放入您的config.yml中)

edge_texy:
  custom_attributes: &custom_attributes
      500: download
  attribute_settings:
      html5_attributes: &html5_attributes
          100: itemid
          101: itemprop
          102: itemref
          103: itemscope
          104: itemtype
      html_identifiers: &html_identifiers
          200: class
          201: id
      global_attributes: &global_attributes
          << : [*html5_attributes, *html_identifiers]
          << : *custom_attributes
  filters:
      sanitize:
          allowed:
            link/url: false
          modules:
            link:
              shorten: false
          outputMode: 4   # Set outputMode to HTML5 - https://github.com/jiripudil/texy/blob/master/Texy/Texy.php#L122
          variables:
              allowedTags:
                  a:
                      << : *global_attributes
                      0: href
                      1: title
                  acronym: [title]
                  b: *global_attributes
                  br: *global_attributes
                  cite: *global_attributes
                  code: *global_attributes
                  div: *global_attributes
                  em: *global_attributes
                  img:
                      << : *global_attributes
                      0: src
                      1: alt
                  strong: *global_attributes
                  sub: *global_attributes
                  sup: *global_attributes
                  q: *global_attributes
                  small: *global_attributes

      strip:
          allowed:
              paragraph: false
          variables:
              allowedTags: '-'

现在,您可以使用过滤器 sanitize 清理用户输入的HTML输出,以及使用过滤器 strip 使得用户输入的HTML不会出现在您的代码中。