4 * Copyright 1999, 2000, 2001, 2002 $ThePhpWikiProgrammingTeam
5 * Copyright 2009 Marc-Etienne Vargenau, Alcatel-Lucent
7 * This file is part of PhpWiki.
9 * PhpWiki is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * PhpWiki is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with PhpWiki; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 * A plugin that runs the highlight_string() function in PHP on it's
26 * arguments to pretty-print PHP code.
29 * <?plugin PhpHighlight default='#FF0000' comment='#0000CC'
30 * code that should be highlighted
33 * You do not have to add '<?php' and '?>' to the code - the plugin
34 * does this automatically if you do not set wrap to 0.
36 * If you do set wrap to 0, then you'll have to start and stop PHP
37 * mode in the source yourself, or you wont see any highlighting. But
38 * you cannot use '<?php' and '?>' in the source, because this
39 * interferes with PhpWiki, you'll have use '< ?php' and '? >'
42 * Author: Martin Geisler <gimpster@gimpster.com>.
44 * Added compatibility for PHP < 4.2.0, where the highlight_string()
45 * function has no second argument.
46 * Added ability to override colors defined in php.ini --Carsten Klapp
49 * <?plugin PhpHighlight
51 * testing~[badworkaround~];
53 * will swallow "[somearray]"
56 class WikiPlugin_PhpHighlight
59 // Four required functions in a WikiPlugin.
62 return _("PhpHighlight");
65 function getDescription () {
66 return _("PHP syntax highlighting");
70 function getVersion() {
71 return preg_replace("/[Revision: $]/", '',
75 // Establish default values for each of this plugin's arguments.
76 function getDefaultArguments() {
77 // TODO: results of ini_get() should be static for multiple
78 // invocations of plugin on one WikiPage
79 return array('wrap' => true,
80 'string' => ini_get("highlight.string"), //'#00CC00',
81 'comment' => ini_get("highlight.comment"), //'#FF9900',
82 'keyword' => ini_get("highlight.keyword"), //'#006600',
83 'bg' => ini_get("highlight.bg"), //'#FFFFFF',
84 'default' => ini_get("highlight.default"), //'#0000CC',
85 'html' => ini_get("highlight.html") //'#000000'
89 function run($dbi, $argstr, &$request, $basepage) {
91 extract($this->getArgs($argstr, $request));
92 $source =& $this->source;
94 $this->sanify_colors($string, $comment, $keyword, $bg, $default, $html);
95 $this->set_colors($string, $comment, $keyword, $bg, $default, $html);
98 /* Wrap with "<?php\n" and "\n?>" required by
99 * highlight_string(): */
100 $source = "<?php\n" . $source . "\n?>";
102 $source = str_replace(array('< ?php', '? >'),
103 array('<?php', '?>'), $source);
106 if (!check_php_version(4,2,0)) {
108 highlight_string($source);
109 $str = ob_get_contents();
112 $str = highlight_string($source, true);
116 /* Remove "<?php\n" and "\n?>" again: */
117 $str = str_replace(array('<?php<br />', '?>'), '', $str);
119 /* We might have made some empty font tags. */
120 foreach (array($string, $comment, $keyword, $bg, $default, $html) as $color) {
121 $search = "<font color=\"$color\"></font>";
122 $str = str_replace($search, '', $str);
124 /* Remove also empty span tags. */
125 foreach (array($string, $comment, $keyword, $bg, $default, $html) as $color) {
126 $search = "<span style=\"color: $color\"></span>";
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 $this->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);
197 // c-hanging-comment-ender-p: nil
198 // indent-tabs-mode: nil