]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/SyntaxHighlighter.php
Alec Thomas original plugin, which comes with highlight http://www.andre-simon.de/,
[SourceForge/phpwiki.git] / lib / plugin / SyntaxHighlighter.php
1 <?php // -*-php-*-
2 rcs_id('$Id: SyntaxHighlighter.php,v 1.1 2004-05-14 14:55:52 rurban Exp $');
3 /**
4  Copyright 2004 $ThePhpWikiProgrammingTeam
5
6  This file is part of PhpWiki.
7
8  PhpWiki is free software; you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation; either version 2 of the License, or
11  (at your option) any later version.
12
13  PhpWiki is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  GNU General Public License for more details.
17
18  You should have received a copy of the GNU General Public License
19  along with PhpWiki; if not, write to the Free Software
20  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22
23 /**
24  * The SyntaxHighlighter plugin passes all its arguments through a C++ 
25  * highlighter called "highlight" (available at http://www.andre-simon.de/).
26  *
27  * @author: alecthomas
28  * 
29  * syntax: See http://www.andre-simon.de/doku/highlight/highlight.html
30  * style = ["ansi", "gnu", "kr", "java", "linux"]
31  
32 <?plugin SyntaxHighlighter syntax=c style=kr
33  #include <stdio.h>
34  
35  int main() {
36  printf("Lalala\n");
37  }
38 ?>
39
40  I did not use beautifier, because it used up more than 8M of memory on
41  my system and PHP killed it. I'm not sure whether this is a problem
42  with my integration, or with beautifier itself.
43
44 Fixes by Reini Urban:
45   support options: syntax, style, color.
46   php version switch
47   HIGHLIGHT_DATA_DIR, HIGHLIGHT_EXE
48 */
49
50 define('HIGHLIGHT_EXE','highlight');
51 //define('HIGHLIGHT_EXE','/usr/local/bin/highlight');
52
53 // highlight requires two subdirs themes and langDefs somewhere.
54 if (isWindows())
55     define('HIGHLIGHT_DATA_DIR','f:\cygnus\usr\local\share\highlight');
56 else  
57     define('HIGHLIGHT_DATA_DIR','/usr/local/share/highlight');
58
59 class WikiPlugin_SyntaxHighlighter
60 extends WikiPlugin
61 {
62     function getName () {
63         return _("SyntaxHighlighter");
64     }
65     function getDescription () {
66         return _("Source code syntax highlighter (via http://www.andre-simon.de)");
67     }
68     function managesValidators() {
69         return true;
70     }
71     function getVersion() {
72         return preg_replace("/[Revision: $]/", '',
73                             "\$Revision: 1.1 $");
74     }
75     function getDefaultArguments() {
76         return array(
77                      'syntax' => null,
78                      'style'  => null,
79                      'color'  => null,
80                      'number' => 0,
81                      'wrap'   => 0,
82                      );
83     }
84     function handle_plugin_args_cruft(&$argstr, &$args) {
85         $this->source = $argstr;
86     }
87
88     function newFilterThroughCmd($input, $commandLine) {
89         $descriptorspec = array(
90                0 => array("pipe", "r"),  // stdin is a pipe that the child will read from
91                1 => array("pipe", "w"),  // stdout is a pipe that the child will write to
92                2 => array("pipe", "w"),  // stdout is a pipe that the child will write to
93         );
94
95         $process = proc_open("$commandLine", $descriptorspec, $pipes);
96         if (is_resource($process)) {
97             // $pipes now looks like this:
98             // 0 => writeable handle connected to child stdin
99             // 1 => readable  handle connected to child stdout
100             // 2 => readable  handle connected to child stderr
101             fwrite($pipes[0], $input);
102             fclose($pipes[0]);
103             $buf = "";
104             while(!feof($pipes[1])) {
105                 $buf .= fgets($pipes[1], 1024);
106             }
107             fclose($pipes[1]);
108             $stderr = '';
109             while(!feof($pipes[2])) {
110                 $stderr .= fgets($pipes[2], 1024);
111             }
112             fclose($pipes[2]);
113             // It is important that you close any pipes before calling
114             // proc_close in order to avoid a deadlock
115             $return_value = proc_close($process);
116             if (empty($buf)) printXML($this->error($stderr));
117             return $buf;
118         }
119     }
120
121     /* PHP versions < 4.3
122      * TODO: via temp file looks more promising
123      */
124     function OldFilterThroughCmd($input, $commandLine) {
125          $input = str_replace ("\\", "\\\\", $input);
126          $input = str_replace ("\"", "\\\"", $input);
127          $input = str_replace ("\$", "\\\$", $input);
128          $input = str_replace ("`", "\`", $input);
129          $input = str_replace ("'", "\'", $input);
130          //$input = str_replace (";", "\;", $input);
131
132          $pipe = popen("echo \"$input\"|$commandLine", 'r');
133          if (!$pipe) {
134             print "pipe failed.";
135             return "";
136          }
137          $output = '';
138          while (!feof($pipe)) {
139             $output .= fread($pipe, 1024);
140          }
141          pclose($pipe);
142          return $output;
143     }
144
145     function run($dbi, $argstr, $request) {
146         extract($this->getArgs($argstr, $request));
147         $source =& $this->source;
148         if (empty($syntax)) return $this->error(_("Syntax language not specified)"));
149         if (!empty($source)) {
150             $args = "";
151             if (defined('HIGHLIGHT_DATA_DIR'))
152                 $args .= " --data-dir " . HIGHLIGHT_DATA_DIR;
153             if ($number != 0) $args .= " -l";
154             if ($wrap != 0)   $args .= " -V";
155             if (!empty($color)) $args .= " --style $color -c ".FindFile("uploads")."/highlight.css";
156             if (!empty($style)) $args .= " -F $style";
157             $commandLine = HIGHLIGHT_EXE . "$args -q -X -f -S $syntax";
158             if (check_php_version(4,3,0))
159                 $code = $this->newFilterThroughCmd($source, $commandLine);
160             else 
161                 $code = $this->oldFilterThroughCmd($source, $commandLine);
162             if (empty($code))
163                 return $this->error(fmt("Couldn't start commandline '%s'",$commandLine));
164             $html = HTML::pre(HTML::raw($code));
165
166             global $Theme;
167             $html->setAttr('class','tightenable top bottom');
168             $css = $Theme->_CSSlink('',empty($color) ? 'highlight.css' : 'uploads/highlight.css','');
169             return HTML($css,$html);
170         } else {
171             return $this->error(_("empty source"));
172         }
173     }
174 };
175
176 // $Log: not supported by cvs2svn $
177 //
178
179 // For emacs users
180 // Local Variables:
181 // mode: php
182 // tab-width: 8
183 // c-basic-offset: 4
184 // c-hanging-comment-ender-p: nil
185 // indent-tabs-mode: nil
186 // End:
187 ?>