This repository has been archived on 2024-06-26. You can view files and clone it, but cannot push or open issues or pull requests.
sakura/app/BBCode/Parser.php

100 lines
2.2 KiB
PHP
Raw Normal View History

2016-08-06 17:31:32 +00:00
<?php
/**
* Holds the BBcode handler.
* @package Sakura
*/
namespace Sakura\BBCode;
use Sakura\DB;
use Sakura\User;
2016-08-06 17:31:32 +00:00
/**
* BBcode handler.
* @package Sakura
* @author Julian van de Groep <me@flash.moe>
*/
class Parser
{
/**
* Holds the bbcode parsers.
* @var array
*/
public static $parsers = [
// Advanced parsing
Tags\Code::class,
Tags\Box::class,
Tags\ListTag::class,
Tags\UserTag::class,
Tags\Markdown::class,
// Quotes
Tags\NamedQuote::class,
Tags\Quote::class,
// More advanced
Tags\Colour::class,
Tags\Align::class,
Tags\Size::class,
Tags\YouTube::class,
2016-08-06 17:31:32 +00:00
// Basic markup
Tags\Bold::class,
Tags\Italics::class,
Tags\Underline::class,
Tags\Strike::class,
Tags\Header::class,
Tags\Image::class,
Tags\Spoiler::class,
// Links
Tags\NamedLink::class,
Tags\Link::class,
// Newline must always be last
Tags\Newline::class,
];
/**
* Parse the emoticons.
* @param string $text
2016-12-04 16:33:52 +00:00
* @param User $poster
2016-08-06 17:31:32 +00:00
* @return string
*/
2016-12-04 16:33:52 +00:00
public static function parseEmoticons(string $text, User $poster = null): string
2016-08-06 17:31:32 +00:00
{
$emotes = DB::table('emoticons')
->get();
foreach ($emotes as $emote) {
2016-10-13 18:45:44 +00:00
if ($poster === null) {
// eventually check for hierarchies here
continue;
}
2016-08-06 17:31:32 +00:00
$image = "<img src='{$emote->emote_path}' alt='{$emote->emote_string}' class='emoticon'>";
$icon = preg_quote($emote->emote_string, '#');
$text = preg_replace("#{$icon}#", $image, $text);
}
return $text;
}
/**
* Convert the parsed text to HTML.
* @param string $text
2016-12-04 16:33:52 +00:00
* @param User $poster
2016-08-06 17:31:32 +00:00
* @return string
*/
2016-12-04 16:33:52 +00:00
public static function toHTML(string $text, User $poster): string
2016-08-06 17:31:32 +00:00
{
$text = self::parseEmoticons($text);
foreach (self::$parsers as $parser) {
$text = call_user_func_array([$parser, 'parse'], [$text, $poster]);
2016-08-06 17:31:32 +00:00
}
return $text;
}
}