]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/SyntaxHighlighter.php
getName should not translate
[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 getDescription()
44     {
45         return _("Source code syntax highlighter (via http://www.andre-simon.de).");
46     }
47
48     function managesValidators()
49     {
50         return true;
51     }
52
53     function getDefaultArguments()
54     {
55         return array(
56             'syntax' => null, // required argument
57             'style' => null, // optional argument ["ansi", "gnu", "kr", "java", "linux"]
58             'color' => null, // optional, see highlight/themes
59             'number' => 0,
60             'wrap' => 0,
61         );
62     }
63
64     function handle_plugin_args_cruft(&$argstr, &$args)
65     {
66         $this->source = $argstr;
67     }
68
69     function newFilterThroughCmd($input, $commandLine)
70     {
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
75         );
76
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);
84             fclose($pipes[0]);
85             $buf = "";
86             while (!feof($pipes[1])) {
87                 $buf .= fgets($pipes[1], 1024);
88             }
89             fclose($pipes[1]);
90             $stderr = '';
91             while (!feof($pipes[2])) {
92                 $stderr .= fgets($pipes[2], 1024);
93             }
94             fclose($pipes[2]);
95             // It is important that you close any pipes before calling
96             // proc_close in order to avoid a deadlock
97             proc_close($process);
98             if (empty($buf)) {
99                 printXML($this->error($stderr));
100             }
101             return $buf;
102         }
103         return '';
104     }
105
106     function run($dbi, $argstr, &$request, $basepage)
107     {
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'));
112         }
113         if (empty($source)) {
114             return HTML::div(array('class' => "error"),
115                    "Please provide source code to SyntaxHighlighter plugin");
116         }
117         $args = "";
118         if (defined('HIGHLIGHT_DATA_DIR')) {
119             $args .= " --data-dir " . HIGHLIGHT_DATA_DIR;
120         }
121         if ($number != 0) {
122             $args .= " -l";
123         }
124         if ($wrap != 0) {
125             $args .= " -V";
126         }
127         $html = HTML();
128         if (!empty($color) and !preg_match('/^[\w-]+$/', $color)) {
129             $html->pushContent($this->error(fmt("invalid %s ignored", 'color')));
130             $color = false;
131         }
132         if (!empty($color)) {
133             $args .= " --style $color --inline-css";
134         }
135         if (!empty($style)) {
136             $args .= " -F $style";
137         }
138         $commandLine = HIGHLIGHT_EXE . "$args -q -X -f -S $syntax";
139         $code = $this->newFilterThroughCmd($source, $commandLine);
140         if (empty($code)) {
141             return $this->error(fmt("Couldn't start commandline ā€œ%sā€", $commandLine));
142         }
143         $pre = HTML::pre(HTML::raw($code));
144         $html->pushContent($pre);
145         return HTML($html);
146     }
147 }
148
149 // Local Variables:
150 // mode: php
151 // tab-width: 8
152 // c-basic-offset: 4
153 // c-hanging-comment-ender-p: nil
154 // indent-tabs-mode: nil
155 // End: