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 if (!defined('HIGHLIGHT_EXE'))
30 define('HIGHLIGHT_EXE', 'highlight');
31 // highlight requires two subdirs themes and langDefs somewhere.
32 // Best by highlight.conf in $HOME, but the webserver user usually
33 // doesn't have a $HOME
34 if (!defined('HIGHLIGHT_DATA_DIR'))
36 define('HIGHLIGHT_DATA_DIR', 'f:\cygnus\usr\local\share\highlight');
38 define('HIGHLIGHT_DATA_DIR', '/usr/share/highlight');
40 class WikiPlugin_SyntaxHighlighter
45 return _("SyntaxHighlighter");
48 function getDescription()
50 return _("Source code syntax highlighter (via http://www.andre-simon.de).");
53 function managesValidators()
58 function getDefaultArguments()
61 'syntax' => null, // required argument
62 'style' => null, // optional argument ["ansi", "gnu", "kr", "java", "linux"]
63 'color' => null, // optional, see highlight/themes
69 function handle_plugin_args_cruft(&$argstr, &$args)
71 $this->source = $argstr;
74 function newFilterThroughCmd($input, $commandLine)
76 $descriptorspec = array(
77 0 => array("pipe", "r"), // stdin is a pipe that the child will read from
78 1 => array("pipe", "w"), // stdout is a pipe that the child will write to
79 2 => array("pipe", "w"), // stdout is a pipe that the child will write to
82 $process = proc_open("$commandLine", $descriptorspec, $pipes);
83 if (is_resource($process)) {
84 // $pipes now looks like this:
85 // 0 => writeable handle connected to child stdin
86 // 1 => readable handle connected to child stdout
87 // 2 => readable handle connected to child stderr
88 fwrite($pipes[0], $input);
91 while (!feof($pipes[1])) {
92 $buf .= fgets($pipes[1], 1024);
96 while (!feof($pipes[2])) {
97 $stderr .= fgets($pipes[2], 1024);
100 // It is important that you close any pipes before calling
101 // proc_close in order to avoid a deadlock
102 proc_close($process);
104 printXML($this->error($stderr));
111 function run($dbi, $argstr, &$request, $basepage)
113 extract($this->getArgs($argstr, $request));
114 $source =& $this->source;
115 if (empty($syntax)) {
116 return $this->error(sprintf(_("A required argument ā%sā is missing."), 'syntax'));
118 if (empty($source)) {
119 return HTML::div(array('class' => "error"),
120 "Please provide source code to SyntaxHighlighter plugin");
123 if (defined('HIGHLIGHT_DATA_DIR')) {
124 $args .= " --data-dir " . HIGHLIGHT_DATA_DIR;
133 if (!empty($color) and !preg_match('/^[\w-]+$/', $color)) {
134 $html->pushContent($this->error(fmt("invalid %s ignored", 'color')));
137 if (!empty($color)) {
138 $args .= " --style $color --inline-css";
140 if (!empty($style)) {
141 $args .= " -F $style";
143 $commandLine = HIGHLIGHT_EXE . "$args -q -X -f -S $syntax";
144 $code = $this->newFilterThroughCmd($source, $commandLine);
146 return $this->error(fmt("Couldn't start commandline ā%sā", $commandLine));
148 $pre = HTML::pre(HTML::raw($code));
149 $html->pushContent($pre);
158 // c-hanging-comment-ender-p: nil
159 // indent-tabs-mode: nil