2 rcs_id('$Id: PhpHighlight.php,v 1.3 2002-11-04 19:29:40 carstenklapp 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 should not add '<?php' and '?>' to the code - the plugin does
15 * Author: Martin Geisler <gimpster@gimpster.com>.
17 * Added compatibility for PHP < 4.2.0, where the highlight_string()
18 * function has no second argument.
19 * Added ability to override colors defined in php.ini --Carsten Klapp
22 class WikiPlugin_PhpHighlight
25 // Four required functions in a WikiPlugin.
28 return _("PhpHighlight");
31 function getDescription () {
32 return _("PHP syntax highlighting");
35 // Establish default values for each of this plugin's arguments.
36 function getDefaultArguments() {
37 // TODO: results of ini_get() should be static for multiple invocations of plugin on one WikiPage
38 return array('source' => false,
39 'string' => ini_get("highlight.string"), //'#00CC00',
40 'comment' => ini_get("highlight.comment"), //'#FF9900',
41 'keyword' => ini_get("highlight.keyword"), //'#006600',
42 'bg' => ini_get("highlight.bg"), //'#FFFFFF',
43 'default' => ini_get("highlight.default"), //'#0000CC',
44 'html' => ini_get("highlight.html") //'#000000'
48 function run($dbi, $argstr, $request) {
50 extract($this->getArgs($argstr, $request));
52 if (!function_exists('version_compare')
53 || version_compare(phpversion(), '4.2.0', 'lt')) {
54 // trigger_error(sprintf(_("%s requires PHP version %s or newer."),
55 // $this->getName(), "4.2.0"), E_USER_NOTICE);
56 /* return unhighlighted text as if <verbatim> were used */
57 // return HTML::pre($argstr); // early return
61 $this->sanify_colors($string, $comment, $keyword, $bg, $default, $html);
62 $this->set_colors($string, $comment, $keyword, $bg, $default, $html);
66 highlight_string("<?php\n" . $source . "\n?>");
67 $str = ob_get_contents();
70 $str = highlight_string("<?php\n" . $source . "\n?>", true);
72 /* Remove "<?php\n" and "\n?>": */
73 $str = str_replace(array('<?php<br />', '?>'), '', $str);
74 /* We might have made some empty font tags: */
75 $search = '<font color="$default"></font>';
76 $str = str_replace($search, '', $str);
78 return new RawXml($str);
81 function handle_plugin_args_cruft(&$argstr, &$args) {
82 $args['source'] = $argstr;
85 function sanify_colors(&$string, &$comment, &$keyword, &$bg, &$default, &$html) {
86 /* Make sure color argument is either 6 digits or 3 digits prepended by a #,
87 or maybe an html color name like "black" */
91 function set_colors($string, $comment, $keyword, $bg, $default, $html) {
92 // set highlight colors
93 ini_set('highlight.string', $string);
94 ini_set('highlight.comment', $comment);
95 ini_set('highlight.keyword', $keyword);
96 ini_set('highlight.bg', $bg);
97 ini_set('highlight.default', $default);
98 ini_set('highlight.html', $html);
108 // c-hanging-comment-ender-p: nil
109 // indent-tabs-mode: nil