]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/PhpHighlight.php
Martin Geisler's PhpHighlight plugin. Added compatibility for PHP < 4.2.0, where...
[SourceForge/phpwiki.git] / lib / plugin / PhpHighlight.php
1 <?php // -*-php-*-\rrcs_id('$Id: PhpHighlight.php,v 1.1 2002-11-04 03:21:03 carstenklapp Exp $');\r/**\r * A plugin that runs the highlight_string() function in PHP on it's\r * arguments to pretty-print PHP code.\r *\r * Usage:\r * <?plugin PhpHighlight default='#FF0000' comment='#0000CC'\r * code that should be highlighted\r * ?>\r *\r * You should not add '<?php' and '?>' to the code - the plugin does\r * this automatically.\r *\r * Author: Martin Geisler <gimpster@gimpster.com>.\r *\r * Added compatibility for PHP < 4.2.0, where the highlight_string()\r * function has no second argument.\r * Added ability to override colors defined in php.ini --Carsten Klapp\r */\r\rclass WikiPlugin_PhpHighlight\rextends WikiPlugin\r{\r    // Four required functions in a WikiPlugin.\r\r    function getName () {\r        return _("PhpHighlight");\r    }\r\r    function getDescription () {\r        return _("PHP syntax highlighting");\r\r    }\r    // Establish default values for each of this plugin's arguments.\r    function getDefaultArguments() {\r        // TODO: results of ini_get() should be static for multiple invocations of plugin on one WikiPage\r        return array('source'  => false,\r                     'string'  => ini_get("highlight.string"),  //'#00CC00',\r                     'comment' => ini_get("highlight.comment"), //'#FF9900',\r                     'keyword' => ini_get("highlight.keyword"), //'#006600',\r                     'bg'      => ini_get("highlight.bg"),      //'#FFFFFF',\r                     'default' => ini_get("highlight.default"), //'#0000CC',\r                     'html'    => ini_get("highlight.html")     //'#000000'\r                     );\r    }\r\r    function run($dbi, $argstr, $request) {\r\r        extract($this->getArgs($argstr, $request));\r\r        if (!function_exists('version_compare')\r            || version_compare(phpversion(), '4.2.0', 'lt')) {\r            // trigger_error(sprintf(_("%s requires PHP version %s or newer."),\r            //                      $this->getName(), "4.2.0"), E_USER_NOTICE);\r            /* return unhighlighted text as if <verbatim> were used */\r            // return HTML::pre($argstr); // early return\r            $has_old_php = true;\r        }\r\r        $this->sanify_colors($string, $comment, $keyword, $bg, $default, $html);\r        $this->set_colors($string, $comment, $keyword, $bg, $default, $html);\r                \r        if ($has_old_php) {\r            ob_start();\r            highlight_string("<?php\n" . $source . "\n?>");\r            $str = ob_get_contents();\r            ob_end_clean();\r        } else {\r            $str = highlight_string("<?php\n" . $source . "\n?>", true);\r        }\r        /* Remove "<?php\n" and "\n?>": */\r        $str = str_replace(array('&lt;?php<br />', '?&gt;'), '', $str);\r        /* We might have made some empty font tags: */\r        $search = '<font color="' . ini_get('highlight.default')\r                . '"></font>';\r        $str = str_replace($search, '', $str);\r\r        return new RawXml($str);\r    }\r\r    function handle_plugin_args_cruft(&$argstr, &$args) {\r        $args['source'] = $argstr;\r    }\r\r    function sanify_colors(&$string, &$comment, &$keyword, &$bg, &$default, &$html) {\r        /* Make sure color argument is either 6 digits or 3 digits prepended by a #,\r           or maybe an html color name like "black" */\r        // TODO\r    }\r    \r    function set_colors($string, $comment, $keyword, $bg, $default, $html) {\r        // set highlight colors\r        ini_set('highlight.string', $string);\r        ini_set('highlight.comment', $comment);\r        ini_set('highlight.keyword', $keyword);\r        ini_set('highlight.bg', $bg);\r        ini_set('highlight.default', $default);\r        ini_set('highlight.html', $html);\r    }\r    \r};\r\r// For emacs users\r// Local Variables:\r// mode: php\r// tab-width: 8\r// c-basic-offset: 4\r// c-hanging-comment-ender-p: nil\r// indent-tabs-mode: nil\r// End:\r?>\r