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 its
26 * arguments to pretty-print PHP code.
29 class WikiPlugin_PhpHighlight
32 function getDescription()
34 return _("PHP syntax highlighting.");
37 // Establish default values for each of this plugin's arguments.
38 function getDefaultArguments()
40 // TODO: results of ini_get() should be static for multiple
41 // invocations of plugin on one WikiPage
42 return array('wrap' => true,
43 'string' => ini_get("highlight.string"), //'#00CC00',
44 'comment' => ini_get("highlight.comment"), //'#FF9900',
45 'keyword' => ini_get("highlight.keyword"), //'#006600',
46 'bg' => (version_compare(PHP_VERSION, '5.4', '<')) ? ini_get("highlight.bg") : '#FFFFFF',
47 'default' => ini_get("highlight.default"), //'#0000CC',
48 'html' => ini_get("highlight.html") //'#000000'
52 function run($dbi, $argstr, &$request, $basepage)
55 extract($this->getArgs($argstr, $request));
56 $source =& $this->source;
58 return HTML::div(array('class' => "error"),
59 _("Please provide source code to PhpHighlight plugin"));
62 $this->sanify_colors($string, $comment, $keyword, $bg, $default, $html);
63 $this->set_colors($string, $comment, $keyword, $bg, $default, $html);
66 /* Wrap with "<?php\n" and "\n?>" required by highlight_string(): */
67 $source = "<?php\n" . $source . "\n?>";
69 $source = str_replace(array('< ?php', '? >'),
70 array('<?php', '?>'), $source);
73 $str = highlight_string($source, true);
76 /* Remove "<?php\n" and "\n?>" again: */
77 $str = str_replace(array('<?php<br />', '?>'), '', $str);
80 /* Remove empty span tags. */
81 foreach (array($string, $comment, $keyword, $bg, $default, $html) as $color) {
82 $search = "<span style=\"color: $color\"></span>";
83 $str = str_replace($search, '', $str);
86 /* restore default colors in case of multiple invocations of this plugin on one page */
87 $this->restore_colors();
88 return new RawXml($str);
91 function handle_plugin_args_cruft(&$argstr, &$args)
93 $this->source = $argstr;
97 * Make sure color argument is valid
98 * See http://www.w3.org/TR/REC-html40/types.html#h-6.5
100 function sanify_colors($string, $comment, $keyword, $bg, $default, $html)
102 static $html4colors = array("black", "silver", "gray", "white",
103 "maroon", "red", "purple", "fuchsia",
104 "green", "lime", "olive", "yellow",
105 "navy", "blue", "teal", "aqua");
106 /* max(strlen("fuchsia"), strlen("#00FF00"), ... ) = 7 */
108 foreach (array($string, $comment, $keyword, $bg, $default, $html) as $color) {
109 $length = strlen($color);
110 //trigger_error(sprintf(_("DEBUG: color “%s” is length %d."), $color, $length), E_USER_NOTICE);
111 if (($length == 7 || $length == 4) && substr($color, 0, 1) == "#"
112 && "#" == preg_replace("/[a-fA-F0-9]/", "", $color)
114 //trigger_error(sprintf(_("DEBUG: color “%s” appears to be hex."), $color), E_USER_NOTICE);
115 // stop checking, ok to go
116 } elseif (($length < $MAXLEN + 1) && in_array($color, $html4colors)) {
117 //trigger_error(sprintf(_("DEBUG color “%s” appears to be an HTML 4 color."), $color), E_USER_NOTICE);
118 // stop checking, ok to go
120 trigger_error(sprintf(_("Invalid color: %s"),
121 $color), E_USER_NOTICE);
122 // FIXME: also change color to something valid like "black" or ini_get("highlight.xxx")
127 function set_colors($string, $comment, $keyword, $bg, $default, $html)
129 // set highlight colors
130 $this->oldstring = ini_set('highlight.string', $string);
131 $this->oldcomment = ini_set('highlight.comment', $comment);
132 $this->oldkeyword = ini_set('highlight.keyword', $keyword);
133 if (version_compare(PHP_VERSION, '5.4', '<')) {
134 $this->oldbg = ini_set('highlight.bg', $bg);
136 $this->olddefault = ini_set('highlight.default', $default);
137 $this->oldhtml = ini_set('highlight.html', $html);
140 function restore_colors()
142 // restore previous default highlight colors
143 ini_set('highlight.string', $this->oldstring);
144 ini_set('highlight.comment', $this->oldcomment);
145 ini_set('highlight.keyword', $this->oldkeyword);
146 if (version_compare(PHP_VERSION, '5.4', '<')) {
147 ini_set('highlight.bg', $this->oldbg);
149 ini_set('highlight.default', $this->olddefault);
150 ini_set('highlight.html', $this->oldhtml);
159 // c-hanging-comment-ender-p: nil
160 // indent-tabs-mode: nil