Rejig formatting API. closes flarum/core#85

It works but it’s not the most pretty thing in the world. @franzliedke
Would be great if you could take a look at the whole formatting API and
work your magic on it sometime… my brain is fried!
This commit is contained in:
Toby Zerner 2015-06-04 10:48:07 +09:30
parent 9487a56d61
commit 42851f425b
8 changed files with 87 additions and 26 deletions

View File

@ -9,6 +9,20 @@ export default class PostPreview extends Component {
var post = this.props.post;
var user = post.user();
var excerpt = post.contentPlain();
var start = 0;
if (highlight) {
var regexp = new RegExp(this.props.highlight, 'gi');
start = Math.max(0, excerpt.search(regexp) - 100);
}
excerpt = (start > 0 ? '...' : '')+excerpt.substring(start, start + 200)+(excerpt.length > start + 200 ? '...' : '');
if (highlight) {
excerpt = highlight(excerpt, regexp);
}
return m('a.post-preview', {
href: app.route.post(post),
config: m.route,
@ -17,7 +31,7 @@ export default class PostPreview extends Component {
avatar(user), ' ',
username(user), ' ',
humanTime(post.time()), ' ',
highlight(post.excerpt(), this.props.highlight)
excerpt
]));
}
}

View File

@ -12,7 +12,7 @@ Post.prototype.user = Model.one('user');
Post.prototype.contentType = Model.prop('contentType');
Post.prototype.content = Model.prop('content');
Post.prototype.contentHtml = Model.prop('contentHtml');
Post.prototype.excerpt = Model.prop('excerpt');
Post.prototype.contentPlain = computed('contentHtml', contentHtml => $('<div/>').html(contentHtml.replace(/(<\/p>|<br>)/g, '$1 ')).text());
Post.prototype.editTime = Model.prop('editTime', Model.date);
Post.prototype.editUser = Model.one('editUser');

View File

@ -25,7 +25,7 @@ class PostBasicSerializer extends BaseSerializer
];
if ($post->type === 'comment') {
$attributes['excerpt'] = str_limit($post->contentPlain, 200);
$attributes['contentHtml'] = $post->content_html;
} else {
$attributes['content'] = $post->content;
}

View File

@ -0,0 +1,48 @@
<?php namespace Flarum\Core\Formatter;
use Flarum\Core\Models\Post;
use Closure;
abstract class FormatterAbstract
{
public function beforePurification($text, Post $post = null)
{
return $text;
}
public function afterPurification($text, Post $post = null)
{
return $text;
}
protected function ignoreTags($text, array $tags, Closure $callback)
{
$chunks = preg_split('/(<.+?>)/is', $text, 0, PREG_SPLIT_DELIM_CAPTURE);
$openTag = null;
for ($i = 0; $i < count($chunks); $i++) {
if ($i % 2 === 0) { // even numbers are text
// Only process this chunk if there are no unclosed $ignoreTags
if (null === $openTag) {
$chunks[$i] = $callback($chunks[$i]);
}
} else { // odd numbers are tags
// Only process this tag if there are no unclosed $ignoreTags
if (null === $openTag) {
// Check whether this tag is contained in $ignoreTags and is not self-closing
if (preg_match("`<(" . implode('|', $tags) . ").*(?<!/)>$`is", $chunks[$i], $matches)) {
$openTag = $matches[1];
}
} else {
// Otherwise, check whether this is the closing tag for $openTag.
if (preg_match('`</\s*' . $openTag . '>`i', $chunks[$i], $matches)) {
$openTag = null;
}
}
}
}
return implode($chunks);
}
}

View File

@ -0,0 +1,10 @@
<?php namespace Flarum\Core\Formatter;
use Flarum\Core\Models\Post;
interface FormatterInterface
{
public function beforePurification($text, Post $post = null);
public function afterPurification($text, Post $post = null);
}

View File

@ -58,8 +58,13 @@ class FormatterManager
public function format($text, $post = null)
{
$formatters = [];
foreach ($this->getFormatters() as $formatter) {
$text = $this->container->make($formatter)->format($text, $post);
$formatters[] = $this->container->make($formatter);
}
foreach ($formatters as $formatter) {
$text = $formatter->beforePurification($text, $post);
}
// Studio does not yet merge autoload_files...
@ -75,16 +80,10 @@ class FormatterManager
$purifier = new HTMLPurifier($config);
return $purifier->purify($text);
}
$text = $purifier->purify($text);
public function strip($text)
{
foreach ($this->getFormatters() as $formatter) {
$formatter = $this->container->make($formatter);
if (method_exists($formatter, 'strip')) {
$text = $formatter->strip($text);
}
foreach ($formatters as $formatter) {
$text = $formatter->afterPurification($text, $post);
}
return $text;

View File

@ -1,8 +1,9 @@
<?php namespace Flarum\Core\Formatter;
use Flarum\Core\Models\Post;
use Misd\Linkify\Linkify;
class LinkifyFormatter
class LinkifyFormatter extends FormatterAbstract
{
protected $linkify;
@ -11,7 +12,7 @@ class LinkifyFormatter
$this->linkify = $linkify;
}
public function format($text)
public function beforePurification($text, Post $post = null)
{
return $this->linkify->process($text, ['attr' => ['target' => '_blank']]);
}

View File

@ -119,17 +119,6 @@ class CommentPost extends Post
return $value;
}
/**
* Get the content formatter as HTML.
*
* @param string $value
* @return string
*/
public function getContentPlainAttribute()
{
return static::$formatter->strip($this->content);
}
/**
* Get text formatter instance.
*