"Fossies" - the Fresh Open Source Software Archive

Member "highlight-3.57-x64/extras/web_plugins/dokuwiki/syntax.php" (12 May 2020, 7222 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: André  Simon
    7 Version: 1.2
    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 /*
   28 == Description ==
   29 
   30 The highlight utility converts source code of 120 programming languages to HTML 
   31 with syntax highlighting. This plugin pipes the content of <highlight>-Tags associated
   32 with a lang parameter to highlight, and returns the output code which is included
   33 in the Wiki page.
   34 
   35 Usage:
   36 
   37 Paste the following in the edit section of the wiki editing form:
   38 
   39 <highlight cpp>#include <stdio.h>
   40 
   41 int main (void){
   42   printf("This is some random code");
   43   return 0;
   44 }</highlight>
   45 
   46 Use the lang parameter to define the programming language (c, php, py, xml, etc).
   47 See the highlight documentation to learn all possible languages.
   48 See the syntax.php file for some formatting options (line numbering, code
   49 indentation, line wrapping etc).
   50 
   51 == Installation ==
   52 
   53 1. Install highlight (www.andre-simon.de) on your host
   54 2. Unzip the dokuwiki_highlight.zip file and upload the content to the 
   55    `lib/plugins/` directory
   56  */
   57  
   58 if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
   59 if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
   60 require_once(DOKU_PLUGIN.'syntax.php');
   61  
   62 class syntax_plugin_highlight extends DokuWiki_Syntax_Plugin {
   63 
   64     function getInfo(){
   65         return array(
   66             'author' => 'Andre Simon',
   67             'email'  => 'andre.simon1@gmx.de',
   68             'date'   => '2007-05-29',
   69             'name'   => 'Highlight Plugin',
   70             'desc'   => 'Plugin which uses the highlight utility (http://www.andre-simon.de) instead of Geshi for source code highlighting',
   71             'url'    => 'http://wiki.splitbrain.org/wiki:plugins',
   72         );
   73     }
   74  
   75     /**
   76      * What kind of syntax are we?
   77      */
   78     function getType(){
   79         return 'formatting';
   80     }
   81  
   82     /**
   83      * What kind of syntax do we allow (optional)
   84      */
   85 //    function getAllowedTypes() {
   86 //        return array();
   87 //    }
   88  
   89     /**
   90      * What about paragraphs? (optional)
   91      */
   92     function getPType(){
   93       return 'block';
   94     }
   95  
   96     /**
   97      * Where to sort in?
   98      */ 
   99     function getSort(){
  100         return 199;
  101     }
  102  
  103  
  104     /**
  105      * Connect pattern to lexer
  106      */
  107     function connectTo($mode) {
  108       $this->Lexer->addEntryPattern('<highlight(?=[^\r\n]*?>.*?</highlight>)',$mode,'plugin_highlight');
  109     }
  110  
  111     function postConnect() {
  112       $this->Lexer->addExitPattern('</highlight>','plugin_highlight');
  113     }
  114 
  115     /**
  116      * Handle the match
  117      */
  118     function handle($match, $state, $pos, &$handler){
  119 
  120         switch ($state) {
  121           case DOKU_LEXER_ENTER :
  122         break;
  123           case DOKU_LEXER_UNMATCHED : 
  124              list($lang, $content) = preg_split('/>/u',$match,2);
  125              $lang = trim($lang);
  126              return array($state, $lang, $content);
  127 
  128           case DOKU_LEXER_EXIT :
  129               break;
  130         }
  131         return array();
  132     }
  133  
  134     /**
  135      * Create output
  136      */
  137     function render($mode, &$renderer, $data) {
  138     // Highlight options:
  139     $hl_options=array();
  140     $hl_options['hl_binary']='/highlight';  // path to the highlight binary
  141     $hl_options['line-numbers']=false;
  142     $hl_options['theme']='edit-kwrite';       // set color theme
  143     $hl_options['linenumber-start']=1;
  144     $hl_options['linenumber-zeroes']=1;  // set to 1 if line numbers should be padded with zeros
  145     $hl_options['linenumber-length']=2;
  146     $hl_options['reformat-style']="gnu"; // possible values for C, C++ and Java Code: ansi, gnu, java, kr, linux
  147     $hl_options['replace-tabs']=4;       //number of spaces which replace a tab
  148     $hl_options['wrap-style']=2;         //0 -> disable, 1-> wrap, 2-> wrap and indent function names
  149     $hl_options['wrap-line-length']=80;  // if wrap-style <>0, this defines the line length before wrapping takes place
  150 
  151 
  152        if($mode == 'xhtml'){
  153             list($state, $lang, $input_code) = $data;
  154             $lang = preg_replace("'[^a-zA-Z]'","",$lang);
  155             switch ($state) {
  156               case DOKU_LEXER_ENTER :
  157             break;
  158  
  159               case DOKU_LEXER_UNMATCHED : // $renderer->doc .= $renderer->_xmlEntities($match); break;
  160         
  161             $search = array("&amp;","&quot;", "&lt;", "&gt;","&#92;","&#39;","&nbsp;");
  162             $replace = array("&","\"", "<", ">","\\","\'", " ");
  163             $input_code = str_replace($search, $replace, $input_code);
  164     
  165             $descriptorspec = array(
  166             0 => array("pipe", "r"),  // stdin is a pipe that the child will read from
  167             1 => array("pipe", "w")  // stdout is a pipe that the child will write to
  168             );
  169             
  170             $hl_cmd_str = $hl_options['hl_binary'];
  171             $hl_cmd_str .= ' --inline-css -f ';
  172             
  173             if ($hl_options['line-numbers']){
  174                 $hl_cmd_str .= " -l -m ";
  175                 $hl_cmd_str .= $hl_options['linenumber-start'];
  176                 if ($hl_options['linenumber-zeroes']){
  177                     $hl_cmd_str .= " -z -j ";
  178                     $hl_cmd_str .= $hl_options['linenumber-length'];
  179                 }
  180             }
  181             
  182             if ($hl_options['replace-tabs']) {
  183                 $hl_cmd_str .= " -t ";
  184                 $hl_cmd_str .= $hl_options['replace-tabs'];
  185             }
  186             
  187             if ($hl_options['wrap-style']){
  188                 $hl_cmd_str .= ($hl_options['wrap-style'] == 1)? ' -V ':' -W ';
  189                 $hl_cmd_str .= " -J ";
  190                 $hl_cmd_str .= $hl_options['wrap-line-length'];
  191             }
  192             
  193             if ($hl_options['reformat-style']){
  194                 $hl_cmd_str .= " -F ";
  195                 $hl_cmd_str .= $hl_options['reformat-style'];
  196             }
  197             
  198             if ($hl_options['theme']){
  199                     $hl_cmd_str .= " -s ";
  200                     $hl_cmd_str .= $hl_options['theme'];
  201             }
  202 
  203             $hl_cmd_str .= " -S $lang ";
  204     
  205             $process = proc_open($hl_cmd_str, $descriptorspec, $pipes);
  206     
  207             if (is_resource($process)) {
  208     
  209                 fwrite($pipes[0], $input_code);
  210                 fclose($pipes[0]);
  211     
  212                 $output = stream_get_contents($pipes[1]);
  213                 fclose($pipes[1]);
  214     
  215                 // It is important that you close any pipes before calling
  216                 // proc_close in order to avoid a deadlock
  217                 proc_close($process);
  218             }
  219             if (!strlen($output)) {
  220                 $renderer->doc .= "<small>ERROR: Execution of highlight ($hl_cmd_str) failed or missing input. Check binary  path.</small>"; 
  221                 $renderer->doc .= "<pre style=\"font-size:9pt;\">";
  222                 $renderer->doc .= $renderer->_xmlEntities($input_code); 
  223                 $renderer->doc .= "</pre>"; 
  224                 
  225             } else {
  226                 $renderer->doc .= "<pre style=\"font-size:9pt;\">";
  227                 $renderer->doc .= $output;
  228                 $renderer->doc .= "</pre>"; 
  229             }
  230 
  231             //$renderer->doc .= "<br>cmd=".$hl_cmd_str;
  232 
  233             break;
  234               case DOKU_LEXER_EXIT :
  235             break;
  236             }
  237             return true;
  238         }
  239         return false;
  240    }
  241 }
  242