From d0e7158379608703a699ed79d6c084ba0120008e Mon Sep 17 00:00:00 2001 From: Toby Zerner Date: Thu, 23 Jul 2015 14:29:33 +0930 Subject: [PATCH] Make formatter extensible --- framework/core/composer.lock | 18 +++++------ .../core/src/Core/Formatter/Formatter.php | 31 ++++++++++++++----- framework/core/src/Core/Posts/CommentPost.php | 21 ++++++++++--- .../core/src/Events/FormatterConfigurator.php | 19 ++++++++++++ framework/core/src/Events/FormatterParser.php | 27 ++++++++++++++++ .../core/src/Events/FormatterRenderer.php | 27 ++++++++++++++++ 6 files changed, 122 insertions(+), 21 deletions(-) create mode 100644 framework/core/src/Events/FormatterConfigurator.php create mode 100644 framework/core/src/Events/FormatterParser.php create mode 100644 framework/core/src/Events/FormatterRenderer.php diff --git a/framework/core/composer.lock b/framework/core/composer.lock index 394b4c803..d1dd2d70c 100644 --- a/framework/core/composer.lock +++ b/framework/core/composer.lock @@ -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", diff --git a/framework/core/src/Core/Formatter/Formatter.php b/framework/core/src/Core/Formatter/Formatter.php index 7508bc4e5..e7075368e 100644 --- a/framework/core/src/Core/Formatter/Formatter.php +++ b/framework/core/src/Core/Formatter/Formatter.php @@ -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); } diff --git a/framework/core/src/Core/Posts/CommentPost.php b/framework/core/src/Core/Posts/CommentPost.php index 366f21873..1e6c3f20e 100755 --- a/framework/core/src/Core/Posts/CommentPost.php +++ b/framework/core/src/Core/Posts/CommentPost.php @@ -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); } /** diff --git a/framework/core/src/Events/FormatterConfigurator.php b/framework/core/src/Events/FormatterConfigurator.php new file mode 100644 index 000000000..5eb02c3d3 --- /dev/null +++ b/framework/core/src/Events/FormatterConfigurator.php @@ -0,0 +1,19 @@ +configurator = $configurator; + } +} diff --git a/framework/core/src/Events/FormatterParser.php b/framework/core/src/Events/FormatterParser.php new file mode 100644 index 000000000..49faa4a76 --- /dev/null +++ b/framework/core/src/Events/FormatterParser.php @@ -0,0 +1,27 @@ +parser = $parser; + $this->post = $post; + } +} diff --git a/framework/core/src/Events/FormatterRenderer.php b/framework/core/src/Events/FormatterRenderer.php new file mode 100644 index 000000000..1f1581f87 --- /dev/null +++ b/framework/core/src/Events/FormatterRenderer.php @@ -0,0 +1,27 @@ +renderer = $renderer; + $this->post = $post; + } +}