2 rcs_id('$Id: PhpHighlight.php,v 1.7 2003-01-18 22:01:43 carstenklapp 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 class WikiPlugin_PhpHighlight
51 // Four required functions in a WikiPlugin.
54 return _("PhpHighlight");
57 function getDescription () {
58 return _("PHP syntax highlighting");
62 function getVersion() {
63 return preg_replace("/[Revision: $]/", '',
67 // Establish default values for each of this plugin's arguments.
68 function getDefaultArguments() {
69 // TODO: results of ini_get() should be static for multiple
70 // invocations of plugin on one WikiPage
71 return array('source' => false,
73 'string' => ini_get("highlight.string"), //'#00CC00',
74 'comment' => ini_get("highlight.comment"), //'#FF9900',
75 'keyword' => ini_get("highlight.keyword"), //'#006600',
76 'bg' => ini_get("highlight.bg"), //'#FFFFFF',
77 'default' => ini_get("highlight.default"), //'#0000CC',
78 'html' => ini_get("highlight.html") //'#000000'
82 function run($dbi, $argstr, $request) {
84 extract($this->getArgs($argstr, $request));
86 if (!function_exists('version_compare')
87 || version_compare(phpversion(), '4.2.0', 'lt')) {
88 // trigger_error(sprintf(_("%s requires PHP version %s or newer."),
89 // $this->getName(), "4.2.0"), E_USER_NOTICE);
90 /* return unhighlighted text as if <verbatim> were used */
91 // return HTML::pre($argstr); // early return
95 $this->sanify_colors($string, $comment, $keyword, $bg, $default, $html);
96 $this->set_colors($string, $comment, $keyword, $bg, $default, $html);
99 /* Wrap with "<?php\n" and "\n?>" required by
100 * highlight_string(): */
101 $source = "<?php\n" . $source . "\n?>";
103 $source = str_replace(array('< ?php', '? >'),
104 array('<?php', '?>'), $source);
107 if (!empty($has_old_php)) {
109 highlight_string($source);
110 $str = ob_get_contents();
113 $str = highlight_string($source, true);
117 /* Remove "<?php\n" and "\n?>" again: */
118 $str = str_replace(array('<?php<br />', '?>'), '', $str);
121 * We might have made some empty font tags. (The following
122 * str_replace string does not produce results on my system,
123 * maybe a php bug? '<font color="$color"></font>')
125 foreach (array($string, $comment, $keyword, $bg, $default, $html) as $color) {
126 $search = "<font color=\"$color\"></font>";
127 $str = str_replace($search, '', $str);
130 /* restore default colors in case of multiple invocations of
131 this plugin on one page */
132 $this->restore_colors();
133 return new RawXml($str);
136 function handle_plugin_args_cruft(&$argstr, &$args) {
137 $args['source'] = $argstr;
141 * Make sure color argument is valid
142 * See http://www.w3.org/TR/REC-html40/types.html#h-6.5
144 function sanify_colors($string, $comment, $keyword, $bg, $default, $html) {
145 static $html4colors = array("black", "silver", "gray", "white",
146 "maroon", "red", "purple", "fuchsia",
147 "green", "lime", "olive", "yellow",
148 "navy", "blue", "teal", "aqua");
149 /* max(strlen("fuchsia"), strlen("#00FF00"), ... ) = 7 */
151 foreach (array($string, $comment, $keyword, $bg, $default, $html) as $color) {
152 $length = strlen($color);
153 //trigger_error(sprintf(_("DEBUG: color '%s' is length %d."), $color, $length), E_USER_NOTICE);
154 if (($length == 7 || $length == 4) && substr($color, 0, 1) == "#"
155 && "#" == preg_replace("/[a-fA-F0-9]/", "", $color)
157 //trigger_error(sprintf(_("DEBUG: color '%s' appears to be hex."), $color), E_USER_NOTICE);
158 // stop checking, ok to go
159 } elseif (($length < $MAXLEN + 1) && in_array($color, $html4colors)) {
160 //trigger_error(sprintf(_("DEBUG color '%s' appears to be an HTML 4 color."), $color), E_USER_NOTICE);
161 // stop checking, ok to go
163 trigger_error(sprintf(_("Invalid color: %s"),
164 $color), E_USER_NOTICE);
165 // FIXME: also change color to something valid like "black" or ini_get("highlight.xxx")
170 function set_colors($string, $comment, $keyword, $bg, $default, $html) {
171 // set highlight colors
172 $this->oldstring = ini_set('highlight.string', $string);
173 $this->oldcomment = ini_set('highlight.comment', $comment);
174 $this->oldkeyword = ini_set('highlight.keyword', $keyword);
175 $this->oldbg = ini_set('highlight.bg', $bg);
176 $this->olddefault = ini_set('highlight.default', $default);
177 $this->oldhtml = ini_set('highlight.html', $html);
180 function restore_colors() {
181 // restore previous default highlight colors
182 ini_set('highlight.string', $this->oldstring);
183 ini_set('highlight.comment', $this->oldcomment);
184 ini_set('highlight.keyword', $this->oldkeyword);
185 ini_set('highlight.bg', $this->oldbg);
186 ini_set('highlight.default', $this->olddefault);
187 ini_set('highlight.html', $this->oldhtml);
192 // $Log: not supported by cvs2svn $
199 // c-hanging-comment-ender-p: nil
200 // indent-tabs-mode: nil