"Fossies" - the Fresh Open Source Software Archive

Member "highlight-3.57-x64/extras/web_plugins/wordpress/highlight.php" (12 May 2020, 4343 Bytes) of package /windows/www/highlight-3.57-x64.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 <?php
    2 /*
    3 Plugin Name: Highlight
    4 Plugin URI: http://www.andre-simon.de
    5 Description: Plugin which uses the highlight utility to generate coloured source code.
    6 Author: Andre Simon
    7 Version: 1.3
    8 Author URI: http://www.andre-simon.de
    9 
   10 
   11 This file is part of Highlight.
   12 
   13 Highlight is free software: you can redistribute it and/or modify
   14 it under the terms of the GNU General Public License as published by
   15 the Free Software Foundation, either version 3 of the License, or
   16 (at your option) any later version.
   17 
   18 Highlight is distributed in the hope that it will be useful,
   19 but WITHOUT ANY WARRANTY; without even the implied warranty of
   20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
   21 GNU General Public License for more details.
   22 
   23 You should have received a copy of the GNU General Public License
   24 along with Highlight.  If not, see <http://www.gnu.org/licenses/>.
   25 */
   26 
   27 // Highlight options:
   28 
   29 $hl_options=array();
   30 $hl_options['hl_binary']='highlight';  // path to the highlight binary
   31 $hl_options['theme']='edit-kwrite';         // set color theme
   32 $hl_options['line-numbers']=false;
   33 $hl_options['linenumber-start']=1;
   34 $hl_options['linenumber-zeroes']=1;  // set to 1 if line numbers should be padded with zeros
   35 $hl_options['linenumber-length']=2;
   36 $hl_options['reformat-style']="gnu"; // possible values for C, C++ and Java Code: ansi, gnu, java, kr, linux
   37 $hl_options['replace-tabs']=4;       // number of spaces which replace a tab
   38 $hl_options['wrap-style']=0;         // 0 -> disable, 1-> wrap, 2-> wrap and indent function names
   39 $hl_options['wrap-line-length']=60;  // if wrap-style <>0, this defines the line length before wrapping takes place
   40 
   41 class as_highlight {
   42     
   43     var $ch_is_excerpt = false;
   44     function __construct()
   45     {
   46                 openlog("myScripLog", LOG_PID | LOG_PERROR, LOG_LOCAL0);
   47         add_filter('the_content',array(&$this, 'hl_the_content_filter'),1);
   48     }
   49     // PHP 4 Constructor
   50     function as_highlight ()
   51     {
   52         $this->__construct() ;
   53     }
   54     
   55     function as_highlight_code($matches){
   56     
   57         global $hl_options;
   58         
   59         $lang = preg_replace("'[^a-zA-Z\-]'","",$matches[1]);
   60         // undo nl and p formatting
   61         $input_code = $matches[2];
   62         $input_code = preg_replace("'<br\s*\/?>\r?\n?'","\n",$input_code);
   63         $search = array("&amp;","&quot;", "&lt;", "&gt;","&#92;","&#39;","&nbsp;");
   64         $replace = array("&","\"", "<", ">","\\","\'", " ");
   65         $input_code = str_replace($search, $replace, $input_code);
   66         
   67         $descriptorspec = array(
   68         0 => array("pipe", "r"),  // stdin is a pipe that the child will read from
   69         1 => array("pipe", "w")  // stdout is a pipe that the child will write to
   70         );
   71         
   72         $hl_cmd_str = $hl_options['hl_binary'];
   73         $hl_cmd_str .= ' --inline-css -I -f ';
   74         
   75         if ($hl_options['line-numbers']){
   76             $hl_cmd_str .= " -l -m ";
   77             $hl_cmd_str .= $hl_options['linenumber-start'];
   78             if ($hl_options['linenumber-zeroes']){
   79                 $hl_cmd_str .= " -z -j ";
   80                 $hl_cmd_str .= $hl_options['linenumber-length'];
   81             }
   82         }
   83         
   84         if ($hl_options['replace-tabs']) {
   85             $hl_cmd_str .= " -t ";
   86             $hl_cmd_str .= $hl_options['replace-tabs'];
   87         }
   88         
   89         if ($hl_options['wrap-style']){
   90             $hl_cmd_str .= ($hl_options['wrap-style'] == 1)? ' -V ':' -W ';
   91             $hl_cmd_str .= " -J ";
   92             $hl_cmd_str .= $hl_options['wrap-line-length'];
   93         }
   94         
   95         if ($hl_options['reformat-style']){
   96             $hl_cmd_str .= " -F ";
   97             $hl_cmd_str .= $hl_options['reformat-style'];
   98         }
   99 
  100         if ($hl_options['theme']){
  101             $hl_cmd_str .= " -s ";
  102             $hl_cmd_str .= $hl_options['theme'];
  103         }
  104         
  105         $hl_cmd_str .= " -S $lang ";
  106     
  107         $process = proc_open($hl_cmd_str, $descriptorspec, $pipes);
  108 
  109 
  110         if (is_resource($process)) {
  111 
  112             fwrite($pipes[0], $input_code);
  113             fclose($pipes[0]);
  114             if (function_exists("stream_get_contents"))
  115             {
  116                 $output = $output . stream_get_contents($pipes[1]);
  117             }
  118             else
  119             {
  120                 $output = "";
  121                 while (!feof($pipes[1])) $output .= fgets($pipes[1]);
  122             }
  123             fclose($pipes[1]);
  124 
  125             // It is important that you close any pipes before calling
  126             // proc_close in order to avoid a deadlock
  127             $return_value = proc_close($process);
  128         }
  129         $newContent = "<pre>". $output ."</pre>";
  130         return $newContent;
  131     }
  132     
  133     function hl_the_content_filter($content) {
  134 
  135             return preg_replace_callback("/<pre\s+lang=\"(.*?)\">(.*)<\/pre>/siU",
  136                             array(&$this, "as_highlight_code"),
  137                             $content);
  138     }
  139 }
  140 
  141 if (!function_exists('as_highlight'))
  142     $as_highlight = new as_highlight();
  143 
  144 ?>