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
|
|
|
|
private $bbcode;
|
|
|
|
|
|
|
|
// Constructor
|
2015-12-02 14:40:28 +00:00
|
|
|
public function __construct($text = null)
|
|
|
|
{
|
2015-12-02 14:38:40 +00:00
|
|
|
// Create new parser class
|
|
|
|
$this->bbcode = new Parser();
|
|
|
|
|
|
|
|
// Add the standard definitions
|
|
|
|
$this->loadStandardCodes();
|
|
|
|
|
|
|
|
// Immediately parse the text if set
|
|
|
|
if ($text != null) {
|
|
|
|
$this->bbcode->parse($text);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add basic bbcodes
|
2015-12-02 14:40:28 +00:00
|
|
|
public function loadStandardCodes()
|
|
|
|
{
|
2015-12-02 14:38:40 +00:00
|
|
|
// Add the standard definitions
|
|
|
|
$this->bbcode->addCodeDefinitionSet(new DefaultCodeDefinitionSet());
|
|
|
|
|
|
|
|
// Header tag
|
2015-12-02 18:15:34 +00:00
|
|
|
$builder = new CodeDefinitionBuilder('header', '<h1>{param}</h1>');
|
2015-12-02 14:38:40 +00:00
|
|
|
$this->bbcode->addCodeDefinition($builder->build());
|
|
|
|
|
|
|
|
// Strike tag
|
|
|
|
$builder = new CodeDefinitionBuilder('s', '<del>{param}</del>');
|
|
|
|
$this->bbcode->addCodeDefinition($builder->build());
|
|
|
|
|
|
|
|
// Spoiler tag
|
2015-12-02 18:15:34 +00:00
|
|
|
$builder = new CodeDefinitionBuilder('spoiler', '<span class="spoiler">{param}</span>');
|
2015-12-02 14:38:40 +00:00
|
|
|
$this->bbcode->addCodeDefinition($builder->build());
|
|
|
|
|
|
|
|
// 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-02 14:38:40 +00:00
|
|
|
$this->bbcode->addCodeDefinition($builder->build());
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
$this->bbcode->addCodeDefinition($builder->build());
|
|
|
|
|
|
|
|
// Quote tag
|
2015-12-02 18:15:34 +00:00
|
|
|
$builder = new CodeDefinitionBuilder('quote', '<blockquote><div class="quote">{param}</div></blockquote>');
|
2015-12-02 14:38:40 +00:00
|
|
|
$this->bbcode->addCodeDefinition($builder->build());
|
|
|
|
|
|
|
|
// Quote tag
|
2015-12-02 18:15:34 +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);
|
|
|
|
$this->bbcode->addCodeDefinition($builder->build());
|
|
|
|
|
|
|
|
// 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
|
|
|
|
$this->bbcode->addCodeDefinition(new $className);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set text
|
2015-12-02 14:40:28 +00:00
|
|
|
public function text($text)
|
|
|
|
{
|
2015-12-02 14:38:40 +00:00
|
|
|
$this->bbcode->parse($text);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get as HTML
|
2015-12-02 14:40:28 +00:00
|
|
|
public function toHTML()
|
|
|
|
{
|
2015-12-02 18:15:34 +00:00
|
|
|
$parsed = nl2br($this->bbcode->getAsHtml());
|
|
|
|
|
|
|
|
$parsed = Main::fixCodeTags($parsed);
|
|
|
|
$parsed = Main::parseEmotes($parsed);
|
|
|
|
|
|
|
|
return $parsed;
|
2015-12-02 14:38:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get as BBmarkup
|
2015-12-02 14:40:28 +00:00
|
|
|
public function toEditor()
|
|
|
|
{
|
2015-12-02 14:38:40 +00:00
|
|
|
return $this->bbcode->getAsBBCode();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get as plaintext
|
2015-12-02 14:40:28 +00:00
|
|
|
public function toPlain()
|
|
|
|
{
|
2015-12-02 14:38:40 +00:00
|
|
|
return $this->bbcode->getAsText();
|
|
|
|
}
|
|
|
|
}
|