Remove BasicFormatter; add LinkifyFormatter

This commit is contained in:
Toby Zerner 2015-05-11 12:11:19 +09:30
parent 4fd61e2466
commit d3845ed1c0
4 changed files with 23 additions and 33 deletions

View File

@ -33,6 +33,8 @@ class CoreServiceProvider extends ServiceProvider
$this->registerGambits();
$this->setupModels();
$this->app['flarum.formatter']->add('linkify', 'Flarum\Core\Formatter\LinkifyFormatter');
$bus->mapUsing(function ($command) {
return Bus::simpleMapping(
$command, 'Flarum\Core\Commands', 'Flarum\Core\Handlers\Commands'
@ -54,11 +56,7 @@ class CoreServiceProvider extends ServiceProvider
// forum, registering, and starting discussions.)
$this->app->singleton('flarum.forum', 'Flarum\Core\Models\Forum');
$this->app->singleton('flarum.formatter', function () {
$formatter = new FormatterManager($this->app);
$formatter->add('basic', 'Flarum\Core\Formatter\BasicFormatter');
return $formatter;
});
$this->app->singleton('flarum.formatter', 'Flarum\Core\Formatter\FormatterManager');
$this->app->bind(
'Flarum\Core\Repositories\DiscussionRepositoryInterface',

View File

@ -1,25 +0,0 @@
<?php namespace Flarum\Core\Formatter;
use Misd\Linkify\Linkify;
class BasicFormatter
{
public function format($text)
{
$text = htmlspecialchars($text);
$linkify = new Linkify;
$text = $linkify->process($text, ['attr' => ['target' => '_blank']]);
$text = preg_replace_callback('/(?:^ *[-*]\s*([^\n]*)(?:\n|$)){2,}/m', function ($matches) {
return '</p><ul>'.preg_replace('/^ *[-*]\s*([^\n]*)(?:\n|$)/m', '<li>$1</li>', trim($matches[0])).'</ul><p>';
}, $text);
$text = '<p>'.preg_replace(['/[\n]{2,}/', '/\n/'], ['</p><p>', '<br>'], trim($text)).'</p>';
$text = preg_replace(array("/<p>\s*<\/p>/i", "/(?<=<p>)\s*(?:<br>)*/i", "/\s*(?:<br>)*\s*(?=<\/p>)/i"), "", $text);
$text = str_replace("<p></p>", "", $text);
return $text;
}
}

View File

@ -29,9 +29,8 @@ class FormatterManager
$this->remove($name);
if (is_string($formatter)) {
$container = $this->container;
$formatter = function () use ($container, $formatter) {
$callable = array($container->make($formatter), 'format');
$formatter = function () use ($formatter) {
$callable = array($this->container->make($formatter), 'format');
$data = func_get_args();
return call_user_func_array($callable, $data);
};

View File

@ -0,0 +1,18 @@
<?php namespace Flarum\Core\Formatter;
use Misd\Linkify\Linkify;
class LinkifyFormatter
{
protected $linkify;
public function __construct(Linkify $linkify)
{
$this->linkify = $linkify;
}
public function format($text)
{
return $this->linkify->process($text, ['attr' => ['target' => '_blank']]);
}
}