Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 29th, 2012  |  syntax: None  |  size: 1.50 KB  |  hits: 17  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. <?php
  2.  
  3. namespace Test\CommentBundle\Markup;
  4.  
  5. use FOS\CommentBundle\Markup\ParserInterface;
  6. use HTMLPurifier;
  7.  
  8. class BBCode implements ParserInterface
  9. {
  10.     private $parser;
  11.     private $purifier;
  12.  
  13.     public function __construct(HtmlPurifier $purifier)
  14.     {
  15.         $this->purifier = $purifier;
  16.     }
  17.  
  18.     /**
  19.      * @return \StringParser_BBCode
  20.      */
  21.     protected function getParser()
  22.     {
  23.         if (null === $this->parser) {
  24.             $parser = new \StringParser_BBCode();
  25.             $parser->setRootParagraphHandling(true);
  26.  
  27.             /**
  28.              * Bold
  29.              *
  30.              * [b][/b] -> <b></b>
  31.              */
  32.             $parser->addCode('b', 'simple_replace', null, array(
  33.                 'start_tag' => '<b>',
  34.                 'end_tag' => '</b>'
  35.             ), 'inline', array('listitem', 'block', 'inline', 'link'), array());
  36.  
  37.             /**
  38.              * Italics
  39.              *
  40.              * [i][/i] -> <i></i>
  41.              */
  42.             $parser->addCode('i', 'simple_replace', null, array(
  43.                 'start_tag' => '<i>',
  44.                 'end_tag' => '</i>'
  45.             ), 'inline', array('listitem', 'block', 'inline', 'link'), array());
  46.  
  47.             $this->parser = $parser;
  48.         }
  49.  
  50.         return $this->parser;
  51.     }
  52.  
  53.     /**
  54.      * Takes a markup string and returns raw html.
  55.      *
  56.      * @param string $raw
  57.      *
  58.      * @return string
  59.      */
  60.     public function parse($raw)
  61.     {
  62.         $raw = $this->purifier->purify($raw);
  63.  
  64.         return $this->getParser()->parse($raw);
  65.     }
  66. }