1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125:
<?php
class BBCodeHighlighter extends AbstractParser
{
const BBCODE_HIGHLIGHTER_INLINE_CODE = true;
const BBCODE_HIGHLIGHTER_BLOCK_CODE = false;
private static $bbcode_tag_color = '#0000FF';
private static $bbcode_param_color = '#7B00FF';
private static $bbcode_param_name_color = '#FF0000';
private static $bbcode_list_item_color = '#00AF07';
public function __construct()
{
parent::__construct();
}
public function parse($inline_code = self::BBCODE_HIGHLIGHTER_BLOCK_CODE)
{
$this->content = TextHelper::htmlspecialchars($this->content);
$this->content = str_replace('[line]', '<span style="color:' . self::$bbcode_tag_color . ';">[line]</span>', $this->content);
$this->content = str_replace('[*]', '<span style="color:' . self::$bbcode_list_item_color . ';">[*]</span>', $this->content);
$simple_tags = array('b', 'i', 'u', 's', 'p', 'sup', 'sub', 'pre', 'math', 'quote', 'block', 'fieldset', 'sound', 'url', 'img', 'mail', 'code', 'tr', 'html', 'row', 'indent', 'hide', 'mail', 'fa', 'member', 'moderator', 'container');
foreach ($simple_tags as $tag)
{
while (preg_match('`\[' . $tag . '\](.*)\[/' . $tag . '\]`isuU', $this->content))
{
$this->content = preg_replace('`\[' . $tag . '\](.*)\[/' . $tag . '\]`isuU', '<span style="color:' . self::$bbcode_tag_color . ';">/[/' . $tag . '/]/</span>$1<span style="color:' . self::$bbcode_tag_color . ';">/[//' . $tag . '/]/</span>', $this->content);
}
}
$tags_with_simple_property = array('img', 'color', 'bgcolor', 'size', 'font', 'align', 'float', 'anchor', 'acronym', 'title', 'stitle', 'style', 'url', 'mail', 'code', 'quote', 'movie', 'swf', 'youtube', 'fa');
foreach ($tags_with_simple_property as $tag)
{
while (preg_match('`\[' . $tag . '=([^\]]+)\](.*)\[/' . $tag . '\]`isuU', $this->content))
{
$this->content = preg_replace('`\[' . $tag . '=([^\]]+)\](.*)\[/' . $tag . '\]`isuU', '<span style="color:' . self::$bbcode_tag_color . ';">/[/' . $tag . '</span>=<span style="color:' . self::$bbcode_param_color . ';">$1</span><span style="color:' . self::$bbcode_tag_color . ';">/]/</span>$2<span style="color:' . self::$bbcode_tag_color . ';">/[//' . $tag . '/]/</span>', $this->content);
}
}
$tags_with_many_parameters = array('table', 'col', 'head', 'list', 'fieldset', 'block', 'wikipedia', 'container');
foreach ($tags_with_many_parameters as $tag)
{
while (preg_match('`\[(' . $tag . ')([^\]]*)\](.*)\[/' . $tag . '\]`isuU', $this->content))
{
$this->content = preg_replace_callback('`\[(' . $tag . ')([^\]]*)\](.*)\[/' . $tag . '\]`isuU', array($this, 'highlight_bbcode_tag_with_many_parameters'), $this->content);
}
}
if ($inline_code == self::BBCODE_HIGHLIGHTER_BLOCK_CODE)
{
$this->content = '<pre>' . $this->content . '</pre>';
}
else
{
$this->content = '<pre style="display:inline;">' . $this->content . '</pre>';
}
$this->content = str_replace(array('/[/', '/]/'), array('[', ']'), $this->content);
}
private function highlight_bbcode_tag_with_many_parameters($matches)
{
$content = $matches[3];
$tag_name = $matches[1];
$matches[2] = preg_replace('`([a-z]+)="([^"]*)"`isuU', '<span style="color:' . self::$bbcode_param_name_color . '">$1</span>=<span style="color:' . self::$bbcode_param_color . '">"$2"</span>', $matches[2]);
return '<span style="color:' . self::$bbcode_tag_color . '">/[/' . $tag_name . '</span>' .$matches[2] . '<span style="color:' . self::$bbcode_tag_color . '">/]/</span>' . $content . '<span style="color:' . self::$bbcode_tag_color . '">/[//' . $tag_name . '/]/</span>';
}
}
?>