2 rcs_id('$Id: PhpHighlight.php,v 1.6 2002-11-25 19:53:23 dairiki Exp $');
4 * A plugin that runs the highlight_string() function in PHP on it's
5 * arguments to pretty-print PHP code.
8 * <?plugin PhpHighlight default='#FF0000' comment='#0000CC'
9 * code that should be highlighted
12 * You do not have to add '<?php' and '?>' to the code - the plugin
13 * does this automatically if you do not set wrap to 0.
15 * If you do set wrap to 0, then you'll have to start and stop PHP
16 * mode in the source yourself, or you wont see any highlighting. But
17 * you cannot use '<?php' and '?>' in the source, because this
18 * interferes with PhpWiki, you'll have use '< ?php' and '? >'
21 * Author: Martin Geisler <gimpster@gimpster.com>.
23 * Added compatibility for PHP < 4.2.0, where the highlight_string()
24 * function has no second argument.
25 * Added ability to override colors defined in php.ini --Carsten Klapp
28 class WikiPlugin_PhpHighlight
31 // Four required functions in a WikiPlugin.
34 return _("PhpHighlight");
37 function getDescription () {
38 return _("PHP syntax highlighting");
41 // Establish default values for each of this plugin's arguments.
42 function getDefaultArguments() {
43 // TODO: results of ini_get() should be static for multiple
44 // invocations of plugin on one WikiPage
45 return array('source' => false,
47 'string' => ini_get("highlight.string"), //'#00CC00',
48 'comment' => ini_get("highlight.comment"), //'#FF9900',
49 'keyword' => ini_get("highlight.keyword"), //'#006600',
50 'bg' => ini_get("highlight.bg"), //'#FFFFFF',
51 'default' => ini_get("highlight.default"), //'#0000CC',
52 'html' => ini_get("highlight.html") //'#000000'
56 function run($dbi, $argstr, $request) {
58 extract($this->getArgs($argstr, $request));
60 if (!function_exists('version_compare')
61 || version_compare(phpversion(), '4.2.0', 'lt')) {
62 // trigger_error(sprintf(_("%s requires PHP version %s or newer."),
63 // $this->getName(), "4.2.0"), E_USER_NOTICE);
64 /* return unhighlighted text as if <verbatim> were used */
65 // return HTML::pre($argstr); // early return
69 $this->sanify_colors($string, $comment, $keyword, $bg, $default, $html);
70 $this->set_colors($string, $comment, $keyword, $bg, $default, $html);
73 /* Wrap with "<?php\n" and "\n?>" required by
74 * highlight_string(): */
75 $source = "<?php\n" . $source . "\n?>";
77 $source = str_replace(array('< ?php', '? >'),
78 array('<?php', '?>'), $source);
81 if (!empty($has_old_php)) {
83 highlight_string($source);
84 $str = ob_get_contents();
87 $str = highlight_string($source, true);
91 /* Remove "<?php\n" and "\n?>" again: */
92 $str = str_replace(array('<?php<br />', '?>'), '', $str);
95 * We might have made some empty font tags. (The following
96 * str_replace string does not produce results on my system,
97 * maybe a php bug? '<font color="$color"></font>')
99 foreach (array($string, $comment, $keyword, $bg, $default, $html) as $color) {
100 $search = "<font color=\"$color\"></font>";
101 $str = str_replace($search, '', $str);
104 /* restore default colors in case of multiple invocations of
105 this plugin on one page */
106 $this->restore_colors();
107 return new RawXml($str);
110 function handle_plugin_args_cruft(&$argstr, &$args) {
111 $args['source'] = $argstr;
115 * Make sure color argument is valid
116 * See http://www.w3.org/TR/REC-html40/types.html#h-6.5
118 function sanify_colors($string, $comment, $keyword, $bg, $default, $html) {
119 static $html4colors = array("black", "silver", "gray", "white",
120 "maroon", "red", "purple", "fuchsia",
121 "green", "lime", "olive", "yellow",
122 "navy", "blue", "teal", "aqua");
123 /* max(strlen("fuchsia"), strlen("#00FF00"), ... ) = 7 */
125 foreach (array($string, $comment, $keyword, $bg, $default, $html) as $color) {
126 $length = strlen($color);
127 //trigger_error(sprintf(_("DEBUG: color '%s' is length %d."), $color, $length), E_USER_NOTICE);
128 if (($length == 7 || $length == 4) && substr($color, 0, 1) == "#"
129 && "#" == preg_replace("/[a-fA-F0-9]/", "", $color)
131 //trigger_error(sprintf(_("DEBUG: color '%s' appears to be hex."), $color), E_USER_NOTICE);
132 // stop checking, ok to go
133 } elseif (($length < $MAXLEN + 1) && in_array($color, $html4colors)) {
134 //trigger_error(sprintf(_("DEBUG color '%s' appears to be an HTML 4 color."), $color), E_USER_NOTICE);
135 // stop checking, ok to go
137 trigger_error(sprintf(_("Invalid color: %s"),
138 $color), E_USER_NOTICE);
139 // FIXME: also change color to something valid like "black" or ini_get("highlight.xxx")
144 function set_colors($string, $comment, $keyword, $bg, $default, $html) {
145 // set highlight colors
146 $this->oldstring = ini_set('highlight.string', $string);
147 $this->oldcomment = ini_set('highlight.comment', $comment);
148 $this->oldkeyword = ini_set('highlight.keyword', $keyword);
149 $this->oldbg = ini_set('highlight.bg', $bg);
150 $this->olddefault = ini_set('highlight.default', $default);
151 $this->oldhtml = ini_set('highlight.html', $html);
154 function restore_colors() {
155 // restore previous default highlight colors
156 ini_set('highlight.string', $this->oldstring);
157 ini_set('highlight.comment', $this->oldcomment);
158 ini_set('highlight.keyword', $this->oldkeyword);
159 ini_set('highlight.bg', $this->oldbg);
160 ini_set('highlight.default', $this->olddefault);
161 ini_set('highlight.html', $this->oldhtml);
171 // c-hanging-comment-ender-p: nil
172 // indent-tabs-mode: nil