Documentation is available at cssgen.php
1 <?php
2 /***********************************************************************************
3 * cssgen.php
4 * ----------
5 * Author: Nigel McNie (oracle.shinoda@gmail.com)
6 * Copyright: (c) 2004 Nigel McNie
7 * Release Version: 1.0.7.3
8 * CVS Revision Version: $Revision: 1.1 $
9 * Date Started: 2004/05/20
10 * Last Modified: $Date: 2006/01/23 09:27:28 $
11 *
12 * Application to generate custom CSS files for GeSHi (based on an idea by Andreas
13 * Gohr)
14 *
15 ************************************************************************************
16 *
17 * This file is part of GeSHi.
18 *
19 * GeSHi is free software; you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation; either version 2 of the License, or
22 * (at your option) any later version.
23 *
24 * GeSHi is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
28 *
29 * You should have received a copy of the GNU General Public License
30 * along with GeSHi; if not, write to the Free Software
31 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
32 *
33 ************************************************************************************/
33
34
35 set_magic_quotes_runtime(0);
36 //
37 // Functions
38 //
39
40 function make_header ( $title )
41 {
42 echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
43 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
44 <head>
45 <title>GeSHi CSS Generator :: ' . $title . ' </title>
46 <style type="text/css" media="screen">
47 <!--
48 html {
49 font-family: Verdana, Arial, sans-serif;
50 font-size: 80%;
51 background-color: #d0d0d0;
52 }
53 body {
54 margin: 10px;
55 padding: 5px;
56 border: 1px solid #f0f0f0;
57 background-color: #f6f6f6;
58 }
59 h1 {
60 border-bottom: 2px solid #e0e0e0;
61 font-weight: normal;
62 font-size: 150%;
63 color: #c0c0c0;
64 }
65 input, textarea {
66 border: 1px solid #d0d0d0;
67 }
68 th {
69 text-align: right;
70 font-weight: normal;
71 }
72 pre {
73 font-size: 110%;
74 color: #202020;
75 }
76 #footer {
77 color: #b0b0b0;
78 text-align: center;
79 font-size: 90%;
80 margin: 0 auto;
81 border-top: 1px solid #e0e0e0;
82 }
83 #footer a {
84 color: #c0c0c0;
85 }
86 -->
87 </style>
88 <script type="text/javascript">
89 function select (state)
90 {
91 var cboxes = document.getElementsByTagName(\'input\');
92 for (var i = 0; i < cboxes.length; i++) {
93 if (cboxes[i].type == "checkbox") {
94 if (state == "true") {
95 cboxes[i].checked = true;
96 } else if (state == "false") {
97 cboxes[i].checked = false;
98 } else if (state == "invert") {
99 cboxes[i].checked = !cboxes[i].checked;
100 }
101 }
102 }
103 }
104 </script>
105 </head>
106 <body>
107 <h1>' . $title . '</h1>
108 ';
109 }
110
111 function make_footer ()
112 {
113 echo '<div id="footer"><a href="http://qbnz.com/highlighter/">GeSHi</a> © Nigel McNie, 2004, released under the GPL</div></body>
114 </html>';
115 }
116
117
118 function get_var ( $var_name )
119 {
120 if ( isset($_GET[$var_name]) )
121 {
122 return str_replace("\'", "'", $_GET[$var_name]);
123 }
124 elseif ( isset($_POST[$var_name]) )
125 {
126 return str_replace("\'", "'", $_POST[$var_name]);
127 }
128 return null;
129 }
130
131
132
133 //
134 // Unset everything
135 //
136 foreach ( $_REQUEST as $var )
137 {
138 unset($$var);
139 }
140 foreach ( array(
141 '_POST' => 'HTTP_POST_VARS',
142 '_GET' => 'HTTP_GET_VARS',
143 '_COOKIE' => 'HTTP_COOKIE_VARS',
144 '_SERVER' => 'HTTP_SERVER_VARS',
145 '_ENV' => 'HTTP_ENV_VARS',
146 '_FILES' => 'HTTP_POST_FILES') as $array => $other )
147 {
148 if ( !isset($$array) )
149 {
150 $$array = $$other;
151 }
152 unset($$other);
153 }
154
155
156 // Get what step we're up to
157 $step = get_var('step');
158
159 if ( !$step || $step == 1 )
160 {
161 $errors = 0;
162 make_header('Step 1');
163 echo "Welcome to the GeSHi CSS generator.<br /><pre>Searching for GeSHi... ";
164
165 // Find GeSHi
166 $geshi_path = get_var('geshi-path');
167 $geshi_lang_path = get_var('geshi-lang-path');
168
169 if ( !$geshi_path )
170 {
171 $geshi_path = '../geshi.php';
172 }
173 if ( !$geshi_lang_path )
174 {
175 $geshi_lang_path = '../geshi/';
176 }
177
178
179 if ( is_file($geshi_path) && is_readable($geshi_path) )
180 {
181 // Get file contents and see if GeSHi is in here
182 $file = @file($geshi_path);
183 $contents = '';
184 foreach ( $file as $line )
185 {
186 $contents .= $line;
187 }
188 if ( strpos($contents, '<?php
189 /**
190 * GeSHi - Generic Syntax Highlighter
191 *
192 * The GeSHi class for Generic Syntax Highlighting. Please refer to the documentation
193 * at http://qbnz.com/highlighter/documentation.php for more information about how to
194 * use this class.') !== false )
195 {
196 echo '<span style="color: green;">Found at ' . realpath($geshi_path) . '</span>';
197 }
198 else
199 {
200 ++$errors;
201 $no_geshi_dot_php_error = true;
202 echo '<span style="color: red;">Not found</span>';
203 }
204 }
205 else
206 {
207 ++$errors;
208 $no_geshi_dot_php_error = true;
209 echo '<span style="color: red;">Not found</span>';
210 }
211
212 // Find language files
213 echo "\nSearching for language files... ";
214 if ( is_readable($geshi_lang_path . 'css-gen.cfg') )
215 {
216
217 echo '<span style="color: green;">Found at ' . realpath($geshi_lang_path) . '</span>';
218 }
219 else
220 {
221 ++$errors;
222 $no_lang_dir_error = true;
223 echo '<span style="color: red;">Not found</span>';
224 }
225 echo "</pre>\n";
226
227 if ( $errors > 0 )
228 {
229 // We're gonna have to ask for the paths...
230 echo 'Unfortunately CSSGen could not detect the following paths. Please input them and press "submit" to try again.';
231 echo "
232 <form action=\"cssgen.php\" method=\"post\">";
233 if ( $no_geshi_dot_php_error )
234 {
235 echo "
236 <br />geshi.php: <input type=\"text\" name=\"geshi-path\" value=\"" . realpath('../geshi.php') . "\" size=\"50\" />";
237 }
238 else
239 {
240 echo '<input type="hidden" name="geshi-path" value="' . htmlspecialchars($geshi_path) . '" />';
241 }
242 if ( $no_lang_dir_error )
243 {
244 echo "
245 <br />language files directory: <input type=\"text\" name=\"geshi-lang-path\" value=\"" . realpath('../geshi/') . "/\" size=\"50\" /> (should have a trailing slash)";
246 }
247 else
248 {
249 echo '<input type="hidden" name="geshi-lang-path" value="' . $geshi_lang_path . '" />';
250 }
251
252 echo "
253 <br /><input type=\"submit\" value=\"Search\" /></form>";
254 }
255 else
256 {
257 // no errors - echo continue form
258 echo 'Everything seems to be detected successfully. Use the button to continue.
259 <br /><br /><form action="cssgen.php?step=2" method="post">
260 <input type="hidden" name="geshi-path" value="' . realpath($geshi_path) . '" /><input type="hidden" name="geshi-lang-path" value="' . realpath($geshi_lang_path) . '" />
261 <input type="submit" value="Step 2" />';
262 }
263
264 make_footer();
265 }
266 // Step 2
267 elseif ( $step == 2 )
268 {
269 make_header('Step 2');
270
271 $geshi_path = get_var('geshi-path');
272 $geshi_lang_path = get_var('geshi-lang-path');
273
274 $dh = opendir($geshi_lang_path);
275 $lang_files = array();
276 $file = readdir($dh);
277 while ( $file !== false )
278 {
279 if ( $file == '.' || $file == '..' || $file == 'CVS' || $file == 'css-gen.cfg' )
280 {
281 $file = readdir($dh);
282 continue;
283 }
284 $lang_files[] = $file;
285 $file = readdir($dh);
286 }
287 closedir($dh);
288 sort($lang_files);
289
290 // Now installed languages are in $lang_files
291
292 echo '<form action="cssgen.php?step=3" method="post" id="step2">
293 What languages are you wanting to make this stylesheet for?<br /><br />
294 Detected languages:<br />';
295
296 foreach ( $lang_files as $lang )
297 {
298 $lang = substr($lang, 0, strpos($lang, '.'));
299 echo "<input type=\"checkbox\" name=\"langs[$lang]\" checked=\"checked\" /> $lang<br />\n";
300 }
301
302 echo "Select: <a href=\"javascript:select('true')\">All</a>, <a href=\"javascript:select('false')\">None</a>, <a href=\"javascript:select('invert')\">Invert</a><br />\n";
303
304 echo 'If you\'d like any other languages not detected here to be supported, please enter
305 them here, one per line:<br /><textarea rows="4" cols="20" name="extra-langs"></textarea><br />
306 ';
307
308 echo '<br />Styles:
309 <table>
310 <tr><th>Style for the overall code block:</th><td><input type="text" name="overall" value="border: 1px dotted #a0a0a0; font-family: \'Courier New\', Courier, monospace; background-color: #f0f0f0; color: #0000bb;" /></td></tr>
311 <tr><th>Default Styles</th><td><input type="text" name="default-styles" value="font-weight:normal;background:transparent;color:#000; padding-left: 5px;" /></td></tr>
312 <tr><th>Keywords I (if, do, while etc)</th><td><input type="text" name="keywords-1" value="color: #a1a100;" /></td></tr>
313 <tr><th>Keywords II (null, true, false etc)</th><td><input type="text" name="keywords-2" value="color: #000; font-weight: bold;" /></td></tr>
314 <tr><th>Inbuilt Functions (echo, print etc)</th><td><input type="text" name="keywords-3" value="color: #000066;" /></td></tr>
315 <tr><th>Data Types (int, boolean etc)</th><td><input type="text" name="keywords-4" value="color: #f63333;" /></td></tr>
316
317 <tr><th>Comments (//, <!-- --> etc)</th><td><input type="text" name="comments" value="color: #808080;" /></td></tr>
318 <tr><th>Escaped Characters (\n, \t etc)</th><td><input type="text" name="escaped-chars" value="color: #000033; font-weight: bold;" /></td></tr>
319 <tr><th>Brackets ( ([{}]) etc)</th><td><input type="text" name="brackets" value="color: #66cc66;" /></td></tr>
320 <tr><th>Strings ("foo" etc)</th><td><input type="text" name="strings" value="color: #ff0000;" /></td></tr>
321 <tr><th>Numbers (1, -54, 2.5 etc)</th><td><input type="text" name="numbers" value="color: #ff33ff;" /></td></tr>
322 <tr><th>Methods (Foo.bar() etc)</th><td><input type="text" name="methods" value="color: #006600;" /></td></tr>
323 </table>';
324
325 echo '<input type="hidden" name="geshi-path" value="' . realpath($geshi_path) . '" /><input type="hidden" name="geshi-lang-path" value="' . realpath($geshi_lang_path) . '" />
326 <input type="submit" value="Step 3" /></form>';
327
328 make_footer();
329 }
330 // Step 3
331 elseif ( $step == 3 )
332 {
333 make_header('Step 3');
334 echo '<p>Here is your completed stylesheet. Note that it may not be perfect - no regular expression styles are included for one thing,
335 you\'ll have to add those yourself (php and xml are just two languages that use them), and line numbers are not included, however
336 it includes most of the basic information.</p>';
337
338 // Make the stylesheet
339 $part_selector_1 = '';
340 $part_selector_2 = '';
341 $part_selector_3 = '';
342
343 $langs = get_var('langs');
344 $extra_langs = trim(get_var('extra-langs'));
345 if ( $extra_langs != '' )
346 {
347 $l = explode("\r\n", $extra_langs);
348 foreach ( $l as $lng )
349 {
350 $langs[$lng] = true;
351 }
352 }
353
354
355 foreach ( $langs as $lang => $dummy )
356 {
357 $part_selector_1 .= ".$lang {PART}, ";
358 $part_selector_2 .= ".$lang {PART1}, .$lang {PART2}, ";
359 $part_selector_3 .= ".$lang {PART1}, .$lang {PART2}, .$lang {PART3}, ";
360 }
361 $part_selector_1 = substr($part_selector_1, 0, -2);
362 $part_selector_2 = substr($part_selector_2, 0, -2);
363 $part_selector_3 = substr($part_selector_3, 0, -2);
364
365
366 $default_styles = get_var('default-styles');
367 $ol_selector = str_replace('{PART}', 'ol', $part_selector_1);
368 $overall_styles = get_var('overall');
369 $overall_selector = str_replace('{PART}', '', $part_selector_1);
370
371 $stylesheet = "/* GeSHi (c) Nigel McNie 2004 (http://qbnz.com/highlighter) */";
372
373 if ( $overall != '' )
374 {
375 $stylesheet .= "\n$overall_selector {{$overall_styles}}";
376 }
377 if ( $default_styles != '' )
378 {
379 $default_selector = str_replace(array('{PART1}', '{PART2}'), array('.de1', '.de2'), $part_selector_2);
380 $stylesheet .= "\n$default_selector {{$default_styles}}";
381 }
382
383 // Do keywords
384 $keywords_1 = get_var('keywords-1');
385 $keyword_selector_1 = str_replace('{PART}', '.kw1', $part_selector_1);
386 if ( $keywords_1 != '' )
387 {
388 $stylesheet .= "\n$keyword_selector_1 {{$keywords_1}}";
389 }
390
391 $keywords_2 = get_var('keywords-2');
392 $keyword_selector_2 = str_replace('{PART}', '.kw2', $part_selector_1);
393 if ( $keywords_2 != '' )
394 {
395 $stylesheet .= "\n$keyword_selector_2 {{$keywords_2}}";
396 }
397
398 $keywords_3 = get_var('keywords-3');
399 $keyword_selector_3 = str_replace('{PART}', '.kw3', $part_selector_1);
400 if ( $keywords_3 != '' )
401 {
402 $stylesheet .= "\n$keyword_selector_3 {{$keywords_3}}";
403 }
404
405 $keywords_4 = get_var('keywords-4');
406 $keyword_selector_4 = str_replace('{PART}', '.kw4', $part_selector_1);
407 if ( $keywords_4 != '' )
408 {
409 $stylesheet .= "\n$keyword_selector_4 {{$keywords_4}}";
410 }
411
412 // Do other lexics
413 $comments = get_var('comments');
414 $comment_selector = str_replace(array('{PART1}', '{PART2}', '{PART3}'), array('.co1', '.co2', '.coMULTI'), $part_selector_3);
415 if ( $comments != '' )
416 {
417 $stylesheet .= "\n$comment_selector {{$comments}}";
418 }
419
420 $esc = get_var('escaped-chars');
421 $esc_selector = str_replace('{PART}', '.es0', $part_selector_1);
422 if ( $esc != '' )
423 {
424 $stylesheet .= "\n$esc_selector {{$esc}}";
425 }
426
427 $brackets = get_var('brackets');
428 $brk_selector = str_replace('{PART}', '.br0', $part_selector_1);
429 if ( $brackets != '' )
430 {
431 $stylesheet .= "\n$brk_selector {{$brackets}}";
432 }
433
434 $strings = get_var('strings');
435 $string_selector = str_replace('{PART}', '.st0', $part_selector_1);
436 if ( $strings != '' )
437 {
438 $stylesheet .= "\n$string_selector {{$strings}}";
439 }
440
441 $numbers = get_var('numbers');
442 $num_selector = str_replace('{PART}', '.nu0', $part_selector_1);
443 if ( $numbers != '' )
444 {
445 $stylesheet .= "\n$num_selector {{$numbers}}";
446 }
447
448 $methods = get_var('methods');
449 $method_selector = str_replace('{PART}', '.me0', $part_selector_1);
450 if ( $methods != '' )
451 {
452 $stylesheet .= "\n$method_selector {{$methods}}";
453 }
454
455 echo "<pre>$stylesheet</pre>";
456
457 make_footer();
458 }
459
460 ?>
Documentation generated on Thu, 22 Sep 2005 13:47:51 +1200 by phpDocumentor 1.2.3