1
0
mirror of https://github.com/flarum/core.git synced 2025-08-11 19:04:29 +02:00

Rejig post formatting

This commit is contained in:
Toby Zerner
2015-02-09 09:49:48 +10:30
parent 3ed893b1b8
commit af94f22334
7 changed files with 309 additions and 249 deletions

View File

@@ -4,6 +4,8 @@ use Illuminate\Support\ServiceProvider;
use Config;
use Event;
use Flarum\Core\Formatter\FormatterManager;
class CoreServiceProvider extends ServiceProvider
{
/**
@@ -28,7 +30,6 @@ class CoreServiceProvider extends ServiceProvider
Event::listen('Flarum.Core.*', 'Flarum\Core\Listeners\DiscussionMetadataUpdater');
Event::listen('Flarum.Core.*', 'Flarum\Core\Listeners\UserMetadataUpdater');
Event::listen('Flarum.Core.*', 'Flarum\Core\Listeners\PostFormatter');
Event::listen('Flarum.Core.*', 'Flarum\Core\Listeners\TitleChangePostCreator');
}
@@ -63,6 +64,13 @@ class CoreServiceProvider extends ServiceProvider
$this->app->bind('flarum.discussionFinder', 'Flarum\Core\Discussions\DiscussionFinder');
$this->app->singleton('flarum.formatter', function () {
$formatter = new FormatterManager($this->app);
$formatter->add('basic', 'Flarum\Core\Formatter\BasicFormatter');
return $formatter;
});
// $this->app->singleton(
// 'Flarum\Core\Repositories\Contracts\DiscussionRepository',

View File

@@ -0,0 +1,18 @@
<?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 = '<p>'.preg_replace(['/[\n]{2,}/', '/\n/'], ['</p><p>', '<br>'], trim($text)).'</p>';
return $text;
}
}

View File

@@ -0,0 +1,67 @@
<?php namespace Flarum\Core\Formatter;
use Illuminate\Container\Container;
class FormatterManager
{
protected $formatters = [];
/**
* The IoC container instance.
*
* @var \Illuminate\Container\Container
*/
protected $container;
/**
* Create a new formatter manager instance.
*
* @param \Illuminate\Container\Container $container
* @return void
*/
public function __construct(Container $container = null)
{
$this->container = $container ?: new Container;
}
public function add($name, $formatter, $priority = 0)
{
$this->remove($name);
if (is_string($formatter)) {
$container = $this->container;
$formatter = function () use ($container, $formatter) {
$callable = array($container->make($formatter), 'format');
$data = func_get_args();
return call_user_func_array($callable, $data);
};
}
$this->formatters[$name] = [$formatter, $priority];
}
public function remove($name)
{
unset($this->formatters[$name]);
}
public function format($text)
{
$sorted = [];
foreach ($this->formatters as $array) {
list($formatter, $priority) = $array;
$sorted[$priority][] = $formatter;
}
ksort($sorted);
foreach ($sorted as $formatters) {
foreach ($formatters as $formatter) {
$text = $formatter($text);
}
}
return $text;
}
}

View File

@@ -1,48 +0,0 @@
<?php namespace Flarum\Core\Listeners;
use Laracasts\Commander\Events\EventListener;
use Flarum\Core\Posts\PostRepository;
use Flarum\Core\Posts\Post;
use Flarum\Core\Posts\Events\ReplyWasPosted;
use Flarum\Core\Posts\Events\PostWasRevised;
class PostFormatter extends EventListener
{
protected $postRepo;
public function __construct(PostRepository $postRepo)
{
$this->postRepo = $postRepo;
}
protected function formatPost($post)
{
$post = $this->postRepo->find($post->id);
// By default, we want to convert paragraphs of text into <p> tags.
// And maybe also wrap URLs in <a> tags.
// However, we want to allow plugins to completely override this, and/or
// just do some superficial formatting afterwards.
$html = htmlspecialchars($post->content);
// Primary formatter
$html = '<p>'.$html.'</p>'; // Move this to Flarum\Core\Support\Formatters\BasicFormatter < FormatterInterface
// Run additional formatters
$post->content_html = $html;
$this->postRepo->save($post);
}
public function whenReplyWasPosted(ReplyWasPosted $event)
{
$this->formatPost($event->post);
}
public function whenPostWasRevised(PostWasRevised $event)
{
$this->formatPost($event->post);
}
}

View File

@@ -1,5 +1,6 @@
<?php namespace Flarum\Core\Posts;
use App;
use Laracasts\Commander\Events\EventGenerator;
use Tobscure\Permissible\Permissible;
@@ -25,6 +26,7 @@ class CommentPost extends Post
$post = new static;
$post->content = $content;
$post->content_html = static::formatContent($post->content);
$post->time = time();
$post->discussion_id = $discussionId;
$post->user_id = $userId;
@@ -38,6 +40,7 @@ class CommentPost extends Post
public function revise($content, $user)
{
$this->content = $content;
$this->content_html = static::formatContent($this->content);
$this->edit_time = time();
$this->edit_user_id = $user->id;
@@ -60,4 +63,20 @@ class CommentPost extends Post
$this->raise(new Events\PostWasRestored($this));
}
public function getContentHtmlAttribute($value)
{
if (! $value) {
$this->content_html = $value = static::formatContent($this->content);
$this->save();
}
return $value;
}
protected static function formatContent($content)
{
$formatter = App::make('flarum.formatter');
return $formatter->format($content);
}
}