public static function do_highlight($in)
 {
     // Look, ma! No Regex!
     $tokenizer = new HTMLTokenizer($in, false);
     $tokens = $tokenizer->parse();
     // fetch div, pre, code slices that have a class="highlight"
     $slices = $tokens->slice(array('div', 'pre', 'code'), array('class' => 'highlight'));
     // iterate the found slices
     foreach ($slices as $slice) {
         // store the class to use once we've stripped the container
         $classAttr = $slice[0]['attrs']['class'];
         // unique name to use in the cache for this slice/markup
         $sliceCacheName = 'plugin.highlight.' . md5((string) $slice) . filemtime(__FILE__);
         // trim off the div, and determine the value
         $slice->trim_container();
         $sliceValue = trim((string) $slice);
         // see if it's already been cached
         if (Cache::has($sliceCacheName)) {
             $output = Cache::get($sliceCacheName);
         } else {
             // trim off the CDATA wrapper, if applicable
             if (substr($sliceValue, 0, 9) == '<![CDATA[' && substr($sliceValue, -3) == ']]>') {
                 $sliceValue = substr($sliceValue, 9, -3);
             }
             $classes = array_filter(explode(' ', trim(str_replace('highlight', '', $classAttr))));
             // ugly, refactor
             $geshi = new Geshi(trim($sliceValue), isset($classes[0]) ? $classes[0] : 'php', HighlightPlugin::$geshi_path . '/geshi/');
             $geshi->set_header_type(GESHI_HEADER_PRE);
             $geshi->set_overall_class('geshicode');
             $output = @$geshi->parse_code();
             // @ is slow, but geshi is full of E_NOTICE
             Cache::set($sliceCacheName, $output);
         }
         $slice->tokenize_replace($output);
         $tokens->replace_slice($slice);
     }
     return (string) $tokens;
 }
示例#2
0
 /**
  * Helper function for overriding some GeSHi defaults.
  *
  * @param \Geshi $geshi
  *   Geshi object.
  * @param string $langcode
  *   The language.
  */
 public static function overrideGeshiDefaults(\Geshi &$geshi, $langcode)
 {
     $config = \Drupal::config('geshifilter.settings');
     // Override the some default GeSHi styles (e.g. GeSHi uses Courier by
     // default, which is ugly).
     $geshi->set_line_style('font-family: monospace; font-weight: normal;', 'font-family: monospace; font-weight: bold; font-style: italic;');
     $geshi->set_code_style('font-family: monospace; font-weight: normal; font-style: normal');
     // Overall class needed for CSS.
     $geshi->set_overall_class('geshifilter-' . $langcode);
     // Set keyword linking.
     $geshi->enable_keyword_links($config->get('enable_keyword_urls', TRUE));
 }