]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/SyntaxHighlighter.php
Remove unused variables
[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 HTML::div(array('class' => "error"),
120                    "Please provide source code to SyntaxHighlighter plugin");
121         }
122         $args = "";
123         if (defined('HIGHLIGHT_DATA_DIR')) {
124             $args .= " --data-dir " . HIGHLIGHT_DATA_DIR;
125         }
126         if ($number != 0) {
127             $args .= " -l";
128         }
129         if ($wrap != 0) {
130             $args .= " -V";
131         }
132         $html = HTML();
133         if (!empty($color) and !preg_match('/^[\w-]+$/', $color)) {
134             $html->pushContent($this->error(fmt("invalid %s ignored", 'color')));
135             $color = false;
136         }
137         if (!empty($color)) {
138             $args .= " --style $color --inline-css";
139         }
140         if (!empty($style)) {
141             $args .= " -F $style";
142         }
143         $commandLine = HIGHLIGHT_EXE . "$args -q -X -f -S $syntax";
144         $code = $this->newFilterThroughCmd($source, $commandLine);
145         if (empty($code)) {
146             return $this->error(fmt("Couldn't start commandline ā€œ%sā€", $commandLine));
147         }
148         $pre = HTML::pre(HTML::raw($code));
149         $html->pushContent($pre);
150         return HTML($html);
151     }
152 }
153
154 // Local Variables:
155 // mode: php
156 // tab-width: 8
157 // c-basic-offset: 4
158 // c-hanging-comment-ender-p: nil
159 // indent-tabs-mode: nil
160 // End: