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