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/libraries/BBcode.php

168 lines
4.8 KiB
PHP
Raw Normal View History

<?php
2016-02-03 22:22:56 +00:00
/**
* Holds the BBcode handler.
*
* @package Sakura
*/
namespace Sakura;
use JBBCode\Parser;
use JBBCode\DefaultCodeDefinitionSet;
use JBBCode\CodeDefinitionBuilder;
/**
2016-02-02 21:04:15 +00:00
* Sakura wrapper for JBBCode.
*
* @package Sakura
2016-02-02 21:04:15 +00:00
* @author Julian van de Groep <me@flash.moe>
*/
class BBcode
{
2016-02-02 21:04:15 +00:00
/**
* The container for JBBCode.
*
* @var Parser
*/
private static $bbcode = null;
2016-02-02 21:04:15 +00:00
/**
* Initialiser.
*/
public static function init()
2015-12-02 14:40:28 +00:00
{
// Create new parser class
self::$bbcode = new Parser();
// Add the standard definitions
self::loadStandardCodes();
}
2016-02-02 21:04:15 +00:00
/**
* Adds the standard BBcode.
*/
public static function loadStandardCodes()
2015-12-02 14:40:28 +00:00
{
// Add the standard definitions
self::$bbcode->addCodeDefinitionSet(new DefaultCodeDefinitionSet());
// Header tag
$builder = new CodeDefinitionBuilder('header', '<h1>{param}</h1>');
self::$bbcode->addCodeDefinition($builder->build());
// Strike tag
$builder = new CodeDefinitionBuilder('s', '<del>{param}</del>');
self::$bbcode->addCodeDefinition($builder->build());
// Spoiler tag
$builder = new CodeDefinitionBuilder('spoiler', '<span class="spoiler">{param}</span>');
self::$bbcode->addCodeDefinition($builder->build());
// Box tag
$builder = new CodeDefinitionBuilder('box', '<div class="spoiler-box-container"><div class="spoiler-box-title" onclick="Sakura.toggleClass(this.parentNode.children[1], \'hidden\');">Click to open</div><div class="spoiler-box-content hidden">{param}</div></div>');
self::$bbcode->addCodeDefinition($builder->build());
// Box tag
$builder = new CodeDefinitionBuilder('box', '<div class="spoiler-box-container"><div class="spoiler-box-title" onclick="Sakura.toggleClass(this.parentNode.children[1], \'hidden\');">{option}</div><div class="spoiler-box-content hidden">{param}</div></div>');
$builder->setUseOption(true);
self::$bbcode->addCodeDefinition($builder->build());
// Quote tag
$builder = new CodeDefinitionBuilder('quote', '<blockquote><div class="quotee">Quote</div><div class="quote">{param}</div></blockquote>');
self::$bbcode->addCodeDefinition($builder->build());
// Quote tag
$builder = new CodeDefinitionBuilder('quote', '<blockquote><div class="quotee">{option} wrote</div><div class="quote">{param}</div></blockquote>');
$builder->setUseOption(true);
self::$bbcode->addCodeDefinition($builder->build());
// Add special definitions (PHP files MUST have the same name as the definition class
foreach (glob(ROOT . 'libraries/BBcodeDefinitions/*.php') as $ext) {
// Include the class
require_once $ext;
// Clean the file path
$ext = str_replace(ROOT . 'libraries/', '', $ext);
$ext = str_replace('.php', '', $ext);
$ext = str_replace('/', '\\', $ext);
// Build the classname
$className = __NAMESPACE__ . '\\' . $ext;
// Add the BBcode definition
self::$bbcode->addCodeDefinition(new $className);
}
}
2016-02-02 21:04:15 +00:00
/**
* Set the text to parse.
*
* @param string $text The text that should be parsed.
*/
public static function text($text)
2015-12-02 14:40:28 +00:00
{
// Check if $bbcode is still null
if (self::$bbcode == null) {
self::init();
}
self::$bbcode->parse($text);
}
2016-02-02 21:04:15 +00:00
/**
* Convert the parsed text to HTML.
*
* @param string $text The text that should be parsed.
*
* @return string The parsed HTML.
*/
public static function toHTML($text = null)
2015-12-02 14:40:28 +00:00
{
// Check if text isn't null
if ($text !== null) {
self::text($text);
}
$parsed = nl2br(self::$bbcode->getAsHtml());
2016-01-17 01:58:31 +00:00
$parsed = Utils::fixCodeTags($parsed);
$parsed = Utils::parseEmotes($parsed);
return $parsed;
}
2016-02-02 21:04:15 +00:00
/**
* Convert the parsed text to BBCode.
*
* @param string $text The text that should be parsed.
*
* @return string The converted bbcode.
*/
public static function toEditor($text = null)
2015-12-02 14:40:28 +00:00
{
// Check if text isn't null
if ($text !== null) {
self::text($text);
}
return self::$bbcode->getAsBBCode();
}
2016-02-02 21:04:15 +00:00
/**
* Convert the parsed text to plain.
*
* @param string $text The text that should be parsed.
*
* @return string The converted plaintext.
*/
public static function toPlain($text = null)
2015-12-02 14:40:28 +00:00
{
// Check if text isn't null
if ($text !== null) {
self::text($text);
}
return self::$bbcode->getAsText();
}
}