]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/PhpHighlight.php
use unix line endings (doh)
[SourceForge/phpwiki.git] / lib / plugin / PhpHighlight.php
1 <?php // -*-php-*-
2 rcs_id('$Id: PhpHighlight.php,v 1.3 2002-11-04 19:29:40 carstenklapp Exp $');
3 /**
4  * A plugin that runs the highlight_string() function in PHP on it's
5  * arguments to pretty-print PHP code.
6  *
7  * Usage:
8  * <?plugin PhpHighlight default='#FF0000' comment='#0000CC'
9  * code that should be highlighted
10  * ?>
11  *
12  * You should not add '<?php' and '?>' to the code - the plugin does
13  * this automatically.
14  *
15  * Author: Martin Geisler <gimpster@gimpster.com>.
16  *
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
20  */
21
22 class WikiPlugin_PhpHighlight
23 extends WikiPlugin
24 {
25     // Four required functions in a WikiPlugin.
26
27     function getName () {
28         return _("PhpHighlight");
29     }
30
31     function getDescription () {
32         return _("PHP syntax highlighting");
33
34     }
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'
45                      );
46     }
47
48     function run($dbi, $argstr, $request) {
49
50         extract($this->getArgs($argstr, $request));
51
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
58             $has_old_php = true;
59         }
60
61         $this->sanify_colors($string, $comment, $keyword, $bg, $default, $html);
62         $this->set_colors($string, $comment, $keyword, $bg, $default, $html);
63
64         if ($has_old_php) {
65             ob_start();
66             highlight_string("<?php\n" . $source . "\n?>");
67             $str = ob_get_contents();
68             ob_end_clean();
69         } else {
70             $str = highlight_string("<?php\n" . $source . "\n?>", true);
71         }
72         /* Remove "<?php\n" and "\n?>": */
73         $str = str_replace(array('&lt;?php<br />', '?&gt;'), '', $str);
74         /* We might have made some empty font tags: */
75         $search = '<font color="$default"></font>';
76         $str = str_replace($search, '', $str);
77
78         return new RawXml($str);
79     }
80
81     function handle_plugin_args_cruft(&$argstr, &$args) {
82         $args['source'] = $argstr;
83     }
84
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" */
88         // TODO
89     }
90
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);
99     }
100
101 };
102
103 // For emacs users
104 // Local Variables:
105 // mode: php
106 // tab-width: 8
107 // c-basic-offset: 4
108 // c-hanging-comment-ender-p: nil
109 // indent-tabs-mode: nil
110 // End:
111 ?>