Version: current

      1 <?php
      2 require_once('HTML/HTMLBBCodeParser.php');
      3 /*Seting up the PEAR bbcode parser*/
      4 $config = parse_ini_file('BBCodeParser.ini', true);
      5 $options = &SSHTMLBBCodeParser::getStaticProperty('SSHTMLBBCodeParser', '_options');
      6 $options = $config['SSHTMLBBCodeParser'];
      7 //Debug::show($options);
      8 unset($options);
      9
     10
     11 /**
     12  * BBCode parser object.
     13  * Use on a text field in a template with $Content.Parse(BBCodeParser).
     14  * @package sapphire
     15  * @subpackage misc
     16  */
     17 class BBCodeParser extends TextParser {
     18
     19     /**
     20      * Set whether phrases starting with http:// or www. are automatically linked
     21      * @var Boolean
     22      */
     23     protected static $autolinkUrls = true;
     24
     25     /**
     26      * Set whether similies :), :(, :P are converted to images
     27      * @var Boolean
     28      */
     29     protected static $allowSimilies = false;
     30
     31     /**
     32      * Set the location of the smiles folder. By default use the ones in sapphire
     33      * but this can be overridden by setting  BBCodeParser::set_icon_folder('themes/yourtheme/images/');
     34      * @var string
     35      */
     36     protected static $smilies_location = 'sapphire/images/smilies';
     37
     38     static function smilies_location() {
     39         return BBCodeParser::$smilies_location;
     40     }
     41     static function set_icon_folder($path) {
     42         BBCodeParser::$smilies_location = $path;
     43     }
     44
     45     static function autolinkUrls() {
     46         return (self::$autolinkUrls != null) ? true : false;
     47     }
     48
     49     static function disable_autolink_urls($autolink = false) {
     50         BBCodeParser::$autolinkUrls = $autolink;
     51     }
     52
     53     static function smiliesAllowed() {
     54         return (self::$allowSimilies != null) ? true : false;
     55     }
     56
     57     static function enable_smilies() {
     58         BBCodeParser::$allowSimilies = true;
     59     }
     60
     61
     62     static function usable_tags() {
     63         return new DataObjectSet(
     64             new ArrayData(array(
     65                 "Title" => _t('BBCodeParser.BOLD', 'Bold Text'),
     66                 "Example" => '[b]<b>'._t('BBCodeParser.BOLDEXAMPLE', 'Bold').'</b>[/b]'
     67             )),
     68             new ArrayData(array(
     69                 "Title" => _t('BBCodeParser.ITALIC', 'Italic Text'),
     70                 "Example" => '[i]<i>'._t('BBCodeParser.ITALICEXAMPLE', 'Italics').'</i>[/i]'
     71             )),
     72             new ArrayData(array(
     73                 "Title" => _t('BBCodeParser.UNDERLINE', 'Underlined Text'),
     74                 "Example" => '[u]<u>'._t('BBCodeParser.UNDERLINEEXAMPLE', 'Underlined').'</u>[/u]'
     75             )),
     76             new ArrayData(array(
     77                 "Title" => _t('BBCodeParser.STRUCK', 'Struck-out Text'),
     78                 "Example" => '[s]<s>'._t('BBCodeParser.STRUCKEXAMPLE', 'Struck-out').'</s>[/s]'
     79             )),
     80             new ArrayData(array(
     81                 "Title" => _t('BBCodeParser.COLORED', 'Colored text'),
     82                 "Example" => '[color=blue]'._t('BBCodeParser.COLOREDEXAMPLE', 'blue text').'[/color]'
     83             )),
     84             new ArrayData(array(
     85                 "Title" => _t('BBCodeParser.ALIGNEMENT', 'Alignment'),
     86                 "Example" => '[align=right]'._t('BBCodeParser.ALIGNEMENTEXAMPLE', 'right aligned').'[/align]'
     87             )),
     88             new ArrayData(array(
     89                 "Title" => _t('BBCodeParser.CODE', 'Code Block'),
     90                 "Description" => _t('BBCodeParser.CODEDESCRIPTION', 'Unformatted code block'),
     91                 "Example" => '[code]'._t('BBCodeParser.CODEEXAMPLE', 'Code block').'[/code]'
     92             )),
     93             new ArrayData(array(
     94                 "Title" => _t('BBCodeParser.EMAILLINK', 'Email link'),
     95                 "Description" => _t('BBCodeParser.EMAILLINKDESCRIPTION', 'Create link to an email address'),
     96                 "Example" => "[email]you@yoursite.com[/email]"
     97             )),
     98             new ArrayData(array(
     99                 "Title" => _t('BBCodeParser.EMAILLINK', 'Email link'),
    100                 "Description" => _t('BBCodeParser.EMAILLINKDESCRIPTION', 'Create link to an email address'),
    101                 "Example" => "[email=you@yoursite.com]Email[/email]"
    102             )),
    103             new ArrayData(array(
    104                 "Title" => _t('BBCodeParser.UNORDERED', 'Unordered list'),
    105                 "Description" => _t('BBCodeParser.UNORDEREDDESCRIPTION', 'Unordered list'),
    106                 "Example" => '[ulist][*]'._t('BBCodeParser.UNORDEREDEXAMPLE1', 'unordered item 1').'[/ulist]'
    107             )),
    108             new ArrayData(array(
    109                 "Title" => _t('BBCodeParser.IMAGE', 'Image'),
    110                 "Description" => _t('BBCodeParser.IMAGEDESCRIPTION', 'Show an image in your post'),
    111                 "Example" => "[img]http://www.website.com/image.jpg[/img]"
    112             )),
    113             new ArrayData(array(
    114                 "Title" => _t('BBCodeParser.LINK', 'Website link'),
    115                 "Description" => _t('BBCodeParser.LINKDESCRIPTION', 'Link to another website or URL'),
    116                 "Example" => '[url]http://www.website.com/[/url]'
    117             )),
    118             new ArrayData(array(
    119                 "Title" => _t('BBCodeParser.LINK', 'Website link'),
    120                 "Description" => _t('BBCodeParser.LINKDESCRIPTION', 'Link to another website or URL'),
    121                 "Example" => "[url=http://www.website.com/]Website[/url]"
    122             ))
    123         );
    124     }
    125
    126     function useable_tagsHTML(){
    127         $useabletags = "<ul class='bbcodeExamples'>";
    128         foreach($this->usable_tags()->toArray() as $tag){
    129             $useabletags = $useabletags."<li><span>".$tag->Example."</span></li>";
    130         }
    131         return $useabletags."</ul>";
    132     }
    133
    134     /**
    135      * Main BBCode parser method. This takes plain jane content and
    136      * runs it through so many filters
    137      *
    138      * @return Text
    139      */
    140     function parse() {
    141         $this->content = str_replace(array('&', '<', '>'), array('&amp;', '&lt;', '&gt;'), $this->content);
    142         $this->content = SSHTMLBBCodeParser::staticQparse($this->content);
    143         $this->content = "<p>".$this->content."</p>";
    144
    145         $this->content = preg_replace('/(<p[^>]*>)\s+/i', '\\1', $this->content);
    146         $this->content = preg_replace('/\s+(<\/p[^>]*>)/i', '\\1', $this->content);
    147
    148         $this->content = preg_replace("/\n\s*\n/", "</p><p>", $this->content);
    149         $this->content = str_replace("\n", "<br />", $this->content);
    150
    151         if(BBCodeParser::smiliesAllowed()) {
    152             $smilies = array(
    153                 '#(?<!\w):D(?!\w)#i'         => " <img src='".BBCodeParser::smilies_location(). "/grin.gif'> ", // :D
    154                 '#(?<!\w):\)(?!\w)#i'        => " <img src='".BBCodeParser::smilies_location(). "/smile.gif'> ", // :)
    155                 '#(?<!\w):-\)(?!\w)#i'        => " <img src='".BBCodeParser::smilies_location(). "/smile.gif'> ", // :-)
    156                 '#(?<!\w):\((?!\w)#i'        => " <img src='".BBCodeParser::smilies_location(). "/sad.gif'> ", // :(
    157                 '#(?<!\w):-\((?!\w)#i'        => " <img src='".BBCodeParser::smilies_location(). "/sad.gif'> ", // :-(
    158                 '#(?<!\w):p(?!\w)#i'         => " <img src='".BBCodeParser::smilies_location(). "/tongue.gif'> ", // :p
    159                 '#(?<!\w)8-\)(?!\w)#i'     => " <img src='".BBCodeParser::smilies_location(). "/cool.gif'> ", // 8-)
    160                 '#(?<!\w):\^\)(?!\w)#i' => " <img src='".BBCodeParser::smilies_location(). "/confused.gif'> " // :^)
    161             );
    162             $this->content = preg_replace(array_keys($smilies), array_values($smilies), $this->content);
    163         }
    164         return $this->content;
    165     }
    166
    167 }
    168 ?>

[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the Silverstripe Forums to ask development related questions. -