1
0
mirror of https://github.com/flarum/core.git synced 2025-08-04 23:47:32 +02:00

Make formatter extensible

This commit is contained in:
Toby Zerner
2015-07-23 14:29:33 +09:30
parent 0fa0bbb541
commit d0e7158379
6 changed files with 122 additions and 21 deletions

View File

@@ -3,6 +3,10 @@
use Illuminate\Contracts\Cache\Repository;
use s9e\TextFormatter\Configurator;
use s9e\TextFormatter\Unparser;
use Flarum\Events\FormatterConfigurator;
use Flarum\Events\FormatterParser;
use Flarum\Events\FormatterRenderer;
use Flarum\Core\Posts\CommentPost;
class Formatter
{
@@ -38,6 +42,8 @@ class Formatter
$configurator->Emoticons->add(':)', '😀');
event(new FormatterConfigurator($configurator));
return $configurator;
}
@@ -50,14 +56,23 @@ class Formatter
});
}
protected function getParser()
protected function getParser(CommentPost $post)
{
return $this->getComponent('parser');
$parser = $this->getComponent('parser');
$parser->registeredVars['post'] = $post;
event(new FormatterParser($parser, $post));
return $parser;
}
protected function getRenderer()
protected function getRenderer(CommentPost $post)
{
return $this->getComponent('renderer');
$renderer = $this->getComponent('renderer');
event(new FormatterRenderer($renderer, $post));
return $renderer;
}
public function getJS()
@@ -72,16 +87,16 @@ class Formatter
])['js'];
}
public function parse($text)
public function parse($text, CommentPost $post)
{
$parser = $this->getParser();
$parser = $this->getParser($post);
return $parser->parse($text);
}
public function render($xml)
public function render($xml, CommentPost $post)
{
$renderer = $this->getRenderer();
$renderer = $this->getRenderer($post);
return $renderer->render($xml);
}

View File

@@ -37,12 +37,14 @@ class CommentPost extends Post
{
$post = new static;
$post->content = $content;
$post->time = time();
$post->discussion_id = $discussionId;
$post->user_id = $userId;
$post->type = static::$type;
// Set content last, as the parsing may rely on other post attributes.
$post->content = $content;
$post->raise(new PostWasPosted($post));
return $post;
@@ -113,15 +115,26 @@ class CommentPost extends Post
}
/**
* Parse the content before it is saved to the database.
* Unparse the parsed content.
*
* @param string $value
* @return string
*/
public function getContentAttribute($value)
{
return static::$formatter->unparse($value);
}
/**
* Get the parsed/raw content.
*
* @return string
*/
public function getParsedContentAttribute()
{
return $this->attributes['content'];
}
/**
* Parse the content before it is saved to the database.
*
@@ -129,7 +142,7 @@ class CommentPost extends Post
*/
public function setContentAttribute($value)
{
$this->attributes['content'] = static::$formatter->parse($value);
$this->attributes['content'] = static::$formatter->parse($value, $this);
}
/**
@@ -140,7 +153,7 @@ class CommentPost extends Post
*/
public function getContentHtmlAttribute($value)
{
return static::$formatter->render($this->attributes['content']);
return static::$formatter->render($this->attributes['content'], $this);
}
/**

View File

@@ -0,0 +1,19 @@
<?php namespace Flarum\Events;
use s9e\TextFormatter\Configurator;
class FormatterConfigurator
{
/**
* @var Configurator
*/
public $configurator;
/**
* @param Configurator $configurator
*/
public function __construct(Configurator $configurator)
{
$this->configurator = $configurator;
}
}

View File

@@ -0,0 +1,27 @@
<?php namespace Flarum\Events;
use s9e\TextFormatter\Parser;
use Flarum\Core\Posts\CommentPost;
class FormatterParser
{
/**
* @var Parser
*/
public $parser;
/**
* @var CommentPost
*/
public $post;
/**
* @param Parser $parser
* @param CommentPost $post
*/
public function __construct(Parser $parser, CommentPost $post)
{
$this->parser = $parser;
$this->post = $post;
}
}

View File

@@ -0,0 +1,27 @@
<?php namespace Flarum\Events;
use s9e\TextFormatter\Renderer;
use Flarum\Core\Posts\CommentPost;
class FormatterRenderer
{
/**
* @var Renderer
*/
public $renderer;
/**
* @var CommentPost
*/
public $post;
/**
* @param Renderer $renderer
* @param CommentPost $post
*/
public function __construct(Renderer $renderer, CommentPost $post)
{
$this->renderer = $renderer;
$this->post = $post;
}
}