2 rcs_id('$Id: PhpHighlight.php,v 1.9 2004-04-10 07:25:24 rurban Exp $');
4 Copyright 1999, 2000, 2001, 2002 $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 * A plugin that runs the highlight_string() function in PHP on it's
25 * arguments to pretty-print PHP code.
28 * <?plugin PhpHighlight default='#FF0000' comment='#0000CC'
29 * code that should be highlighted
32 * You do not have to add '<?php' and '?>' to the code - the plugin
33 * does this automatically if you do not set wrap to 0.
35 * If you do set wrap to 0, then you'll have to start and stop PHP
36 * mode in the source yourself, or you wont see any highlighting. But
37 * you cannot use '<?php' and '?>' in the source, because this
38 * interferes with PhpWiki, you'll have use '< ?php' and '? >'
41 * Author: Martin Geisler <gimpster@gimpster.com>.
43 * Added compatibility for PHP < 4.2.0, where the highlight_string()
44 * function has no second argument.
45 * Added ability to override colors defined in php.ini --Carsten Klapp
48 * <?plugin PhpHighlight
50 * testing~[badworkaround~];
52 * will swallow "[somearray]"
55 class WikiPlugin_PhpHighlight
58 // Four required functions in a WikiPlugin.
61 return _("PhpHighlight");
64 function getDescription () {
65 return _("PHP syntax highlighting");
69 function getVersion() {
70 return preg_replace("/[Revision: $]/", '',
74 // Establish default values for each of this plugin's arguments.
75 function getDefaultArguments() {
76 // TODO: results of ini_get() should be static for multiple
77 // invocations of plugin on one WikiPage
78 return array('wrap' => true,
79 'string' => ini_get("highlight.string"), //'#00CC00',
80 'comment' => ini_get("highlight.comment"), //'#FF9900',
81 'keyword' => ini_get("highlight.keyword"), //'#006600',
82 'bg' => ini_get("highlight.bg"), //'#FFFFFF',
83 'default' => ini_get("highlight.default"), //'#0000CC',
84 'html' => ini_get("highlight.html") //'#000000'
88 function run($dbi, $argstr, &$request, $basepage) {
90 extract($this->getArgs($argstr, $request));
91 $source =& $this->source;
93 $this->sanify_colors($string, $comment, $keyword, $bg, $default, $html);
94 $this->set_colors($string, $comment, $keyword, $bg, $default, $html);
97 /* Wrap with "<?php\n" and "\n?>" required by
98 * highlight_string(): */
99 $source = "<?php\n" . $source . "\n?>";
101 $source = str_replace(array('< ?php', '? >'),
102 array('<?php', '?>'), $source);
105 if (!check_php_version(4,2,0)) {
107 highlight_string($source);
108 $str = ob_get_contents();
111 $str = highlight_string($source, true);
115 /* Remove "<?php\n" and "\n?>" again: */
116 $str = str_replace(array('<?php<br />', '?>'), '', $str);
119 * We might have made some empty font tags. (The following
120 * str_replace string does not produce results on my system,
121 * maybe a php bug? '<font color="$color"></font>')
123 foreach (array($string, $comment, $keyword, $bg, $default, $html) as $color) {
124 $search = "<font color=\"$color\"></font>";
125 $str = str_replace($search, '', $str);
128 /* restore default colors in case of multiple invocations of
129 this plugin on one page */
130 $this->restore_colors();
131 return new RawXml($str);
134 function handle_plugin_args_cruft(&$argstr, &$args) {
135 $this->source = $argstr;
139 * Make sure color argument is valid
140 * See http://www.w3.org/TR/REC-html40/types.html#h-6.5
142 function sanify_colors($string, $comment, $keyword, $bg, $default, $html) {
143 static $html4colors = array("black", "silver", "gray", "white",
144 "maroon", "red", "purple", "fuchsia",
145 "green", "lime", "olive", "yellow",
146 "navy", "blue", "teal", "aqua");
147 /* max(strlen("fuchsia"), strlen("#00FF00"), ... ) = 7 */
149 foreach (array($string, $comment, $keyword, $bg, $default, $html) as $color) {
150 $length = strlen($color);
151 //trigger_error(sprintf(_("DEBUG: color '%s' is length %d."), $color, $length), E_USER_NOTICE);
152 if (($length == 7 || $length == 4) && substr($color, 0, 1) == "#"
153 && "#" == preg_replace("/[a-fA-F0-9]/", "", $color)
155 //trigger_error(sprintf(_("DEBUG: color '%s' appears to be hex."), $color), E_USER_NOTICE);
156 // stop checking, ok to go
157 } elseif (($length < $MAXLEN + 1) && in_array($color, $html4colors)) {
158 //trigger_error(sprintf(_("DEBUG color '%s' appears to be an HTML 4 color."), $color), E_USER_NOTICE);
159 // stop checking, ok to go
161 trigger_error(sprintf(_("Invalid color: %s"),
162 $color), E_USER_NOTICE);
163 // FIXME: also change color to something valid like "black" or ini_get("highlight.xxx")
168 function set_colors($string, $comment, $keyword, $bg, $default, $html) {
169 // set highlight colors
170 $this->oldstring = ini_set('highlight.string', $string);
171 $this->oldcomment = ini_set('highlight.comment', $comment);
172 $this->oldkeyword = ini_set('highlight.keyword', $keyword);
173 $this->oldbg = ini_set('highlight.bg', $bg);
174 $this->olddefault = ini_set('highlight.default', $default);
175 $this->oldhtml = ini_set('highlight.html', $html);
178 function restore_colors() {
179 // restore previous default highlight colors
180 ini_set('highlight.string', $this->oldstring);
181 ini_set('highlight.comment', $this->oldcomment);
182 ini_set('highlight.keyword', $this->oldkeyword);
183 ini_set('highlight.bg', $this->oldbg);
184 ini_set('highlight.default', $this->olddefault);
185 ini_set('highlight.html', $this->oldhtml);
190 // $Log: not supported by cvs2svn $
191 // Revision 1.8 2004/02/17 12:11:36 rurban
192 // added missing 4th basepage arg at plugin->run() to almost all plugins. This caused no harm so far, because it was silently dropped on normal usage. However on plugin internal ->run invocations it failed. (InterWikiSearch, IncludeSiteMap, ...)
194 // Revision 1.7 2003/01/18 22:01:43 carstenklapp
196 // Reformatting & tabs to spaces;
197 // Added copyleft, getVersion, getDescription, rcs_id.
205 // c-hanging-comment-ender-p: nil
206 // indent-tabs-mode: nil