2 rcs_id('$Id: SyntaxHighlighter.php,v 1.1 2004-05-14 14:55:52 rurban Exp $');
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
19 along with PhpWiki; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 * The SyntaxHighlighter plugin passes all its arguments through a C++
25 * highlighter called "highlight" (available at http://www.andre-simon.de/).
29 * syntax: See http://www.andre-simon.de/doku/highlight/highlight.html
30 * style = ["ansi", "gnu", "kr", "java", "linux"]
32 <?plugin SyntaxHighlighter syntax=c style=kr
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.
45 support options: syntax, style, color.
47 HIGHLIGHT_DATA_DIR, HIGHLIGHT_EXE
50 define('HIGHLIGHT_EXE','highlight');
51 //define('HIGHLIGHT_EXE','/usr/local/bin/highlight');
53 // highlight requires two subdirs themes and langDefs somewhere.
55 define('HIGHLIGHT_DATA_DIR','f:\cygnus\usr\local\share\highlight');
57 define('HIGHLIGHT_DATA_DIR','/usr/local/share/highlight');
59 class WikiPlugin_SyntaxHighlighter
63 return _("SyntaxHighlighter");
65 function getDescription () {
66 return _("Source code syntax highlighter (via http://www.andre-simon.de)");
68 function managesValidators() {
71 function getVersion() {
72 return preg_replace("/[Revision: $]/", '',
75 function getDefaultArguments() {
84 function handle_plugin_args_cruft(&$argstr, &$args) {
85 $this->source = $argstr;
88 function newFilterThroughCmd($input, $commandLine) {
89 $descriptorspec = array(
90 0 => array("pipe", "r"), // stdin is a pipe that the child will read from
91 1 => array("pipe", "w"), // stdout is a pipe that the child will write to
92 2 => array("pipe", "w"), // stdout is a pipe that the child will write to
95 $process = proc_open("$commandLine", $descriptorspec, $pipes);
96 if (is_resource($process)) {
97 // $pipes now looks like this:
98 // 0 => writeable handle connected to child stdin
99 // 1 => readable handle connected to child stdout
100 // 2 => readable handle connected to child stderr
101 fwrite($pipes[0], $input);
104 while(!feof($pipes[1])) {
105 $buf .= fgets($pipes[1], 1024);
109 while(!feof($pipes[2])) {
110 $stderr .= fgets($pipes[2], 1024);
113 // It is important that you close any pipes before calling
114 // proc_close in order to avoid a deadlock
115 $return_value = proc_close($process);
116 if (empty($buf)) printXML($this->error($stderr));
121 /* PHP versions < 4.3
122 * TODO: via temp file looks more promising
124 function OldFilterThroughCmd($input, $commandLine) {
125 $input = str_replace ("\\", "\\\\", $input);
126 $input = str_replace ("\"", "\\\"", $input);
127 $input = str_replace ("\$", "\\\$", $input);
128 $input = str_replace ("`", "\`", $input);
129 $input = str_replace ("'", "\'", $input);
130 //$input = str_replace (";", "\;", $input);
132 $pipe = popen("echo \"$input\"|$commandLine", 'r');
134 print "pipe failed.";
138 while (!feof($pipe)) {
139 $output .= fread($pipe, 1024);
145 function run($dbi, $argstr, $request) {
146 extract($this->getArgs($argstr, $request));
147 $source =& $this->source;
148 if (empty($syntax)) return $this->error(_("Syntax language not specified)"));
149 if (!empty($source)) {
151 if (defined('HIGHLIGHT_DATA_DIR'))
152 $args .= " --data-dir " . HIGHLIGHT_DATA_DIR;
153 if ($number != 0) $args .= " -l";
154 if ($wrap != 0) $args .= " -V";
155 if (!empty($color)) $args .= " --style $color -c ".FindFile("uploads")."/highlight.css";
156 if (!empty($style)) $args .= " -F $style";
157 $commandLine = HIGHLIGHT_EXE . "$args -q -X -f -S $syntax";
158 if (check_php_version(4,3,0))
159 $code = $this->newFilterThroughCmd($source, $commandLine);
161 $code = $this->oldFilterThroughCmd($source, $commandLine);
163 return $this->error(fmt("Couldn't start commandline '%s'",$commandLine));
164 $html = HTML::pre(HTML::raw($code));
167 $html->setAttr('class','tightenable top bottom');
168 $css = $Theme->_CSSlink('',empty($color) ? 'highlight.css' : 'uploads/highlight.css','');
169 return HTML($css,$html);
171 return $this->error(_("empty source"));
176 // $Log: not supported by cvs2svn $
184 // c-hanging-comment-ender-p: nil
185 // indent-tabs-mode: nil