freepius/php-richtext

一个简单的包装器(还有更多),允许使用 Markdown(Extra) 和 SmartyPants(Typographer)

v1.0 2015-02-01 13:21 UTC

This package is not auto-updated.

Last update: 2024-09-24 03:28:34 UTC


README

此包提供

Markdown(Extra)SmartyPants(Typographer) 包来自 Michel Fortin。感谢他为出色的作品!

php-richtextPHP 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-richtextSilex

<?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> 标签

注意:如果定义了 localesmartypants.attrnull,则根据 locale 猜测 smartypants.attr。目前只处理 'en'(既定事实)和 'fr'。

测试

要运行测试套件,您需要Composer

$ php composer.phar install --dev
$ vendor/bin/phpunit

许可证

php-richtext遵循CC0许可证。