]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/PhpHighlight.php
Remove unused variables
[SourceForge/phpwiki.git] / lib / plugin / PhpHighlight.php
1 <?php
2
3 /**
4  * Copyright 1999, 2000, 2001, 2002 $ThePhpWikiProgrammingTeam
5  * Copyright 2009 Marc-Etienne Vargenau, Alcatel-Lucent
6  *
7  * This file is part of PhpWiki.
8  *
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.
13  *
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.
18  *
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.
22  */
23
24 /**
25  * A plugin that runs the highlight_string() function in PHP on its
26  * arguments to pretty-print PHP code.
27  */
28
29 class WikiPlugin_PhpHighlight
30     extends WikiPlugin
31 {
32     function getName()
33     {
34         return _("PhpHighlight");
35     }
36
37     function getDescription()
38     {
39         return _("PHP syntax highlighting.");
40     }
41
42     // Establish default values for each of this plugin's arguments.
43     function getDefaultArguments()
44     {
45         // TODO: results of ini_get() should be static for multiple
46         // invocations of plugin on one WikiPage
47         return array('wrap' => true,
48             'string' => ini_get("highlight.string"), //'#00CC00',
49             'comment' => ini_get("highlight.comment"), //'#FF9900',
50             'keyword' => ini_get("highlight.keyword"), //'#006600',
51             'bg' => (version_compare(PHP_VERSION, '5.4', '<')) ? ini_get("highlight.bg") : '#FFFFFF',
52             'default' => ini_get("highlight.default"), //'#0000CC',
53             'html' => ini_get("highlight.html") //'#000000'
54         );
55     }
56
57     function run($dbi, $argstr, &$request, $basepage)
58     {
59
60         extract($this->getArgs($argstr, $request));
61         $source =& $this->source;
62         if (empty($source)) {
63             return HTML::div(array('class' => "error"),
64                    _("Please provide source code to PhpHighlight plugin"));
65         }
66
67         $this->sanify_colors($string, $comment, $keyword, $bg, $default, $html);
68         $this->set_colors($string, $comment, $keyword, $bg, $default, $html);
69
70         if ($wrap) {
71             /* Wrap with "<?php\n" and "\n?>" required by highlight_string(): */
72             $source = "<?php\n" . $source . "\n?>";
73         } else {
74             $source = str_replace(array('< ?php', '? >'),
75                 array('<?php', '?>'), $source);
76         }
77
78         $str = highlight_string($source, true);
79
80         if ($wrap) {
81             /* Remove "<?php\n" and "\n?>" again: */
82             $str = str_replace(array('&lt;?php<br />', '?&gt;'), '', $str);
83         }
84
85         /* Remove empty span tags. */
86         foreach (array($string, $comment, $keyword, $bg, $default, $html) as $color) {
87             $search = "<span style=\"color: $color\"></span>";
88             $str = str_replace($search, '', $str);
89         }
90
91         /* restore default colors in case of multiple invocations of this plugin on one page */
92         $this->restore_colors();
93         return new RawXml($str);
94     }
95
96     function handle_plugin_args_cruft(&$argstr, &$args)
97     {
98         $this->source = $argstr;
99     }
100
101     /**
102      * Make sure color argument is valid
103      * See http://www.w3.org/TR/REC-html40/types.html#h-6.5
104      */
105     function sanify_colors($string, $comment, $keyword, $bg, $default, $html)
106     {
107         static $html4colors = array("black", "silver", "gray", "white",
108             "maroon", "red", "purple", "fuchsia",
109             "green", "lime", "olive", "yellow",
110             "navy", "blue", "teal", "aqua");
111         /* max(strlen("fuchsia"), strlen("#00FF00"), ... ) = 7 */
112         static $MAXLEN = 7;
113         foreach (array($string, $comment, $keyword, $bg, $default, $html) as $color) {
114             $length = strlen($color);
115             //trigger_error(sprintf(_("DEBUG: color “%s” is length %d."), $color, $length), E_USER_NOTICE);
116             if (($length == 7 || $length == 4) && substr($color, 0, 1) == "#"
117                 && "#" == preg_replace("/[a-fA-F0-9]/", "", $color)
118             ) {
119                 //trigger_error(sprintf(_("DEBUG: color “%s” appears to be hex."), $color), E_USER_NOTICE);
120                 // stop checking, ok to go
121             } elseif (($length < $MAXLEN + 1) && in_array($color, $html4colors)) {
122                 //trigger_error(sprintf(_("DEBUG color “%s” appears to be an HTML 4 color."), $color), E_USER_NOTICE);
123                 // stop checking, ok to go
124             } else {
125                 trigger_error(sprintf(_("Invalid color: %s"),
126                     $color), E_USER_NOTICE);
127                 // FIXME: also change color to something valid like "black" or ini_get("highlight.xxx")
128             }
129         }
130     }
131
132     function set_colors($string, $comment, $keyword, $bg, $default, $html)
133     {
134         // set highlight colors
135         $this->oldstring = ini_set('highlight.string', $string);
136         $this->oldcomment = ini_set('highlight.comment', $comment);
137         $this->oldkeyword = ini_set('highlight.keyword', $keyword);
138         if (version_compare(PHP_VERSION, '5.4', '<')) {
139             $this->oldbg = ini_set('highlight.bg', $bg);
140         }
141         $this->olddefault = ini_set('highlight.default', $default);
142         $this->oldhtml = ini_set('highlight.html', $html);
143     }
144
145     function restore_colors()
146     {
147         // restore previous default highlight colors
148         ini_set('highlight.string', $this->oldstring);
149         ini_set('highlight.comment', $this->oldcomment);
150         ini_set('highlight.keyword', $this->oldkeyword);
151         if (version_compare(PHP_VERSION, '5.4', '<')) {
152             ini_set('highlight.bg', $this->oldbg);
153         }
154         ini_set('highlight.default', $this->olddefault);
155         ini_set('highlight.html', $this->oldhtml);
156     }
157
158 }
159
160 // Local Variables:
161 // mode: php
162 // tab-width: 8
163 // c-basic-offset: 4
164 // c-hanging-comment-ender-p: nil
165 // indent-tabs-mode: nil
166 // End: