From d3845ed1c027f2d3365c6827cedb37a1435cfd41 Mon Sep 17 00:00:00 2001 From: Toby Zerner Date: Mon, 11 May 2015 12:11:19 +0930 Subject: [PATCH] Remove BasicFormatter; add LinkifyFormatter --- .../core/src/Core/CoreServiceProvider.php | 8 +++--- .../src/Core/Formatter/BasicFormatter.php | 25 ------------------- .../src/Core/Formatter/FormatterManager.php | 5 ++-- .../src/Core/Formatter/LinkifyFormatter.php | 18 +++++++++++++ 4 files changed, 23 insertions(+), 33 deletions(-) delete mode 100644 framework/core/src/Core/Formatter/BasicFormatter.php create mode 100644 framework/core/src/Core/Formatter/LinkifyFormatter.php diff --git a/framework/core/src/Core/CoreServiceProvider.php b/framework/core/src/Core/CoreServiceProvider.php index 572a8bf1c..6224f7d58 100644 --- a/framework/core/src/Core/CoreServiceProvider.php +++ b/framework/core/src/Core/CoreServiceProvider.php @@ -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', diff --git a/framework/core/src/Core/Formatter/BasicFormatter.php b/framework/core/src/Core/Formatter/BasicFormatter.php deleted file mode 100644 index 41316704f..000000000 --- a/framework/core/src/Core/Formatter/BasicFormatter.php +++ /dev/null @@ -1,25 +0,0 @@ -process($text, ['attr' => ['target' => '_blank']]); - - $text = preg_replace_callback('/(?:^ *[-*]\s*([^\n]*)(?:\n|$)){2,}/m', function ($matches) { - return '

'; - }, $text); - - $text = '

'.preg_replace(['/[\n]{2,}/', '/\n/'], ['

', '
'], trim($text)).'

'; - - $text = preg_replace(array("/

\s*<\/p>/i", "/(?<=

)\s*(?:
)*/i", "/\s*(?:
)*\s*(?=<\/p>)/i"), "", $text); - $text = str_replace("

", "", $text); - - return $text; - } -} diff --git a/framework/core/src/Core/Formatter/FormatterManager.php b/framework/core/src/Core/Formatter/FormatterManager.php index e0871b7ab..79510d098 100644 --- a/framework/core/src/Core/Formatter/FormatterManager.php +++ b/framework/core/src/Core/Formatter/FormatterManager.php @@ -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); }; diff --git a/framework/core/src/Core/Formatter/LinkifyFormatter.php b/framework/core/src/Core/Formatter/LinkifyFormatter.php new file mode 100644 index 000000000..763943d2b --- /dev/null +++ b/framework/core/src/Core/Formatter/LinkifyFormatter.php @@ -0,0 +1,18 @@ +linkify = $linkify; + } + + public function format($text) + { + return $this->linkify->process($text, ['attr' => ['target' => '_blank']]); + } +}