freepius / php-richtext
A simple wrapper (and more) allowing to use Markdown(Extra) and SmartyPants(Typographer)
Installs: 53
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 0
Open Issues: 0
pkg:composer/freepius/php-richtext
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: 2025-10-21 09:09:33 UTC
README
This package provides :
- a simple wrapper allowing to use Markdown(Extra) and SmartyPants(Typographer), together or alone
- a service provider for the Pimple DI Container (and so for Silex too)
- an extension for the Twig template engine
Markdown(Extra) and SmartyPants(Typographer) packages come from Michel Fortin. Thanks to him for its great work!
php-richtext works with PHP 5.3 or later.
Installation
The recommended way to install php-richtext is through Composer. Just create a
composer.json file and run the php composer.phar install command to
install it:
{
"require": {
"freepius/php-richtext": "~1.0"
}
}
Usage
Use from an instance of the Richtext class:
<?php require_once __DIR__.'/../vendor/autoload.php'; $richtext = new Freepius\Richtext($config); echo $richtext->transform($text); echo $richtext->markdown($text); echo $richtext->smartypants($text);
Use as services of a Pimple DI Container:
<?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);
Since Silex uses internally the Pimple DI Container, you can use php-richtext with 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();
If Twig is installed, you can also use the richtext filters in your Twig templates:
<?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 }}
Note for Silex: If you use Twig through Silex, first register the TwigServiceProvider,
then register the RichtextServiceProvider. This one will add automatically the twig extension!
Configuration
The constructor of Richtext class accepts the following configuration parameters
(as an associative array):
- locale
- type :
string - default :
null - description : if defined, the SmartyPants(Typographer) will be configured depending on this locale. Presently, only 'en' (de facto) and 'fr' are handled.
- type :
- extra
- type :
bool - default :
true - description : if
true,MarkdownExtrais used (instead ofMarkdown)
- type :
- typo
- type :
bool - default :
true - description : if
true,SmartyPantsTypographeris used (instead ofSmartyPants)
- type :
- smartypants.attr
- type :
string - default :
SMARTYPANTS_ATTR_LONG_EM_DASH_SHORT_EN - description : attributes to pass to SmartyPants(Typographer) constructor
- type :
- remove.script.tags
- type :
bool - default :
true - description : if
true, remove the<script>tags of the final html
- type :
Note: If locale is defined and smartypants.attr is null,
smartypants.attr is guessed according to locale.
Presently, only 'en' (de facto) and 'fr' are handled.
Tests
To run the test suite, you need Composer:
$ php composer.phar install --dev $ vendor/bin/phpunit
License
php-richtext is licensed under the CC0 license.