2015-12-02 14:38:40 +00:00
|
|
|
<?php
|
|
|
|
/*
|
|
|
|
* BBcode Wrapper
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Sakura;
|
|
|
|
|
|
|
|
use JBBCode\Parser;
|
|
|
|
use JBBCode\DefaultCodeDefinitionSet;
|
|
|
|
use JBBCode\CodeDefinitionBuilder;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class BBcode
|
|
|
|
* @package Sakura
|
|
|
|
*/
|
|
|
|
class BBcode
|
|
|
|
{
|
|
|
|
// Parser container
|
2015-12-03 18:41:14 +00:00
|
|
|
private static $bbcode = null;
|
2015-12-02 14:38:40 +00:00
|
|
|
|
|
|
|
// Constructor
|
2015-12-03 18:41:14 +00:00
|
|
|
public static function init()
|
2015-12-02 14:40:28 +00:00
|
|
|
{
|
2015-12-02 14:38:40 +00:00
|
|
|
// Create new parser class
|
2015-12-03 18:41:14 +00:00
|
|
|
self::$bbcode = new Parser();
|
2015-12-02 14:38:40 +00:00
|
|
|
|
|
|
|
// Add the standard definitions
|
2015-12-03 18:41:14 +00:00
|
|
|
self::loadStandardCodes();
|
2015-12-02 14:38:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add basic bbcodes
|
2015-12-03 18:41:14 +00:00
|
|
|
public static function loadStandardCodes()
|
2015-12-02 14:40:28 +00:00
|
|
|
{
|
2015-12-02 14:38:40 +00:00
|
|
|
// Add the standard definitions
|
2015-12-03 18:41:14 +00:00
|
|
|
self::$bbcode->addCodeDefinitionSet(new DefaultCodeDefinitionSet());
|
2015-12-02 14:38:40 +00:00
|
|
|
|
|
|
|
// Header tag
|
2015-12-02 18:15:34 +00:00
|
|
|
$builder = new CodeDefinitionBuilder('header', '<h1>{param}</h1>');
|
2015-12-03 18:41:14 +00:00
|
|
|
self::$bbcode->addCodeDefinition($builder->build());
|
2015-12-02 14:38:40 +00:00
|
|
|
|
|
|
|
// Strike tag
|
|
|
|
$builder = new CodeDefinitionBuilder('s', '<del>{param}</del>');
|
2015-12-03 18:41:14 +00:00
|
|
|
self::$bbcode->addCodeDefinition($builder->build());
|
2015-12-02 14:38:40 +00:00
|
|
|
|
|
|
|
// Spoiler tag
|
2015-12-02 18:15:34 +00:00
|
|
|
$builder = new CodeDefinitionBuilder('spoiler', '<span class="spoiler">{param}</span>');
|
2015-12-03 18:41:14 +00:00
|
|
|
self::$bbcode->addCodeDefinition($builder->build());
|
2015-12-02 14:38:40 +00:00
|
|
|
|
|
|
|
// Box tag
|
2015-12-02 18:15:34 +00:00
|
|
|
$builder = new CodeDefinitionBuilder('box', '<div class="spoiler-box-container"><div class="spoiler-box-title" onclick="toggleClass(this.parentNode.children[1], \'hidden\');">Click to open</div><div class="spoiler-box-content hidden">{param}</div></div>');
|
2015-12-03 18:41:14 +00:00
|
|
|
self::$bbcode->addCodeDefinition($builder->build());
|
2015-12-02 14:38:40 +00:00
|
|
|
|
|
|
|
// Box tag
|
2015-12-02 18:15:34 +00:00
|
|
|
$builder = new CodeDefinitionBuilder('box', '<div class="spoiler-box-container"><div class="spoiler-box-title" onclick="toggleClass(this.parentNode.children[1], \'hidden\');">{option}</div><div class="spoiler-box-content hidden">{param}</div></div>');
|
2015-12-02 14:38:40 +00:00
|
|
|
$builder->setUseOption(true);
|
2015-12-03 18:41:14 +00:00
|
|
|
self::$bbcode->addCodeDefinition($builder->build());
|
2015-12-02 14:38:40 +00:00
|
|
|
|
|
|
|
// Quote tag
|
2015-12-03 18:41:14 +00:00
|
|
|
$builder = new CodeDefinitionBuilder('quote', '<blockquote><div class="quotee">Quote</div><div class="quote">{param}</div></blockquote>');
|
|
|
|
self::$bbcode->addCodeDefinition($builder->build());
|
2015-12-02 14:38:40 +00:00
|
|
|
|
|
|
|
// Quote tag
|
2015-12-03 18:41:14 +00:00
|
|
|
$builder = new CodeDefinitionBuilder('quote', '<blockquote><div class="quotee">{option} wrote</div><div class="quote">{param}</div></blockquote>');
|
2015-12-02 14:38:40 +00:00
|
|
|
$builder->setUseOption(true);
|
2015-12-03 18:41:14 +00:00
|
|
|
self::$bbcode->addCodeDefinition($builder->build());
|
2015-12-02 14:38:40 +00:00
|
|
|
|
|
|
|
// Add special definitions (PHP files MUST have the same name as the definition class
|
|
|
|
foreach (glob(ROOT . '_sakura/components/BBcodeDefinitions/*.php') as $ext) {
|
|
|
|
// Include the class
|
|
|
|
require_once $ext;
|
|
|
|
|
|
|
|
// Clean the file path
|
|
|
|
$ext = str_replace(ROOT . '_sakura/components/', '', $ext);
|
|
|
|
$ext = str_replace('.php', '', $ext);
|
|
|
|
$ext = str_replace('/', '\\', $ext);
|
|
|
|
|
|
|
|
// Build the classname
|
|
|
|
$className = __NAMESPACE__ . '\\' . $ext;
|
|
|
|
|
|
|
|
// Add the BBcode definition
|
2015-12-03 18:41:14 +00:00
|
|
|
self::$bbcode->addCodeDefinition(new $className);
|
2015-12-02 14:38:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set text
|
2015-12-03 18:41:14 +00:00
|
|
|
public static function text($text)
|
2015-12-02 14:40:28 +00:00
|
|
|
{
|
2015-12-03 18:41:14 +00:00
|
|
|
// Check if $bbcode is still null
|
|
|
|
if (self::$bbcode == null) {
|
|
|
|
self::init();
|
|
|
|
}
|
|
|
|
|
|
|
|
self::$bbcode->parse($text);
|
2015-12-02 14:38:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get as HTML
|
2015-12-03 18:41:14 +00:00
|
|
|
public static function toHTML($text = null)
|
2015-12-02 14:40:28 +00:00
|
|
|
{
|
2015-12-03 18:41:14 +00:00
|
|
|
// Check if text isn't null
|
|
|
|
if ($text !== null) {
|
|
|
|
self::text($text);
|
|
|
|
}
|
|
|
|
|
|
|
|
$parsed = nl2br(self::$bbcode->getAsHtml());
|
2015-12-02 18:15:34 +00:00
|
|
|
|
|
|
|
$parsed = Main::fixCodeTags($parsed);
|
|
|
|
$parsed = Main::parseEmotes($parsed);
|
|
|
|
|
|
|
|
return $parsed;
|
2015-12-02 14:38:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get as BBmarkup
|
2015-12-03 18:41:14 +00:00
|
|
|
public static function toEditor($text = null)
|
2015-12-02 14:40:28 +00:00
|
|
|
{
|
2015-12-03 18:41:14 +00:00
|
|
|
// Check if text isn't null
|
|
|
|
if ($text !== null) {
|
|
|
|
self::text($text);
|
|
|
|
}
|
|
|
|
|
|
|
|
return self::$bbcode->getAsBBCode();
|
2015-12-02 14:38:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get as plaintext
|
2015-12-03 18:41:14 +00:00
|
|
|
public static function toPlain($text = null)
|
2015-12-02 14:40:28 +00:00
|
|
|
{
|
2015-12-03 18:41:14 +00:00
|
|
|
// Check if text isn't null
|
|
|
|
if ($text !== null) {
|
|
|
|
self::text($text);
|
|
|
|
}
|
|
|
|
|
|
|
|
return self::$bbcode->getAsText();
|
2015-12-02 14:38:40 +00:00
|
|
|
}
|
|
|
|
}
|