cssgen.php 14 KB

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