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

@ -8,16 +8,16 @@
"packages": [
{
"name": "danielstjules/stringy",
"version": "1.9.0",
"version": "1.10.0",
"source": {
"type": "git",
"url": "https://github.com/danielstjules/Stringy.git",
"reference": "3cf18e9e424a6dedc38b7eb7ef580edb0929461b"
"reference": "4749c205db47ee5b32e8d1adf6d9aff8db6caf3b"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/danielstjules/Stringy/zipball/3cf18e9e424a6dedc38b7eb7ef580edb0929461b",
"reference": "3cf18e9e424a6dedc38b7eb7ef580edb0929461b",
"url": "https://api.github.com/repos/danielstjules/Stringy/zipball/4749c205db47ee5b32e8d1adf6d9aff8db6caf3b",
"reference": "4749c205db47ee5b32e8d1adf6d9aff8db6caf3b",
"shasum": ""
},
"require": {
@ -60,7 +60,7 @@
"utility",
"utils"
],
"time": "2015-02-10 06:19:18"
"time": "2015-07-23 00:54:12"
},
{
"name": "dflydev/fig-cookies",
@ -553,12 +553,12 @@
"source": {
"type": "git",
"url": "https://github.com/s9e/TextFormatter.git",
"reference": "6260b224c4e993dcce4367229ee4af4efd8db197"
"reference": "27c94f07c964ff0ea0c0c86078ab0fe3d811e391"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/s9e/TextFormatter/zipball/6260b224c4e993dcce4367229ee4af4efd8db197",
"reference": "6260b224c4e993dcce4367229ee4af4efd8db197",
"url": "https://api.github.com/repos/s9e/TextFormatter/zipball/27c94f07c964ff0ea0c0c86078ab0fe3d811e391",
"reference": "27c94f07c964ff0ea0c0c86078ab0fe3d811e391",
"shasum": ""
},
"require": {
@ -604,7 +604,7 @@
"parser",
"shortcodes"
],
"time": "2015-07-21 17:04:09"
"time": "2015-07-22 14:20:26"
},
{
"name": "tobscure/json-api",

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;
}
}