示例#1
0
 /**
  * User handler for code block.
  *
  * @param  TexyHandlerInvocation  handler invocation
  * @param  string  block type
  * @param  string  text to highlight
  * @param  string  language
  * @param  TexyModifier modifier
  * @return TexyHtml
  */
 public static function blockHandler($invocation, $blocktype, $content, $lang, $modifier)
 {
     if (preg_match('#^block/(php|neon|javascript|js|css|html|htmlcb|latte)$#', $blocktype)) {
         list(, $lang) = explode('/', $blocktype);
     } elseif ($blocktype !== 'block/code') {
         return $invocation->proceed();
     }
     $lang = strtolower($lang);
     if ($lang === 'htmlcb' || $lang === 'latte') {
         $lang = 'html';
     } elseif ($lang === 'javascript') {
         $lang = 'js';
     }
     if ($lang === 'html') {
         $langClass = 'FSHL\\Lexer\\LatteHtml';
     } elseif ($lang === 'js') {
         $langClass = 'FSHL\\Lexer\\LatteJavascript';
     } else {
         $langClass = 'FSHL\\Lexer\\' . ucfirst($lang);
     }
     $texy = $invocation->getTexy();
     $content = Texy::outdent($content);
     if (class_exists($langClass)) {
         $fshl = new FSHL\Highlighter(new FSHL\Output\Html(), FSHL\Highlighter::OPTION_TAB_INDENT);
         $content = $fshl->highlight($content, new $langClass());
     } else {
         $content = htmlSpecialChars($content);
     }
     $content = $texy->protect($content, Texy::CONTENT_BLOCK);
     $elPre = TexyHtml::el('pre');
     if ($modifier) {
         $modifier->decorate($texy, $elPre);
     }
     $elPre->attrs['class'] = 'src-' . strtolower($lang);
     $elCode = $elPre->create('code', $content);
     return $elPre;
 }
示例#2
0
 public function blockHandler($invocation, $blocktype, $content, $lang, $modifier)
 {
     if ($blocktype !== 'block/code') {
         return $invocation->proceed();
         //vstup se nebude zpracovavat
     }
     $highlighter = new \FSHL\Highlighter(new \FSHL\Output\HtmlManual(), \FSHL\Highlighter::OPTION_TAB_INDENT);
     $texy = $invocation->getTexy();
     $content = \Texy::outdent($content);
     //Set correct lexer:
     switch (strtoupper($lang)) {
         case 'C':
         case 'CPP':
             $lexer = new \FSHL\Lexer\Cpp();
             break;
         case 'CSS':
             $lexer = new \FSHL\Lexer\Css();
             break;
         case 'HTML':
             $lexer = new \FSHL\Lexer\Html();
             break;
             //HtmlOnly lexer
         //HtmlOnly lexer
         case 'JAVA':
             $lexer = new \FSHL\Lexer\Java();
             break;
         case 'JS':
         case 'JAVASCRIPT':
             $lexer = new \FSHL\Lexer\Javascript();
             break;
         case 'NEON':
             $lexer = new \FSHL\Lexer\Neon();
             break;
         case 'PHP':
             $lexer = new \FSHL\Lexer\Php();
             break;
         case 'PYTHON':
             $lexer = new \FSHL\Lexer\Python();
             break;
         case 'SQL':
             $lexer = new \FSHL\Lexer\Sql();
             break;
         case 'TEX':
             $lexer = new \FSHL\Lexer\Tex();
             break;
         case 'TEXY':
             $lexer = new \FSHL\Lexer\Texy();
             break;
         default:
             $lexer = new \FSHL\Lexer\Minimal();
     }
     $content = $highlighter->highlight($content, $lexer);
     $content = $texy->protect($content, \Texy::CONTENT_BLOCK);
     $elPre = \TexyHtml::el('pre');
     if ($modifier) {
         $modifier->decorate($texy, $elPre);
     }
     $elPre->attrs['class'] = strtolower($lang);
     $elPre->create('code', $content);
     return $elPre;
 }