"Fossies" - the Fresh Open Source Software Archive

Member "highlight-3.57-x64/extras/web_plugins/serendipity_event_highlight/serendipity_event_highlight.php" (12 May 2020, 13199 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. See also the last Fossies "Diffs" side-by-side code changes report for "serendipity_event_highlight.php": 3.53_vs_3.54.

    1 <?php
    2 /**
    3 
    4 serendipity_event_highlight.php
    5 
    6 === Highlight plugin ===
    7 
    8 This plugin uses highlight (http://www.andre-simon.de/) to add syntax highlighting to serendipity.
    9 
   10 == Description ==
   11 
   12 The highlight utility converts source code of 120 programming languages to HTML
   13 with syntax highlighting. This plugin pipes the content of [highlight] tags associated
   14 with a lang parameter to highlight, and returns the output code which is included
   15 in the Serendipity blog entry.
   16 The plugin configuration menu offers the most important options of the highlight utility.
   17 The highlighted code is formatted with inline CSS to avoid complicated plugin setup.
   18 
   19 Usage:
   20 
   21 Paste the following in the code section of the blog editing form:
   22 
   23 [highlight lang=c]#include <stdio.h>
   24 
   25 int main (void){
   26   printf("This is some random code");
   27   return 0;
   28 } [/highlight]
   29 
   30 Normally the file extension can be used for the lang parameter (like pl, py, java, pas etc).
   31 Execute 'highlight --list-langs' to get a list of all supported programming languages.
   32 
   33 == Installation ==
   34 
   35 1. Install the highlight utility on your host (www.andre-simon.de)
   36 2. Copy the serendipity_event_highlight dir into the serendipity plugin dir
   37 3. Activate and configure the plugin in the serendipity administration menu
   38 
   39    IMPORTANT:
   40    To avoid insertion of superfluous <br> Tags by the nl2br plugin, add the
   41    string "highlight" to the  "list of HTML-tags where no breaks shall be
   42    converted" in the nl2br configuration menu.
   43 
   44  */
   45 
   46 
   47 if (IN_serendipity !== true) {
   48     die ("Don't hack!");
   49 }
   50 
   51 // Probe for a language include with constants. Still include defines later on, if some constants were missing
   52 $probelang = dirname(__FILE__) . '/' . $serendipity['charset'] . 'lang_' . $serendipity['lang'] . '.inc.php';
   53 if (file_exists($probelang)) {
   54     include $probelang;
   55 }
   56 
   57 include dirname(__FILE__) . '/lang_en.inc.php';
   58 
   59 class serendipity_event_highlight extends serendipity_event
   60 {
   61     var $title = PLUGIN_EVENT_HIGHLIGHT_NAME;
   62     // Top Level Configuration, requires name of the Plugin, description text, and configuration information in an array..
   63     function introspect(&$propbag)
   64     {
   65         global $serendipity;
   66 
   67         $propbag->add('name',          PLUGIN_EVENT_HIGHLIGHT_NAME);
   68         $propbag->add('description',   PLUGIN_EVENT_HIGHLIGHT_DESC);
   69         $propbag->add('stackable',     false);
   70         $propbag->add('author',        'Andre Simon');
   71         $propbag->add('requirements',  array(
   72             'serendipity' => '0.9',
   73             'highlight'   => '3.23'
   74         ));
   75         $propbag->add('version',       '0.2');
   76         $propbag->add('event_hooks', array('frontend_display' => true, 'frontend_comment' => true));
   77         $propbag->add('groups', array('MARKUP'));
   78 
   79         $this->markup_elements = array(
   80             array(
   81               'name'     => 'ENTRY_BODY',
   82               'element'  => 'body',
   83             ),
   84             array(
   85               'name'     => 'EXTENDED_BODY',
   86               'element'  => 'extended',
   87             ),
   88             array(
   89               'name'     => 'COMMENT',
   90               'element'  => 'comment',
   91             ),
   92             array(
   93               'name'     => 'HTML_NUGGET',
   94               'element'  => 'html_nugget',
   95             )
   96         );
   97 
   98     #Colour themes of a highlight default installation
   99         $this->themes = array(
  100     "acid", "aiseered", "andes", "anotherdark", "autumn", "baycomb",
  101     "bclear", "biogoo", "bipolar", "blacknblue", "bluegreen", "breeze",
  102     "bright", "camo", "candy", "clarity", "dante", "darkblue",
  103     "darkbone", "darkness", "darkslategray", "darkspectrum", "denim",
  104     "dusk", "earendel", "easter", "edit-anjuta", "edit-eclipse",
  105     "edit-emacs", "edit-flashdevelop", "edit-gedit", "edit-jedit",
  106     "edit-kwrite", "edit-matlab", "edit-msvs2008", "edit-nedit",
  107     "edit-vim-dark", "edit-vim", "edit-xcode", "ekvoli", "fine_blue",
  108     "freya", "fruit", "golden", "greenlcd", "kellys", "leo",
  109     "lucretia", "manxome", "maroloccio", "matrix", "moe", "molokai",
  110     "moria", "navajo-night", "navy", "neon", "night", "nightshimmer",
  111     "nuvola", "olive", "orion", "oxygenated", "pablo", "peaksea",
  112     "print", "rand01", "rdark", "relaxedgreen", "rootwater",
  113     "seashell", "solarized-dark", "solarized-light", "tabula",
  114     "tcsoft", "the", "vampire", "whitengrey", "xoria256", "zellner",
  115     "zenburn", "zmrok"
  116         );
  117 
  118     #Reformatting schemes of a highlight default installation
  119         $this->reformatschemes = array ("disabled","allman", "ansi", "banner",
  120         "gnu", "google", "horstmann", "java", "kr", "linux", "lisp",
  121         "otbs", "pico", "stroustrup", "vtk", "whitesmith");
  122 
  123     #highlight output options
  124         $conf_array = array('hl_bin_dir','hl_data_dir','hl_linenumbers','hl_linenumberstart','hl_linenumberzeroes','hl_linenumberlen',
  125                             'hl_wrap','hl_wrap_len','hl_tab_len', 'hl_theme','hl_format');
  126 
  127         foreach($this->markup_elements as $element) {
  128             $conf_array[] = $element['name'];
  129         }
  130         $propbag->add('configuration', $conf_array);
  131     }
  132 
  133     function generate_content(&$title) {
  134         $title = $this->title;
  135     }
  136 
  137 
  138     function introspect_config_item($name, &$propbag) {
  139         switch ($name) {
  140             case 'hl_bin_dir' :
  141                 $propbag->add('name',        PLUGIN_EVENT_HIGHLIGHT_HLBINDIR);
  142                 $propbag->add('type', 'string');
  143                 $propbag->add('description', PLUGIN_EVENT_HIGHLIGHT_HLBINDIR_DESC);
  144                 $propbag->add('default', 'highlight');
  145                 break;
  146             case 'hl_data_dir' :
  147                 $propbag->add('name',        PLUGIN_EVENT_HIGHLIGHT_HLDATADIR);
  148                 $propbag->add('type', 'string');
  149                 $propbag->add('description', PLUGIN_EVENT_HIGHLIGHT_HLDATADIR_DESC);
  150                 $propbag->add('default', '/usr/share/highlight/');
  151                 break;
  152             case 'hl_linenumbers' :
  153                 $propbag->add('name',        PLUGIN_EVENT_HIGHLIGHT_LINENUMBERS);
  154                 $propbag->add('type', 'boolean');
  155                 $propbag->add('description', PLUGIN_EVENT_HIGHLIGHT_LINENUMBERS_DESC);
  156                 $propbag->add('default', 'false');
  157                 break;
  158             case 'hl_linenumberzeroes' :
  159                 $propbag->add('name',        PLUGIN_EVENT_HIGHLIGHT_LINENUMBERZEROES);
  160                 $propbag->add('type', 'boolean');
  161                 $propbag->add('description', PLUGIN_EVENT_HIGHLIGHT_LINENUMBERZEROES_DESC);
  162                 $propbag->add('default', 'false');
  163                 break;
  164             case 'hl_linenumberstart' :
  165                 $propbag->add('name',        PLUGIN_EVENT_HIGHLIGHT_LINENUMBERSTART);
  166                 $propbag->add('type', 'string');
  167                 $propbag->add('description', PLUGIN_EVENT_HIGHLIGHT_LINENUMBERSTART_DESC);
  168                 $propbag->add('default', '1');
  169         $propbag->add('validate', 'number');
  170                 break;
  171 
  172             case 'hl_linenumberlen' :
  173                 $propbag->add('name',        PLUGIN_EVENT_HIGHLIGHT_LINENUMBERLEN);
  174                 $propbag->add('type', 'string');
  175                 $propbag->add('description', PLUGIN_EVENT_HIGHLIGHT_LINENUMBERLEN_DESC);
  176                 $propbag->add('default', '2');
  177                 $propbag->add('validate', 'number');
  178                 break;
  179 
  180             case 'hl_theme' :
  181                 $propbag->add('type', 'select');
  182                 $propbag->add('name', PLUGIN_EVENT_HIGHLIGHT_THEMES);
  183                 $propbag->add('description', PLUGIN_EVENT_HIGHLIGHT_THEMES_DESC);
  184                 $propbag->add('select_values', $this->themes);
  185                 $propbag->add('default', '1');
  186                 break;
  187 
  188             case 'hl_format' :
  189                 $propbag->add('type', 'select');
  190                 $propbag->add('name', PLUGIN_EVENT_HIGHLIGHT_FORMAT);
  191                 $propbag->add('description', PLUGIN_EVENT_HIGHLIGHT_FORMAT_DESC);
  192                 $propbag->add('select_values', $this->reformatschemes);
  193                 $propbag->add('default', '0');
  194                 break;
  195 
  196             case 'hl_wrap' :
  197                 $wrapstyles=array ('No wrap', 'Simple wrap','Intelligent wrap');
  198                 $propbag->add('type', 'select');
  199                 $propbag->add('name', PLUGIN_EVENT_HIGHLIGHT_WRAP);
  200                 $propbag->add('description', PLUGIN_EVENT_HIGHLIGHT_WRAP_DESC);
  201                 $propbag->add('select_values', $wrapstyles);
  202                 $propbag->add('default', '0');
  203                 break;
  204             case 'hl_wrap_len' :
  205                 $propbag->add('name',        PLUGIN_EVENT_HIGHLIGHT_WRAPLEN);
  206                 $propbag->add('type', 'string');
  207                 $propbag->add('description', PLUGIN_EVENT_HIGHLIGHT_WRAPLEN_DESC);
  208                 $propbag->add('default', '60');
  209                 $propbag->add('validate', 'number');
  210                 break;
  211             case 'hl_tab_len' :
  212                 $propbag->add('name',        PLUGIN_EVENT_HIGHLIGHT_TABLEN);
  213                 $propbag->add('type', 'string');
  214                 $propbag->add('description', PLUGIN_EVENT_HIGHLIGHT_TABLEN_DESC);
  215                 $propbag->add('default', '4');
  216                 $propbag->add('validate', 'number');
  217                 break;
  218             default :
  219                 $propbag->add('name',        constant($name));
  220                 $propbag->add('type',        'boolean');
  221                 $propbag->add('default',     'true');
  222                 $propbag->add('description', sprintf(APPLY_MARKUP_TO, constant($name)));
  223         }
  224         return true;
  225     }
  226 
  227     function geshi($input) {
  228         $input = preg_replace_callback('/\[highlight(?:\s)*lang=([A-Za-z0-9_\-]+)(?:\s)*()?\](.*?)\[\/highlight\]/si', array(&$this, 'highlightcallback'), $input);
  229         return $input;
  230     }
  231 
  232     function event_hook($event, &$bag, &$eventData) {
  233         global $serendipity;
  234 
  235         $hooks = &$bag->get('event_hooks');
  236 
  237         if (isset($hooks[$event])) {
  238             switch($event) {
  239                 case 'frontend_display':
  240                     foreach ($this->markup_elements as $temp) {
  241                         if (serendipity_db_bool($this->get_config($temp['name'], true)) && isset($eventData[$temp['element']]) &&
  242                             !$eventData['properties']['ep_disable_markup_' . $this->instance] &&
  243                             !isset($serendipity['POST']['properties']['disable_markup_' . $this->instance])) {
  244                             $element = $temp['element'];
  245                             $eventData[$element] = $this->geshi($eventData[$element]);
  246                         }
  247                     }
  248                     return true;
  249                     break;
  250 
  251                 case 'frontend_comment':
  252                     if (serendipity_db_bool($this->get_config('COMMENT', true))) {
  253                         echo '<div class="serendipity_commentDirection serendipity_comment_geshi">' . PLUGIN_EVENT_HIGHLIGHT_TRANSFORM . '</div>';
  254                     }
  255                     return true;
  256                     break;
  257 
  258                 default:
  259                   return false;
  260             }
  261         } else {
  262             return false;
  263         }
  264     }
  265 
  266     function highlightcallback($matches) {
  267 
  268     $search = array("&amp;","&quot;", "&lt;", "&gt;","&#92;","&#39;","&nbsp;");
  269     $replace = array("&","\"", "<", ">","\\","\'", " ");
  270     $input_code = str_replace($search, $replace, $matches[3]);
  271 
  272     $descriptorspec = array(
  273     0 => array("pipe", "r"),  // stdin is a pipe that the child will read from
  274     1 => array("pipe", "w")  // stdout is a pipe that the child will write to
  275     );
  276 
  277     $hl_cmd_str =  $this->get_config('hl_bin_dir');
  278     $hl_cmd_str .= ' --inline-css -f ';
  279 
  280     if ( $this->get_config('hl_linenumbers')){
  281         $hl_cmd_str .= " -l -m ";
  282         $hl_cmd_str .= $this->get_config('hl_linenumberstart');
  283         if ($this->get_config('hl_linenumberzeroes')){
  284             $hl_cmd_str .= " -z ";
  285         }
  286         $hl_cmd_str .= ' -j ';
  287         $hl_cmd_str .= $this->get_config('hl_linenumberlen');
  288     }
  289 
  290     if ($this->get_config('hl_tab_len')) {
  291         $hl_cmd_str .= " -t ";
  292         $hl_cmd_str .= $this->get_config('hl_tab_len');
  293     }
  294 
  295     if ($this->get_config('hl_wrap')>0){
  296         $hl_cmd_str .= ($this->get_config('hl_wrap') == 1)? ' -V ':' -W ';
  297         $hl_cmd_str .= " -J ";
  298         $hl_cmd_str .= $this->get_config('hl_wrap_len');
  299     }
  300 
  301     if ($this->get_config('hl_format')>1){
  302         $hl_cmd_str .= " -F ";
  303         $hl_cmd_str .= $this->reformatschemes[$this->get_config('hl_format')];
  304     }
  305 
  306 
  307     $hl_cmd_str .= " -s ";
  308     $hl_cmd_str .= $this->themes[$this->get_config('hl_theme')];
  309 
  310 
  311     $lang = strtolower($matches[1]);
  312     $hl_cmd_str .= " -S $lang ";
  313 
  314     $process = proc_open($hl_cmd_str, $descriptorspec, $pipes);
  315     if (is_resource($process)) {
  316 
  317         fwrite($pipes[0], $matches[3]);
  318         fclose($pipes[0]);
  319 
  320         $output = stream_get_contents($pipes[1]);
  321         fclose($pipes[1]);
  322 
  323         // It is important that you close any pipes before calling
  324         // proc_close in order to avoid a deadlock
  325         proc_close($process);
  326     }
  327     $result='';
  328     if (!strlen($output)) {
  329         $result .= "<small>ERROR: Execution of highlight ($hl_cmd_str) failed or missing input. Check binary  path.</small>";
  330         $result .= "<pre style=\"font-size:9pt;\">";
  331         $result .= $input_code;
  332         $result .= "</pre>";
  333 
  334     } else {
  335         $result .= "<pre style=\"font-size:9pt;\">";
  336         $result .= $output;
  337         $result .= "</pre>";
  338     }
  339     return $result;
  340     }
  341 }
  342 
  343 ?>