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
43 function getDescription()
45 return _("Source code syntax highlighter (via http://www.andre-simon.de).");
48 function managesValidators()
53 function getDefaultArguments()
56 'syntax' => null, // required argument
57 'style' => null, // optional argument ["ansi", "gnu", "kr", "java", "linux"]
58 'color' => null, // optional, see highlight/themes
64 function handle_plugin_args_cruft(&$argstr, &$args)
66 $this->source = $argstr;
69 function newFilterThroughCmd($input, $commandLine)
71 $descriptorspec = array(
72 0 => array("pipe", "r"), // stdin is a pipe that the child will read from
73 1 => array("pipe", "w"), // stdout is a pipe that the child will write to
74 2 => array("pipe", "w"), // stdout is a pipe that the child will write to
77 $process = proc_open("$commandLine", $descriptorspec, $pipes);
78 if (is_resource($process)) {
79 // $pipes now looks like this:
80 // 0 => writeable handle connected to child stdin
81 // 1 => readable handle connected to child stdout
82 // 2 => readable handle connected to child stderr
83 fwrite($pipes[0], $input);
86 while (!feof($pipes[1])) {
87 $buf .= fgets($pipes[1], 1024);
91 while (!feof($pipes[2])) {
92 $stderr .= fgets($pipes[2], 1024);
95 // It is important that you close any pipes before calling
96 // proc_close in order to avoid a deadlock
99 printXML($this->error($stderr));
106 function run($dbi, $argstr, &$request, $basepage)
108 extract($this->getArgs($argstr, $request));
109 $source =& $this->source;
110 if (empty($syntax)) {
111 return $this->error(sprintf(_("A required argument ā%sā is missing."), 'syntax'));
113 if (empty($source)) {
114 return HTML::div(array('class' => "error"),
115 "Please provide source code to SyntaxHighlighter plugin");
118 if (defined('HIGHLIGHT_DATA_DIR')) {
119 $args .= " --data-dir " . HIGHLIGHT_DATA_DIR;
128 if (!empty($color) and !preg_match('/^[\w-]+$/', $color)) {
129 $html->pushContent($this->error(fmt("invalid %s ignored", 'color')));
132 if (!empty($color)) {
133 $args .= " --style $color --inline-css";
135 if (!empty($style)) {
136 $args .= " -F $style";
138 $commandLine = HIGHLIGHT_EXE . "$args -q -X -f -S $syntax";
139 $code = $this->newFilterThroughCmd($source, $commandLine);
141 return $this->error(fmt("Couldn't start commandline ā%sā", $commandLine));
143 $pre = HTML::pre(HTML::raw($code));
144 $html->pushContent($pre);
153 // c-hanging-comment-ender-p: nil
154 // indent-tabs-mode: nil