freepius / php-richtext
一个简单的包装器(还有更多),允许使用 Markdown(Extra) 和 SmartyPants(Typographer)
Requires
- michelf/php-markdown: 1.4.*@dev
- michelf/php-smartypants: 1.6.*@dev
Requires (Dev)
- phpunit/phpunit: ~4.0
- pimple/pimple: ~3.0
- twig/twig: ~1.8
Suggests
- pimple/pimple: ~3.0
- silex/silex: 2.0.*@dev
- twig/twig: ~1.8
This package is not auto-updated.
Last update: 2024-09-24 03:28:34 UTC
README
此包提供
- 一个简单的包装器,允许使用 Markdown(Extra) 和 SmartyPants(Typographer),可以单独使用或一起使用
- 为 Pimple DI 容器提供服务(因此也可以用于 Silex)
- 为 Twig 模板引擎提供扩展
Markdown(Extra) 和 SmartyPants(Typographer) 包来自 Michel Fortin。感谢他为出色的作品!
php-richtext 与 PHP 5.3 或更高版本兼容。
安装
安装 php-richtext 的推荐方式是通过 Composer。只需创建一个 composer.json
文件,然后运行 php composer.phar install
命令即可安装
{ "require": { "freepius/php-richtext": "~1.0" } }
使用
从 Richtext
类的实例使用
<?php require_once __DIR__.'/../vendor/autoload.php'; $richtext = new Freepius\Richtext($config); echo $richtext->transform($text); echo $richtext->markdown($text); echo $richtext->smartypants($text);
作为 Pimple DI 容器的服务使用
<?php require_once __DIR__.'/../vendor/autoload.php'; $c = new Pimple\Container(); $c->register(new Freepius\Pimple\Provider\RichtextServiceProvider(), array( 'richtext.config' => array(/*config here*/), )); echo $c['richtext']->transform($text); echo $c['richtext']->markdown($text); echo $c['richtext']->smartypants($text);
由于 Silex 内部使用 Pimple DI 容器,因此您可以使用 php-richtext 与 Silex
<?php require_once __DIR__.'/../vendor/autoload.php'; use Freepius\Pimple\Provider\RichtextServiceProvider; use Silex\Application; use Symfony\Component\HttpFoundation\Request; $app = new Application(); $app->register(new RichtextServiceProvider(), array( 'richtext.config' => array(/*config here*/), )); $app->post('/blog/render-text', function (Application $app, Request $request) { return $app['richtext']->transform($request->get('text')); }); $app->run();
如果已安装 Twig,您还可以在您的 Twig 模板中使用 richtext 过滤器
<?php require_once __DIR__.'/../vendor/autoload.php'; /* From there, Twig is assumed to be loaded */ $richtext = new Freepius\Richtext($config); $twig->addExtension( new Freepius\Twig\Extension\RichtextTwigExtension($richtext) );
{{ 'Here a <<markdown-extra>> and/or ,,smartypants-typo`` text.' | richtext }} {{ 'Here a <<markdown-extra>> and/or ,,smartypants-typo`` text.' | markdown }} {{ 'Here a <<markdown-extra>> and/or ,,smartypants-typo`` text.' | smartypants }}
注意(针对 Silex):如果您通过 Silex 使用 Twig,首先注册 TwigServiceProvider
,然后注册 RichtextServiceProvider
。这将自动添加 twig 扩展!
配置
Richtext
类的构造函数接受以下配置参数(作为关联数组)
- locale
- 类型:
字符串
- 默认值:
null
- 描述:如果定义,则根据此区域设置配置 SmartyPants(Typographer)。目前只处理 'en'(既定事实)和 'fr'。
- 类型:
- extra
- 类型:
布尔值
- 默认值:
true
- 描述:如果为
true
,则使用MarkdownExtra
(而不是Markdown
)
- 类型:
- typo
- 类型:
布尔值
- 默认值:
true
- 描述:如果为
true
,则使用SmartyPantsTypographer
(而不是SmartyPants
)
- 类型:
- smartypants.attr
- 类型:
字符串
- 默认值:
SMARTYPANTS_ATTR_LONG_EM_DASH_SHORT_EN
- 描述:传递给 SmartyPants(Typographer) 构造函数的属性
- 类型:
- remove.script.tags
- 类型:
布尔值
- 默认值:
true
- 描述:如果为
true
,则从最终的 html 中删除<script>
标签
- 类型:
注意:如果定义了 locale
且 smartypants.attr
为 null
,则根据 locale
猜测 smartypants.attr
。目前只处理 'en'(既定事实)和 'fr'。
测试
要运行测试套件,您需要Composer
$ php composer.phar install --dev $ vendor/bin/phpunit
许可证
php-richtext遵循CC0许可证。