mirror of
https://github.com/flarum/framework.git
synced 2025-02-23 03:26:24 +08:00
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:
parent
80f766127a
commit
944e5c649c
@ -9,6 +9,20 @@ export default class PostPreview extends Component {
|
|||||||
var post = this.props.post;
|
var post = this.props.post;
|
||||||
var user = post.user();
|
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', {
|
return m('a.post-preview', {
|
||||||
href: app.route.post(post),
|
href: app.route.post(post),
|
||||||
config: m.route,
|
config: m.route,
|
||||||
@ -17,7 +31,7 @@ export default class PostPreview extends Component {
|
|||||||
avatar(user), ' ',
|
avatar(user), ' ',
|
||||||
username(user), ' ',
|
username(user), ' ',
|
||||||
humanTime(post.time()), ' ',
|
humanTime(post.time()), ' ',
|
||||||
highlight(post.excerpt(), this.props.highlight)
|
excerpt
|
||||||
]));
|
]));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -12,7 +12,7 @@ Post.prototype.user = Model.one('user');
|
|||||||
Post.prototype.contentType = Model.prop('contentType');
|
Post.prototype.contentType = Model.prop('contentType');
|
||||||
Post.prototype.content = Model.prop('content');
|
Post.prototype.content = Model.prop('content');
|
||||||
Post.prototype.contentHtml = Model.prop('contentHtml');
|
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.editTime = Model.prop('editTime', Model.date);
|
||||||
Post.prototype.editUser = Model.one('editUser');
|
Post.prototype.editUser = Model.one('editUser');
|
||||||
|
@ -25,7 +25,7 @@ class PostBasicSerializer extends BaseSerializer
|
|||||||
];
|
];
|
||||||
|
|
||||||
if ($post->type === 'comment') {
|
if ($post->type === 'comment') {
|
||||||
$attributes['excerpt'] = str_limit($post->contentPlain, 200);
|
$attributes['contentHtml'] = $post->content_html;
|
||||||
} else {
|
} else {
|
||||||
$attributes['content'] = $post->content;
|
$attributes['content'] = $post->content;
|
||||||
}
|
}
|
||||||
|
48
framework/core/src/Core/Formatter/FormatterAbstract.php
Normal file
48
framework/core/src/Core/Formatter/FormatterAbstract.php
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
10
framework/core/src/Core/Formatter/FormatterInterface.php
Normal file
10
framework/core/src/Core/Formatter/FormatterInterface.php
Normal 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);
|
||||||
|
}
|
@ -58,8 +58,13 @@ class FormatterManager
|
|||||||
|
|
||||||
public function format($text, $post = null)
|
public function format($text, $post = null)
|
||||||
{
|
{
|
||||||
|
$formatters = [];
|
||||||
foreach ($this->getFormatters() as $formatter) {
|
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...
|
// Studio does not yet merge autoload_files...
|
||||||
@ -75,16 +80,10 @@ class FormatterManager
|
|||||||
|
|
||||||
$purifier = new HTMLPurifier($config);
|
$purifier = new HTMLPurifier($config);
|
||||||
|
|
||||||
return $purifier->purify($text);
|
$text = $purifier->purify($text);
|
||||||
}
|
|
||||||
|
|
||||||
public function strip($text)
|
foreach ($formatters as $formatter) {
|
||||||
{
|
$text = $formatter->afterPurification($text, $post);
|
||||||
foreach ($this->getFormatters() as $formatter) {
|
|
||||||
$formatter = $this->container->make($formatter);
|
|
||||||
if (method_exists($formatter, 'strip')) {
|
|
||||||
$text = $formatter->strip($text);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return $text;
|
return $text;
|
||||||
|
@ -1,8 +1,9 @@
|
|||||||
<?php namespace Flarum\Core\Formatter;
|
<?php namespace Flarum\Core\Formatter;
|
||||||
|
|
||||||
|
use Flarum\Core\Models\Post;
|
||||||
use Misd\Linkify\Linkify;
|
use Misd\Linkify\Linkify;
|
||||||
|
|
||||||
class LinkifyFormatter
|
class LinkifyFormatter extends FormatterAbstract
|
||||||
{
|
{
|
||||||
protected $linkify;
|
protected $linkify;
|
||||||
|
|
||||||
@ -11,7 +12,7 @@ class LinkifyFormatter
|
|||||||
$this->linkify = $linkify;
|
$this->linkify = $linkify;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function format($text)
|
public function beforePurification($text, Post $post = null)
|
||||||
{
|
{
|
||||||
return $this->linkify->process($text, ['attr' => ['target' => '_blank']]);
|
return $this->linkify->process($text, ['attr' => ['target' => '_blank']]);
|
||||||
}
|
}
|
||||||
|
@ -119,17 +119,6 @@ class CommentPost extends Post
|
|||||||
return $value;
|
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.
|
* Get text formatter instance.
|
||||||
*
|
*
|
||||||
|
Loading…
x
Reference in New Issue
Block a user