4 * Copyright 2004 $ThePhpWikiProgrammingTeam
6 * This file is part of PhpWiki.
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.
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.
18 * You should have received a copy of the GNU General Public License along
19 * with PhpWiki; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 * The SyntaxHighlighter plugin passes all its arguments through a C++
25 * highlighter called "highlight" (available at http://www.andre-simon.de/).
29 * syntax: See http://www.andre-simon.de/doku/highlight/highlight.html
30 * style = ["ansi", "gnu", "kr", "java", "linux"]
32 <<SyntaxHighlighter syntax=c style=kr color=emacs
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.
45 support options: syntax, style, color.
47 HIGHLIGHT_DATA_DIR, HIGHLIGHT_EXE
49 if (!defined('HIGHLIGHT_EXE'))
50 define('HIGHLIGHT_EXE', 'highlight');
51 //define('HIGHLIGHT_EXE','/usr/local/bin/highlight');
52 //define('HIGHLIGHT_EXE','/home/groups/p/ph/phpwiki/bin/highlight');
54 // highlight requires two subdirs themes and langDefs somewhere.
55 // Best by highlight.conf in $HOME, but the webserver user usually
56 // doesn't have a $HOME
57 if (!defined('HIGHLIGHT_DATA_DIR'))
59 define('HIGHLIGHT_DATA_DIR', 'f:\cygnus\usr\local\share\highlight');
61 define('HIGHLIGHT_DATA_DIR', '/usr/share/highlight');
62 //define('HIGHLIGHT_DATA_DIR','/home/groups/p/ph/phpwiki/share/highlight');
64 class WikiPlugin_SyntaxHighlighter
69 return _("SyntaxHighlighter");
72 function getDescription()
74 return _("Source code syntax highlighter (via http://www.andre-simon.de)");
77 function managesValidators()
82 function getDefaultArguments()
85 'syntax' => null, // required argument
86 'style' => null, // optional argument ["ansi", "gnu", "kr", "java", "linux"]
87 'color' => null, // optional, see highlight/themes
93 function handle_plugin_args_cruft(&$argstr, &$args)
95 $this->source = $argstr;
98 function newFilterThroughCmd($input, $commandLine)
100 $descriptorspec = array(
101 0 => array("pipe", "r"), // stdin is a pipe that the child will read from
102 1 => array("pipe", "w"), // stdout is a pipe that the child will write to
103 2 => array("pipe", "w"), // stdout is a pipe that the child will write to
106 $process = proc_open("$commandLine", $descriptorspec, $pipes);
107 if (is_resource($process)) {
108 // $pipes now looks like this:
109 // 0 => writeable handle connected to child stdin
110 // 1 => readable handle connected to child stdout
111 // 2 => readable handle connected to child stderr
112 fwrite($pipes[0], $input);
115 while (!feof($pipes[1])) {
116 $buf .= fgets($pipes[1], 1024);
120 while (!feof($pipes[2])) {
121 $stderr .= fgets($pipes[2], 1024);
124 // It is important that you close any pipes before calling
125 // proc_close in order to avoid a deadlock
126 $return_value = proc_close($process);
127 if (empty($buf)) printXML($this->error($stderr));
132 function run($dbi, $argstr, &$request, $basepage)
134 extract($this->getArgs($argstr, $request));
135 $source =& $this->source;
136 if (empty($syntax)) {
137 return $this->error(sprintf(_("A required argument '%s' is missing."), 'syntax'));
139 if (!empty($source)) {
141 if (defined('HIGHLIGHT_DATA_DIR'))
142 $args .= " --data-dir " . HIGHLIGHT_DATA_DIR;
143 if ($number != 0) $args .= " -l";
144 if ($wrap != 0) $args .= " -V";
146 if (!empty($color) and !preg_match('/^[\w-]+$/', $color)) {
147 $html->pushContent($this->error(fmt("invalid %s ignored", 'color')));
150 if (!empty($color)) $args .= " --style $color --inline-css";
151 if (!empty($style)) $args .= " -F $style";
152 $commandLine = HIGHLIGHT_EXE . "$args -q -X -f -S $syntax";
153 $code = $this->newFilterThroughCmd($source, $commandLine);
155 return $this->error(fmt("Couldn't start commandline '%s'", $commandLine));
156 $pre = HTML::pre(HTML::raw($code));
157 $html->pushContent($pre);
160 return $this->error(fmt("empty source"));
171 // c-hanging-comment-ender-p: nil
172 // indent-tabs-mode: nil