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 along
20 * with PhpWiki; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 * A plugin that runs the highlight_string() function in PHP on it's
26 * arguments to pretty-print PHP code.
29 * <<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 ability to override colors defined in php.ini --Carsten Klapp
49 * testing~[badworkaround~];
51 * will swallow "[somearray]"
54 class WikiPlugin_PhpHighlight
57 // Four required functions in a WikiPlugin.
60 return _("PhpHighlight");
63 function getDescription () {
64 return _("PHP syntax highlighting");
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('wrap' => true,
72 'string' => ini_get("highlight.string"), //'#00CC00',
73 'comment' => ini_get("highlight.comment"), //'#FF9900',
74 'keyword' => ini_get("highlight.keyword"), //'#006600',
75 'bg' => ini_get("highlight.bg"), //'#FFFFFF',
76 'default' => ini_get("highlight.default"), //'#0000CC',
77 'html' => ini_get("highlight.html") //'#000000'
81 function run($dbi, $argstr, &$request, $basepage) {
83 extract($this->getArgs($argstr, $request));
84 $source =& $this->source;
86 $this->sanify_colors($string, $comment, $keyword, $bg, $default, $html);
87 $this->set_colors($string, $comment, $keyword, $bg, $default, $html);
90 /* Wrap with "<?php\n" and "\n?>" required by
91 * highlight_string(): */
92 $source = "<?php\n" . $source . "\n?>";
94 $source = str_replace(array('< ?php', '? >'),
95 array('<?php', '?>'), $source);
98 $str = highlight_string($source, true);
101 /* Remove "<?php\n" and "\n?>" again: */
102 $str = str_replace(array('<?php<br />', '?>'), '', $str);
104 /* We might have made some empty font tags. */
105 foreach (array($string, $comment, $keyword, $bg, $default, $html) as $color) {
106 $search = "<font color=\"$color\"></font>";
107 $str = str_replace($search, '', $str);
109 /* Remove also empty span tags. */
110 foreach (array($string, $comment, $keyword, $bg, $default, $html) as $color) {
111 $search = "<span style=\"color: $color\"></span>";
112 $str = str_replace($search, '', $str);
115 /* restore default colors in case of multiple invocations of
116 this plugin on one page */
117 $this->restore_colors();
118 return new RawXml($str);
121 function handle_plugin_args_cruft(&$argstr, &$args) {
122 $this->source = $argstr;
126 * Make sure color argument is valid
127 * See http://www.w3.org/TR/REC-html40/types.html#h-6.5
129 function sanify_colors($string, $comment, $keyword, $bg, $default, $html) {
130 static $html4colors = array("black", "silver", "gray", "white",
131 "maroon", "red", "purple", "fuchsia",
132 "green", "lime", "olive", "yellow",
133 "navy", "blue", "teal", "aqua");
134 /* max(strlen("fuchsia"), strlen("#00FF00"), ... ) = 7 */
136 foreach (array($string, $comment, $keyword, $bg, $default, $html) as $color) {
137 $length = strlen($color);
138 //trigger_error(sprintf(_("DEBUG: color '%s' is length %d."), $color, $length), E_USER_NOTICE);
139 if (($length == 7 || $length == 4) && substr($color, 0, 1) == "#"
140 && "#" == preg_replace("/[a-fA-F0-9]/", "", $color)
142 //trigger_error(sprintf(_("DEBUG: color '%s' appears to be hex."), $color), E_USER_NOTICE);
143 // stop checking, ok to go
144 } elseif (($length < $MAXLEN + 1) && in_array($color, $html4colors)) {
145 //trigger_error(sprintf(_("DEBUG color '%s' appears to be an HTML 4 color."), $color), E_USER_NOTICE);
146 // stop checking, ok to go
148 trigger_error(sprintf(_("Invalid color: %s"),
149 $color), E_USER_NOTICE);
150 // FIXME: also change color to something valid like "black" or ini_get("highlight.xxx")
155 function set_colors($string, $comment, $keyword, $bg, $default, $html) {
156 // set highlight colors
157 $this->oldstring = ini_set('highlight.string', $string);
158 $this->oldcomment = ini_set('highlight.comment', $comment);
159 $this->oldkeyword = ini_set('highlight.keyword', $keyword);
160 $this->oldbg = ini_set('highlight.bg', $bg);
161 $this->olddefault = ini_set('highlight.default', $default);
162 $this->oldhtml = ini_set('highlight.html', $html);
165 function restore_colors() {
166 // restore previous default highlight colors
167 ini_set('highlight.string', $this->oldstring);
168 ini_set('highlight.comment', $this->oldcomment);
169 ini_set('highlight.keyword', $this->oldkeyword);
170 ini_set('highlight.bg', $this->oldbg);
171 ini_set('highlight.default', $this->olddefault);
172 ini_set('highlight.html', $this->oldhtml);
181 // c-hanging-comment-ender-p: nil
182 // indent-tabs-mode: nil