]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/PhpHighlight.php
Add error message: Please provide source code to PhpHighlight plugin
[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 it's
26  * arguments to pretty-print PHP code.
27  *
28  * Usage:
29  * <<PhpHighlight default='#FF0000' comment='#0000CC'
30  * code that should be highlighted
31  * >>
32  *
33  * You do not have to add '<?php' and '?>' to the code - the plugin
34  * does this automatically if you do not set wrap to 0.
35  *
36  * If you do set wrap to 0, then you'll have to start and stop PHP
37  * mode in the source yourself, or you wont see any highlighting. But
38  * you cannot use '<?php' and '?>' in the source, because this
39  * interferes with PhpWiki, you'll have use '< ?php' and '? >'
40  * instead.
41  *
42  * Author: Martin Geisler <gimpster@gimpster.com>.
43  *
44  * Added ability to override colors defined in php.ini --Carsten Klapp
45  *
46  * Known Problems:
47  *   <<PhpHighlight
48  *   testing[somearray];
49  *   testing~[badworkaround~];
50  *   >>
51  * will swallow "[somearray]"
52  */
53
54 class WikiPlugin_PhpHighlight
55     extends WikiPlugin
56 {
57     // Four required functions in a WikiPlugin.
58
59     function getName()
60     {
61         return _("PhpHighlight");
62     }
63
64     function getDescription()
65     {
66         return _("PHP syntax highlighting");
67     }
68
69     // Establish default values for each of this plugin's arguments.
70     function getDefaultArguments()
71     {
72         // TODO: results of ini_get() should be static for multiple
73         // invocations of plugin on one WikiPage
74         return array('wrap' => true,
75             'string' => ini_get("highlight.string"), //'#00CC00',
76             'comment' => ini_get("highlight.comment"), //'#FF9900',
77             'keyword' => ini_get("highlight.keyword"), //'#006600',
78             'bg' => ini_get("highlight.bg"), //'#FFFFFF',
79             'default' => ini_get("highlight.default"), //'#0000CC',
80             'html' => ini_get("highlight.html") //'#000000'
81         );
82     }
83
84     function run($dbi, $argstr, &$request, $basepage)
85     {
86
87         extract($this->getArgs($argstr, $request));
88         $source =& $this->source;
89         if (empty($source)) {
90             return HTML::div(array('class' => "error"),
91                    "Please provide source code to PhpHighlight plugin");
92         }
93
94         $this->sanify_colors($string, $comment, $keyword, $bg, $default, $html);
95         $this->set_colors($string, $comment, $keyword, $bg, $default, $html);
96
97         if ($wrap) {
98             /* Wrap with "<?php\n" and "\n?>" required by highlight_string(): */
99             $source = "<?php\n" . $source . "\n?>";
100         } else {
101             $source = str_replace(array('< ?php', '? >'),
102                 array('<?php', '?>'), $source);
103         }
104
105         $str = highlight_string($source, true);
106
107         if ($wrap) {
108             /* Remove "<?php\n" and "\n?>" again: */
109             $str = str_replace(array('&lt;?php<br />', '?&gt;'), '', $str);
110         }
111
112         /* Remove empty span tags. */
113         foreach (array($string, $comment, $keyword, $bg, $default, $html) as $color) {
114             $search = "<span style=\"color: $color\"></span>";
115             $str = str_replace($search, '', $str);
116         }
117
118         /* restore default colors in case of multiple invocations of this plugin on one page */
119         $this->restore_colors();
120         return new RawXml($str);
121     }
122
123     function handle_plugin_args_cruft(&$argstr, &$args)
124     {
125         $this->source = $argstr;
126     }
127
128     /**
129      * Make sure color argument is valid
130      * See http://www.w3.org/TR/REC-html40/types.html#h-6.5
131      */
132     function sanify_colors($string, $comment, $keyword, $bg, $default, $html)
133     {
134         static $html4colors = array("black", "silver", "gray", "white",
135             "maroon", "red", "purple", "fuchsia",
136             "green", "lime", "olive", "yellow",
137             "navy", "blue", "teal", "aqua");
138         /* max(strlen("fuchsia"), strlen("#00FF00"), ... ) = 7 */
139         static $MAXLEN = 7;
140         foreach (array($string, $comment, $keyword, $bg, $default, $html) as $color) {
141             $length = strlen($color);
142             //trigger_error(sprintf(_("DEBUG: color '%s' is length %d."), $color, $length), E_USER_NOTICE);
143             if (($length == 7 || $length == 4) && substr($color, 0, 1) == "#"
144                 && "#" == preg_replace("/[a-fA-F0-9]/", "", $color)
145             ) {
146                 //trigger_error(sprintf(_("DEBUG: color '%s' appears to be hex."), $color), E_USER_NOTICE);
147                 // stop checking, ok to go
148             } elseif (($length < $MAXLEN + 1) && in_array($color, $html4colors)) {
149                 //trigger_error(sprintf(_("DEBUG color '%s' appears to be an HTML 4 color."), $color), E_USER_NOTICE);
150                 // stop checking, ok to go
151             } else {
152                 trigger_error(sprintf(_("Invalid color: %s"),
153                     $color), E_USER_NOTICE);
154                 // FIXME: also change color to something valid like "black" or ini_get("highlight.xxx")
155             }
156         }
157     }
158
159     function set_colors($string, $comment, $keyword, $bg, $default, $html)
160     {
161         // set highlight colors
162         $this->oldstring = ini_set('highlight.string', $string);
163         $this->oldcomment = ini_set('highlight.comment', $comment);
164         $this->oldkeyword = ini_set('highlight.keyword', $keyword);
165         $this->oldbg = ini_set('highlight.bg', $bg);
166         $this->olddefault = ini_set('highlight.default', $default);
167         $this->oldhtml = ini_set('highlight.html', $html);
168     }
169
170     function restore_colors()
171     {
172         // restore previous default highlight colors
173         ini_set('highlight.string', $this->oldstring);
174         ini_set('highlight.comment', $this->oldcomment);
175         ini_set('highlight.keyword', $this->oldkeyword);
176         ini_set('highlight.bg', $this->oldbg);
177         ini_set('highlight.default', $this->olddefault);
178         ini_set('highlight.html', $this->oldhtml);
179     }
180
181 }
182
183 // Local Variables:
184 // mode: php
185 // tab-width: 8
186 // c-basic-offset: 4
187 // c-hanging-comment-ender-p: nil
188 // indent-tabs-mode: nil
189 // End: