bupy7 / yii2-bbcode
该包已废弃,不再维护。没有建议的替代包。
一个轻量但可扩展的BBCode解析器,适用于Yii2,并带有HtmlPurifier过滤器。
v1.1.2
2018-02-07 11:19 UTC
Requires
- php: >=5.4.0
- jbbcode/jbbcode: 1.3.*
This package is auto-updated.
Last update: 2020-04-11 01:58:19 UTC
README
基于jBBCode和HtmlPurifier的BB-codes解析行为,适用于Yii2。此行为非常简单,可以根据您的需求进行扩展。使用jBBCode和HtmlPurifier的行为。
安装
安装此扩展的首选方式是通过composer。
运行以下命令之一:
$ php composer.phar require bupy7/yii2-bbcode "1.*"
或将其添加到您的composer.json文件中的require部分:
"bupy7/yii2-bbcode": "1.*"
to the require section of your composer.json file.
如何使用
添加以下代码到您的视图:
echo $form->field($model, 'content')->textArea();
添加以下代码到您的模型:
use bupy7\bbcode\BBCodeBehavior; public function behaviors() { return [ ... [ 'class' => BBCodeBehavior::className(), 'attribute' => 'content', 'saveAttribute' => 'purified_content', ], ... ]; }
支持BB-code
BB-code | HTML结果 |
---|---|
[b]bold[/b] |
<strong>bold</strong> |
[i]italic[/i] |
<em>italic</em> |
[u]underline[/u] |
<u>underline</u> |
[url=http://github.com]GitHub[/url] |
<a href="http://github.com">GitHub</a> |
[color=red]color[/color] |
<span style="color:red">color</span> |
[img=My photo]http://link.to/image.png[/img] |
<img src="http://link.to/image.png" alt="My photo" /> |
[img]http://link.to/image.png[/img] |
<img src="http://link.to/image.png" /> |
[p]paragraph[/p] |
<p>paragraph</p> |
[quote]blockquote[/quote] |
<blockquote><p>blockquote</p></blockquote> |
[h=1]header 1[/h] |
<h1>header 1</h1> |
[h=2]header 2[/h] |
<h2>header 2</h2> |
[h=3]header 3[/h] |
<h3>header 3</h3> |
[h=4]header 4[/h] |
<h4>header 4</h4> |
[h=5]header 5[/h] |
<h5>header 5</h5> |
[h=6]header 6[/h] |
<h6>header 6</h6> |
[center]align by center[/center] |
<div style="text-align: center">align by center</div> |
[left]align by left[/left] |
<div style="text-align: left">align by left</div> |
[right]align by right[/right] |
<div style="text-align: right">align by right</div> |
[hr][/hr] |
<hr /> |
[list][*]first[*]second[/list] |
<ul><li>first</li><li>second</li></ul> |
[list=1][*]first[*]second[/list] |
<ol><li>first</li><li>second</li></ol> |
[table][tr][td]first[/td][td]second[/td][/tr][/table] |
<table class="bb-table"><tr><td>first</td><td>second</td></tr></table> |
如何添加新的BB-code
将自定义BBCodes添加到您的解析器非常简单。对于简单的文本替换BBCodes,只需创建一个包含{param}的替换字符串,其中bbcode的内容应放置于此。可选地,您可以使用{option}变量来设置一个选项。
示例
public function behaviors() { return [ ... [ 'class' => BBCodeBehavior::className(), 'attribute' => 'content', 'saveAttribute' => 'purified_content', 'codeDefinitionBuilder' => [ // as elements of array ['quote', '<blockquote>{param}</blockquote>'], // as class name where class is instance of extended class \JBBCode\CodeDefinitionBuilder '/namespace/to/CodeDefinitionBuilder/ExtendedClassName', // as extended instance of extended class \JBBCode\CodeDefinitionBuilder $className, // as callable function where $builder is instance of class \JBBCode\CodeDefinitionBuilder function($builder) { $builder->setTagName('code'); $builder->setReplacementText('<pre>{param}</pre>'); return $builder->build(); }, ], ] ... ]; }
添加BB-code定义扩展的类\JBBCode\CodeDefinitionSet
示例
public function behaviors() { return [ ... [ 'class' => BBCodeBehavior::className(), 'attribute' => 'content', 'saveAttribute' => 'purified_content', 'codeDefinitionSet' => [ // as class name where class is instance of extended class \JBBCode\CodeDefinitionSet '/namespace/to/CodeDefinitionSet/ExtendedClassName', // as extended instance of extended class \JBBCode\CodeDefinitionSet $className, ], ], ... ]; }
添加BB-code定义扩展的类\JBBCode\CodeDefinition
示例
public function behaviors() { return [ ... [ 'class' => BBCodeBehavior::className(), 'attribute' => 'content', 'saveAttribute' => 'purified_content', 'codeDefinition' => [ // as class name where class is instance of extended class \JBBCode\CodeDefinition '/namespace/to/CodeDefinition/ExtendedClassName', // as extended instance of extended class \JBBCode\CodeDefinition $className, ], ], ... ]; }
HtmlPurifier
在解析和未解析BB-code之后,可以应用HtmlPurifier并使用不同的配置。有关更多信息,请参阅bupy7\bbcode\BBCodeBehavior
。
许可证
yii2-bbcode遵循BSD 3-Clause许可证发布。