]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/SyntaxHighlighter.php
Better use method
[SourceForge/phpwiki.git] / lib / plugin / SyntaxHighlighter.php
1 <?php
2
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 along
19  * with PhpWiki; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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 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'))
35     if (isWindows())
36         define('HIGHLIGHT_DATA_DIR', 'f:\cygnus\usr\local\share\highlight');
37     else
38         define('HIGHLIGHT_DATA_DIR', '/usr/share/highlight');
39
40 class WikiPlugin_SyntaxHighlighter
41     extends WikiPlugin
42 {
43     function getName()
44     {
45         return _("SyntaxHighlighter");
46     }
47
48     function getDescription()
49     {
50         return _("Source code syntax highlighter (via http://www.andre-simon.de)");
51     }
52
53     function managesValidators()
54     {
55         return true;
56     }
57
58     function getDefaultArguments()
59     {
60         return array(
61             'syntax' => null, // required argument
62             'style' => null, // optional argument ["ansi", "gnu", "kr", "java", "linux"]
63             'color' => null, // optional, see highlight/themes
64             'number' => 0,
65             'wrap' => 0,
66         );
67     }
68
69     function handle_plugin_args_cruft(&$argstr, &$args)
70     {
71         $this->source = $argstr;
72     }
73
74     function newFilterThroughCmd($input, $commandLine)
75     {
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
80         );
81
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);
89             fclose($pipes[0]);
90             $buf = "";
91             while (!feof($pipes[1])) {
92                 $buf .= fgets($pipes[1], 1024);
93             }
94             fclose($pipes[1]);
95             $stderr = '';
96             while (!feof($pipes[2])) {
97                 $stderr .= fgets($pipes[2], 1024);
98             }
99             fclose($pipes[2]);
100             // It is important that you close any pipes before calling
101             // proc_close in order to avoid a deadlock
102             proc_close($process);
103             if (empty($buf)) {
104                 printXML($this->error($stderr));
105             }
106             return $buf;
107         }
108         return '';
109     }
110
111     function run($dbi, $argstr, &$request, $basepage)
112     {
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'));
117         }
118         if (empty($source)) {
119             return $this->error(fmt("empty source"));
120         }
121         $args = "";
122         if (defined('HIGHLIGHT_DATA_DIR')) {
123             $args .= " --data-dir " . HIGHLIGHT_DATA_DIR;
124         }
125         if ($number != 0) {
126             $args .= " -l";
127         }
128         if ($wrap != 0) {
129             $args .= " -V";
130         }
131         $html = HTML();
132         if (!empty($color) and !preg_match('/^[\w-]+$/', $color)) {
133             $html->pushContent($this->error(fmt("invalid %s ignored", 'color')));
134             $color = false;
135         }
136         if (!empty($color)) {
137             $args .= " --style $color --inline-css";
138         }
139         if (!empty($style)) {
140             $args .= " -F $style";
141         }
142         $commandLine = HIGHLIGHT_EXE . "$args -q -X -f -S $syntax";
143         $code = $this->newFilterThroughCmd($source, $commandLine);
144         if (empty($code)) {
145             return $this->error(fmt("Couldn't start commandline '%s'", $commandLine));
146         }
147         $pre = HTML::pre(HTML::raw($code));
148         $html->pushContent($pre);
149         return HTML($html);
150     }
151 }
152
153 // Local Variables:
154 // mode: php
155 // tab-width: 8
156 // c-basic-offset: 4
157 // c-hanging-comment-ender-p: nil
158 // indent-tabs-mode: nil
159 // End: