"Fossies" - the Fresh Open Source Software Archive

Member "highlight-3.57-x64/extras/highlight_pipe.php" (12 May 2020, 4283 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 /**This PHP class serves as interface to the highlight utility.
    4 Input and output streams are handled with pipes.
    5 Command line parameters are validated before use.
    6 */
    7 
    8 class HighlightPipe {
    9 
   10   // alter these members to control highlight output
   11   // see manpage for the options
   12 
   13   var $hl_option = array(
   14   'hl_bin' => 'highlight',     // configure path of highlight binary
   15   'syntax' => 'c',
   16   'theme' => 'kwrite',
   17   'force' => 1,
   18   'line-numbers' => 0,
   19   'line-number-length' => 4,
   20   'line-number-start' => 0,
   21   'zeroes' => 0,
   22   'wraptype' => 0,
   23   'line-length' => 0,
   24   'reformat' => '',
   25   'kw-case' => '',
   26   'replace-tabs' => 0,
   27   'encoding' => '',
   28   'enclose-pre' => 1,
   29   'inline-css' => 1,
   30   'fragment' => 1,
   31   );
   32 
   33   // this member contains the input source code
   34   var $input='';
   35 
   36   // this member will contain the command string after getResult() was called
   37   var $hl_cmd_str='';
   38 
   39   function getInfo(){
   40         return array(
   41             'author' => 'Andre Simon',
   42             'email'  => 'andre.simon1@gmx.de',
   43             'date'   => '2015-10-13',
   44             'url'    => 'http://www.andre-simon.de/',
   45             'version'    => '1.2',
   46         );
   47   }
   48 
   49   // call this method to generate highlighted HTML code
   50   function getResult() {
   51 
   52         foreach ($this->hl_option as $key => $value) {
   53           $this->hl_option[$key] = $this->validate( $value );
   54         }
   55 
   56     $descriptorspec = array(
   57     0 => array("pipe", "r"),
   58     1 => array("pipe", "w")
   59     );
   60 
   61     $this->hl_cmd_str = $this->hl_option['hl_bin'];
   62 
   63     if ($this->hl_option['line-numbers']){
   64         $this->hl_cmd_str .= " -l -m 1";
   65         /*$this->hl_cmd_str .= $this->get_config('hl_linenumbersberstart');*/
   66         if ($this->hl_option['zeroes']){
   67             $this->hl_cmd_str .= " -z ";
   68         }
   69         if ($this->hl_option['line-number-length']!='0' && is_numeric($this->hl_option['line-number-length'])) {
   70             $this->hl_cmd_str .= ' -j ';
   71             $this->hl_cmd_str .=$this->hl_option['line-number-length'];
   72         }
   73     }
   74 
   75     if (is_numeric($this->hl_option['replace-tabs']) and $this->hl_option['replace-tabs']>0) {
   76         $this->hl_cmd_str .= " -t ";
   77                 $this->hl_cmd_str .= $this->hl_option['replace-tabs'];
   78     }
   79 
   80     if ($this->hl_option['wraptype']>0){
   81         $this->hl_cmd_str .= ($this->hl_option['wraptype'] == 1)? ' -V ':' -W ';
   82         if ($this->hl_option['line-length']>0 && is_numeric($this->hl_option['line-length'])) {
   83             $this->hl_cmd_str .= " -J ";
   84             $this->hl_cmd_str .= $this->hl_option['line-length'];
   85         }
   86     }
   87 
   88     if (strlen($this->hl_option['reformat'])>1){
   89         $this->hl_cmd_str .= " -F ";
   90         $this->hl_cmd_str .= $this->hl_option['reformat'];
   91     }
   92 
   93     if (strlen($this->hl_option['kw-case'])>1){
   94         $this->hl_cmd_str .= " --kw-case ";
   95         $this->hl_cmd_str .= $this->hl_option['kw-case'];
   96     }
   97 
   98     if ($this->hl_option['force']){
   99         $this->hl_cmd_str .= " --force ";
  100     }
  101 
  102     if ($this->hl_option['inline-css']){
  103         $this->hl_cmd_str .= " --inline-css ";
  104     }
  105 
  106     if ($this->hl_option['fragment']){
  107         $this->hl_cmd_str .= " -f ";
  108     }
  109 
  110     if ($this->hl_option['theme']){
  111         $this->hl_cmd_str .= " -s ";
  112         $this->hl_cmd_str .= $this->hl_option['theme'];
  113     }
  114 
  115     if ($this->hl_option['encoding']){
  116         $this->hl_cmd_str .= " -u ";
  117         $this->hl_cmd_str .= $this->hl_option['encoding'];
  118     }
  119 
  120     if ($this->hl_option['enclose-pre']){
  121         $this->hl_cmd_str .= " --enclose-pre ";
  122     }
  123 
  124     $this->hl_cmd_str .= " -S  ";
  125     $this->hl_cmd_str .= $this->hl_option['syntax'];
  126 
  127     $process = proc_open($this->hl_cmd_str, $descriptorspec, $pipes);
  128     if (is_resource($process)) {
  129 
  130         fwrite($pipes[0], $this->input);
  131         fclose($pipes[0]);
  132 
  133         $output = stream_get_contents($pipes[1]);
  134         fclose($pipes[1]);
  135 
  136         // It is important that you close any pipes before calling
  137         // proc_close in order to avoid a deadlock
  138         proc_close($process);
  139     }
  140         return $output;
  141     }
  142 
  143 
  144    // PRIVATE STUFF
  145    var $special = array(' ', '/','!','&','*','\\', '.', '|', 'ยด','\'', '<', '>');
  146 
  147    function validate($string) {
  148       return (strlen($string)>50)? "" : str_replace($this->special,"",$string);
  149    }
  150 
  151   }
  152 
  153 /*****************************************************************************/
  154 
  155 /*
  156 
  157 // Sample code:
  158 
  159   $generator = new HighlightPipe;
  160   $generator->input='int main () { return 0; }';
  161 
  162   $generator->hl_option['theme']='neon';
  163   $generator->hl_option['syntax']='c';
  164 
  165   $result= $generator->getResult();
  166 
  167   print $result;
  168 */
  169 
  170 ?>