Example #1
0
 /**
  * Perform code highlightning
  */
 function PerformHighlightCodeBlock($text, $opts, $content, $suffix = '', $before = '', $after = '')
 {
     // Parse options
     $options = CodeColorerOptions::ParseOptions($opts, $suffix);
     // Load code from a file
     if (isset($options['file'])) {
         $uploadPath = wp_upload_dir();
         $baseDir = realpath($uploadPath['basedir']);
         $filePath = realpath(path_join($baseDir, $options['file']));
         # Security check: do not allow to display arbitrary files, only the ones from
         # uploads folder.
         if (false === $filePath || 0 !== strncmp($baseDir, $filePath, strlen($baseDir)) || !is_file($filePath)) {
             $text = 'Specified file is not in uploads folder, does not exists, or not a file.';
             $options['lang'] = 'text';
         } else {
             $text = file_get_contents($filePath);
         }
     }
     // Preprocess source text
     $text = str_replace(array("\\\"", "\\\\'"), array("\"", "\\'"), $text);
     $text = preg_replace('/(< \\?php)/i', '<?php', $text);
     $text = preg_replace('/(?:^(?:\\s*[\\r\\n])+|\\s+$)/', '', $text);
     if ($options['escaped']) {
         $text = html_entity_decode($text, ENT_QUOTES);
         $text = preg_replace('~&#x0*([0-9a-f]+);~ei', 'chr(hexdec("\\1"))', $text);
         $text = preg_replace('~&#0*([0-9]+);~e', 'chr(\\1)', $text);
     }
     $result = '';
     // Check if CodeColorer has been disabled for this particular block
     if (!$options['enabled']) {
         $result = '<code>' . $text . '</code>';
     } else {
         // See if we should force a height
         $num_lines = count(explode("\n", $text));
         $result = $this->PerformHighlightGeshi($text, $options);
         $result = $this->AddContainer($result, $options, $num_lines);
     }
     if ($options['inline']) {
         $blockID = $this->GetBlockID($content, false, '<span>', '</span>');
     } else {
         $blockID = $this->GetBlockID($content);
     }
     $this->blocks[$blockID] = $result;
     if ($options['inline']) {
         $result = $before . $blockID . $after;
     } else {
         $result = "\n\n{$blockID}\n\n";
     }
     return $result;
 }
Example #2
0
 /**
  * Perform code highlightning
  */
 function PerformHighlightCodeBlock($text, $opts, $content, $suffix = '', $before = '', $after = '')
 {
     // Preprocess source text
     $text = str_replace(array("\\\"", "\\\\'"), array("\"", "\\'"), $text);
     $text = preg_replace('/(< \\?php)/i', '<?php', $text);
     $text = preg_replace('/(?:^(?:\\s*[\\r\\n])+|\\s+$)/', '', $text);
     // Parse options
     $options = CodeColorerOptions::ParseOptions($opts, $suffix);
     if ($options['escaped']) {
         $text = html_entity_decode($text, ENT_QUOTES);
         $text = preg_replace('~&#x0*([0-9a-f]+);~ei', 'chr(hexdec("\\1"))', $text);
         $text = preg_replace('~&#0*([0-9]+);~e', 'chr(\\1)', $text);
     }
     $result = '';
     // Check if CodeColorer has been disabled for this particular block
     if (!$options['enabled']) {
         $result = '<code>' . $text . '</code>';
     } else {
         // See if we should force a height
         $num_lines = count(explode("\n", $text));
         $result = $this->PerformHighlightGeshi($text, $options);
         $result = $this->AddContainer($result, $options, $num_lines);
     }
     if ($options['inline']) {
         $blockID = $this->GetBlockID($content, false, '<span>', '</span>');
     } else {
         $blockID = $this->GetBlockID($content);
     }
     $this->blocks[$blockID] = $result;
     if ($options['inline']) {
         $result = $before . $blockID . $after;
     } else {
         $result = "\n\n{$blockID}\n\n";
     }
     return $result;
 }