helios-ag / fm-bbcode-bundle
Symfony FMBbCodeBundle
Requires
- php: >=5.5
- mjohnson/decoda: 6.*
- sensio/distribution-bundle: ~5.0
- symfony/assetic-bundle: ~2.4
- symfony/config: ~3.0
- symfony/console: ~3.0
- symfony/dependency-injection: ~3.0
- symfony/framework-bundle: ~3.0
- symfony/http-foundation: ~3.0
- symfony/http-kernel: ~3.0
- symfony/process: ~3.0
- symfony/templating: ~3.0
- symfony/twig-bundle: ~3.0
- symfony/yaml: ~3.0
Requires (Dev)
- satooshi/php-coveralls: dev-master
- symfony/symfony: ~3.0
README
PHP-Decoda 在 Symfony2 中的集成
BBCode 样式的轻量级词法字符串解析器。
贡献者
- Alexandre Quercia alquerci
- Gaiffe Antoine toinouu
- Luis Íñiguez idetia
- Sebastian slider
- olleyyy
- Dirk Olbertz dolbertz
- Florian Krauthan fkrauthan
- predakanga
- Dan piratadelfuturo
代码质量保证
安装
要安装此包,您需要同时安装 Decoda 库 和此包。安装取决于您的项目设置
步骤 1: 安装
使用 Composer,只需将以下配置添加到您的 composer.json
或者您可以使用 composer 安装此包
对于 symfony <3.0,使用最新 ~6 版本
composer require helios-ag/fm-bbcode-bundle:~6
对于 Symfony 3
composer require helios-ag/fm-bbcode-bundle
或者
composer require helios-ag/fm-bbcode-bundle:~7
步骤 2: 启用包
最后,在 kernel 中启用该包
<?php // app/AppKernel.php public function registerBundles() { $bundles = array( // ... new FM\BbcodeBundle\FMBbcodeBundle(), ); }
步骤 3: 导出表情符号(可选)
要启用通过表情符号钩子表情符号,使用以下命令将表情符号图像复制到公共文件夹(web/emoticons)
./app/console bbcode:dump
基本配置
通过更新您的配置使 Twig 扩展可用
By default only "default" filter enabled, which provide support
for [b], [i], [u], [s], [sub], [sup], [abbr], [br], [hr], [time]
BBCodes
在您的 Twig 模板中使用扩展的示例
在您的 config.yml 中定义 BBCode 过滤器
fm_bbcode: filter_sets: my_default_filter: strict: false # if you want to parse attr values without quotes locale: ru xhtml: true filters: [ default ]
然后您可以这样做
{{'[b]Bold text[/b]'|bbcode_filter('my_default_filter')}} {{'[u]Underlined text[/u]'|bbcode_filter('my_default_filter')}} {{'[i]Italic text[/i]'|bbcode_filter('my_default_filter')}}
fm_bbcode: filter_sets: my_default_filter: locale: ru xhtml: true filters: [ default, quote ] whitelist: [ b, quote ]
在启用 "quote" 过滤器后,您可以这样做
{{'[quote="helios"]My quote[/quote]'|bbcode_filter('my_default_filter')}}
您还可以在 filter_sets 参数下定义多个过滤器集,如下所示
fm_bbcode: filter_sets: my_forum_filter: locale: ru xhtml: true filters: [ default, quote ] whitelist: [ b, quote ] my_comment_filter: locale: ru xhtml: true filters: [ default, block, code, email, image, list, quote, text, url, video ]
{{'[quote="helios"]My quote[/quote]'|bbcode_filter('my_forum_filter')}} {{'[code]My source code[/code]'|bbcode_filter('my_comment_filter')}}
请注意,白名单标签会抑制过滤器配置中应用的所有标签。
剥离过滤器
要清除文本中的任何 BBCodes,请使用 bbcode_clean 过滤器:示例
{{'[b]some text[/b]'|bbcode_clean}}
此过滤器将消除 decoda 所知的所有标签
转义和换行符
转义和换行符可以通过过滤器配置下的 "escaping" 和 "line_breaks" 参数进行控制。它们的默认配置为 'true'。如果您希望管道化过滤器,请将转义设置为 false。
高级配置
覆盖消息
一些模板和钩子使用可翻译成不同语言的文本字符串,原始文件位于 decoda/config 目录下,但此文件的内容可以用 messages 选项下的消息覆盖。文件应为 json 格式。
fm_bbcode: config: messages: @SomeBundle/Resources/config/messages.json
添加自己的模板
您可以在 templates 节下定义自己的模板,以下示例显示如何进行
fm_bbcode: config: templates: - path: @SomeBundle/Resources/views/templates
模板示例可以在 decoda 库内部找到
添加自己的过滤器
要启用自定义过滤器,将其添加为您的配置中的一个常规服务,并使用 fm_bbcode.decoda.filter
标签标记它
services: acme_demo.decoda.filter.your_filter_name: class: Fully\Qualified\Filter\Class\Name tags: - { name: fm_bbcode.decoda.filter, id: your_filter_name }
您的服务必须实现 Decoda\Filter
接口。
如果您的服务由工厂创建,您必须正确设置此标签的类参数,以便它正确工作。
添加自己的钩子
要启用自定义钩子,将其作为常规服务添加到您的配置之一中,并使用fm_bbcode.decoda.hook
进行标记。
services: acme_demo.decoda.hook.your_hook_name: class: Fully\Qualified\Hook\Class\Name tags: - { name: fm_bbcode.decoda.hook, id: your_hook_name }
您的服务必须实现Decoda\Hook
接口。
如果您的服务由工厂创建,您必须正确设置此标签的类参数,以便它正确工作。
然后在您的配置中启用它。
fm_bbcode: filter_sets: my_default_filter: filters: [ default ] hooks: [ your_hook_name ]
自定义您自己的表情符号
您可以在emoticon
节点中定义自己的表情符号,下面的示例展示了如何操作。
fm_bbcode: emoticon: resource: path/to/emoticons.yml
# path/to/emoticons.yml imports: - { resource: path/to/another/emoticons.yml } emoticons: my_emoticon: url: # Default: %fm_bbcode.emoticon.path%/my_emoticon.png html: # Default: <img src="%fm_bbcode.emoticon.path%/my_emoticon.png" alt="" > xHtml: # Default: <img src="%fm_bbcode.emoticon.path%/my_emoticon.png" alt="" /> smilies: - ":my_emoticon:"
如何在每次部署时自动导出表情符号
将以下命令添加到您的项目composer.json中
# composer.json "scripts": { "post-install-cmd": [ "FM\\BbcodeBundle\\Composer\\ScriptHandler::installEmoticons" ], "post-update-cmd": [ "FM\\BbcodeBundle\\Composer\\ScriptHandler::installEmoticons" ] }
要自定义表情符号资源文件夹,请使用path
和folder
节点配置
# app/config.yml fm_bbcode: emoticon: path: # Default: /emoticons folder: # Default: %kernel.root_dir%/../vendor/mjohnson/decoda/emoticons%