]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/SyntaxHighlighter.php
plugin->run consistency: request as reference, added basepage.
[SourceForge/phpwiki.git] / lib / plugin / SyntaxHighlighter.php
1 <?php // -*-php-*-
2 rcs_id('$Id: SyntaxHighlighter.php,v 1.7 2004-07-08 20:30:07 rurban Exp $');
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
19  along with PhpWiki; if not, write to the Free Software
20  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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  * syntax: See http://www.andre-simon.de/doku/highlight/highlight.html
30  * style = ["ansi", "gnu", "kr", "java", "linux"]
31  
32 <?plugin SyntaxHighlighter syntax=c style=kr color=emacs
33  #include <stdio.h>
34  
35  int main() {
36  printf("Lalala\n");
37  }
38 ?>
39
40  I did not use beautifier, because it used up more than 8M of memory on
41  my system and PHP killed it. I'm not sure whether this is a problem
42  with my integration, or with beautifier itself.
43
44 Fixes by Reini Urban:
45   support options: syntax, style, color.
46   php version switch
47   HIGHLIGHT_DATA_DIR, HIGHLIGHT_EXE
48 */
49 if (!defined('HIGHLIGHT_EXE'))
50     define('HIGHLIGHT_EXE','highlight');
51 //define('HIGHLIGHT_EXE','/usr/local/bin/highlight');
52 //define('HIGHLIGHT_EXE','/home/groups/p/ph/phpwiki/bin/highlight');
53
54 // highlight requires two subdirs themes and langDefs somewhere.
55 // Best by highlight.conf in $HOME, but the webserver user usually 
56 // doesn't have a $HOME
57 if (!defined('HIGHLIGHT_DATA_DIR'))
58     if (isWindows())
59         define('HIGHLIGHT_DATA_DIR','f:\cygnus\usr\local\share\highlight');
60     else
61         define('HIGHLIGHT_DATA_DIR','/usr/share/highlight');
62         //define('HIGHLIGHT_DATA_DIR','/home/groups/p/ph/phpwiki/share/highlight');
63
64 class WikiPlugin_SyntaxHighlighter
65 extends WikiPlugin
66 {
67     function getName () {
68         return _("SyntaxHighlighter");
69     }
70     function getDescription () {
71         return _("Source code syntax highlighter (via http://www.andre-simon.de)");
72     }
73     function managesValidators() {
74         return true;
75     }
76     function getVersion() {
77         return preg_replace("/[Revision: $]/", '',
78                             "\$Revision: 1.7 $");
79     }
80     function getDefaultArguments() {
81         return array(
82                      'syntax' => null, // required argument
83                      'style'  => null, // optional argument ["ansi", "gnu", "kr", "java", "linux"]
84                      'color'  => null, // optional, see highlight/themes
85                      'number' => 0,
86                      'wrap'   => 0,
87                      );
88     }
89     function handle_plugin_args_cruft(&$argstr, &$args) {
90         $this->source = $argstr;
91     }
92
93     function newFilterThroughCmd($input, $commandLine) {
94         $descriptorspec = array(
95                0 => array("pipe", "r"),  // stdin is a pipe that the child will read from
96                1 => array("pipe", "w"),  // stdout is a pipe that the child will write to
97                2 => array("pipe", "w"),  // stdout is a pipe that the child will write to
98         );
99
100         $process = proc_open("$commandLine", $descriptorspec, $pipes);
101         if (is_resource($process)) {
102             // $pipes now looks like this:
103             // 0 => writeable handle connected to child stdin
104             // 1 => readable  handle connected to child stdout
105             // 2 => readable  handle connected to child stderr
106             fwrite($pipes[0], $input);
107             fclose($pipes[0]);
108             $buf = "";
109             while(!feof($pipes[1])) {
110                 $buf .= fgets($pipes[1], 1024);
111             }
112             fclose($pipes[1]);
113             $stderr = '';
114             while(!feof($pipes[2])) {
115                 $stderr .= fgets($pipes[2], 1024);
116             }
117             fclose($pipes[2]);
118             // It is important that you close any pipes before calling
119             // proc_close in order to avoid a deadlock
120             $return_value = proc_close($process);
121             if (empty($buf)) printXML($this->error($stderr));
122             return $buf;
123         }
124     }
125
126     /* PHP versions < 4.3
127      * TODO: via temp file looks more promising
128      */
129     function OldFilterThroughCmd($input, $commandLine) {
130          $input = str_replace ("\\", "\\\\", $input);
131          $input = str_replace ("\"", "\\\"", $input);
132          $input = str_replace ("\$", "\\\$", $input);
133          $input = str_replace ("`", "\`", $input);
134          $input = str_replace ("'", "\'", $input);
135          //$input = str_replace (";", "\;", $input);
136
137          $pipe = popen("echo \"$input\"|$commandLine", 'r');
138          if (!$pipe) {
139             print "pipe failed.";
140             return "";
141          }
142          $output = '';
143          while (!feof($pipe)) {
144             $output .= fread($pipe, 1024);
145          }
146          pclose($pipe);
147          return $output;
148     }
149
150     function run($dbi, $argstr, &$request, $basepage) {
151         extract($this->getArgs($argstr, $request));
152         $source =& $this->source;
153         if (empty($syntax)) return $this->error(_("Syntax language not specified."));
154         if (!empty($source)) {
155             $args = "";
156             if (defined('HIGHLIGHT_DATA_DIR'))
157                 $args .= " --data-dir " . HIGHLIGHT_DATA_DIR;
158             if ($number != 0) $args .= " -l";
159             if ($wrap != 0)   $args .= " -V";
160             $html = HTML();
161             if (!empty($color) and !preg_match('/^[\w-]+$/',$color)) {
162                 $html->pushContent($this->error(fmt("invalid %s ignored",'color')));
163                 $color = false;
164             }
165             if (!empty($color)) $args .= " --style $color -c ".FindFile("uploads")."/highlight-$color.css";
166             if (!empty($style)) $args .= " -F $style";
167             $commandLine = HIGHLIGHT_EXE . "$args -q -X -f -S $syntax";
168             if (check_php_version(4,3,0))
169                 $code = $this->newFilterThroughCmd($source, $commandLine);
170             else 
171                 $code = $this->oldFilterThroughCmd($source, $commandLine);
172             if (empty($code))
173                 return $this->error(fmt("Couldn't start commandline '%s'",$commandLine));
174             $pre = HTML::pre(HTML::raw($code));
175             $pre->setAttr('class','tightenable top bottom');
176             $html->pushContent($pre);
177             $css = $GLOBALS['WikiTheme']->_CSSlink('',empty($color) ? 'highlight.css' : "uploads/highlight-$color.css",'');
178             return HTML($css,$html);
179         } else {
180             return $this->error(fmt("empty source"));
181         }
182     }
183 };
184
185 // $Log: not supported by cvs2svn $
186 // Revision 1.6  2004/06/29 18:47:40  rurban
187 // use predefined constants, and added sf.net defaults
188 //
189 // Revision 1.5  2004/06/14 11:31:39  rurban
190 // renamed global $Theme to $WikiTheme (gforge nameclash)
191 // inherit PageList default options from PageList
192 //   default sortby=pagename
193 // use options in PageList_Selectable (limit, sortby, ...)
194 // added action revert, with button at action=diff
195 // added option regex to WikiAdminSearchReplace
196 //
197 // Revision 1.4  2004/05/18 14:49:52  rurban
198 // Simplified strings for easier translation
199 //
200 // Revision 1.3  2004/05/14 17:33:12  rurban
201 // new plugin RecentChanges
202 //
203 // Revision 1.2  2004/05/14 15:56:16  rurban
204 // protect color argument, more error handling, added default css
205 //
206 // Revision 1.1  2004/05/14 14:55:52  rurban
207 // Alec Thomas original plugin, which comes with highlight http://www.andre-simon.de/,
208 // plus some extensions by Reini Urban
209 //
210 //
211
212 // For emacs users
213 // Local Variables:
214 // mode: php
215 // tab-width: 8
216 // c-basic-offset: 4
217 // c-hanging-comment-ender-p: nil
218 // indent-tabs-mode: nil
219 // End:
220 ?>