cssgen.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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. } elseif (state == "false") {
  93. cboxes[i].checked = false;
  94. } elseif (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(strstr($geshi_path, '..')) {
  155. unset($geshi_path);
  156. }
  157. if(strstr($geshi_lang_path, '..')) {
  158. unset($geshi_lang_path);
  159. }
  160. if ( !$geshi_path )
  161. {
  162. $geshi_path = '../geshi.php';
  163. }
  164. if ( !$geshi_lang_path )
  165. {
  166. $geshi_lang_path = '../geshi/';
  167. }
  168. if ( is_file($geshi_path) && is_readable($geshi_path) )
  169. {
  170. // Get file contents and see if GeSHi is in here
  171. $file = @file($geshi_path);
  172. $contents = '';
  173. foreach ( $file as $line )
  174. {
  175. $contents .= $line;
  176. }
  177. if ( strpos($contents, '<?php
  178. /**
  179. * GeSHi - Generic Syntax Highlighter') !== false )
  180. {
  181. echo '<span style="color: green;">Found at ' . realpath($geshi_path) . '</span>';
  182. }
  183. else
  184. {
  185. ++$errors;
  186. $no_geshi_dot_php_error = true;
  187. echo '<span style="color: red;">Not found</span>';
  188. }
  189. }
  190. else
  191. {
  192. ++$errors;
  193. $no_geshi_dot_php_error = true;
  194. echo '<span style="color: red;">Not found</span>';
  195. }
  196. // Find language files
  197. echo "\nSearching for language files... ";
  198. if ( is_readable($geshi_lang_path . 'css-gen.cfg') )
  199. {
  200. echo '<span style="color: green;">Found at ' . realpath($geshi_lang_path) . '</span>';
  201. }
  202. else
  203. {
  204. ++$errors;
  205. $no_lang_dir_error = true;
  206. echo '<span style="color: red;">Not found</span>';
  207. }
  208. echo "</pre>\n";
  209. if ( $errors > 0 )
  210. {
  211. // We're gonna have to ask for the paths...
  212. echo 'Unfortunately CSSGen could not detect the following paths. Please input them and press &quot;submit&quot; to try again.';
  213. echo "
  214. <form action=\"cssgen.php\" method=\"post\">";
  215. if ( $no_geshi_dot_php_error )
  216. {
  217. echo "
  218. <br />geshi.php: <input type=\"text\" name=\"geshi-path\" value=\"" . realpath('../geshi.php') . "\" size=\"50\" />";
  219. }
  220. else
  221. {
  222. echo '<input type="hidden" name="geshi-path" value="' . htmlspecialchars($geshi_path) . '" />';
  223. }
  224. if ( $no_lang_dir_error )
  225. {
  226. echo "
  227. <br />language files directory: <input type=\"text\" name=\"geshi-lang-path\" value=\"" . realpath('../geshi/') . "/\" size=\"50\" /> (should have a trailing slash)";
  228. }
  229. else
  230. {
  231. echo '<input type="hidden" name="geshi-lang-path" value="' . $geshi_lang_path . '" />';
  232. }
  233. echo "
  234. <br /><input type=\"submit\" value=\"Search\" /></form>";
  235. }
  236. else
  237. {
  238. // no errors - echo continue form
  239. echo 'Everything seems to be detected successfully. Use the button to continue.
  240. <br /><br /><form action="cssgen.php?step=2" method="post">
  241. <input type="hidden" name="geshi-path" value="' . realpath($geshi_path) . '" /><input type="hidden" name="geshi-lang-path" value="' . realpath($geshi_lang_path) . '" />
  242. <input type="submit" value="Step 2" />';
  243. }
  244. make_footer();
  245. }
  246. // Step 2
  247. elseif ( $step == 2 )
  248. {
  249. make_header('Step 2');
  250. $geshi_path = get_var('geshi-path');
  251. $geshi_lang_path = get_var('geshi-lang-path');
  252. $dh = opendir($geshi_lang_path);
  253. $lang_files = array();
  254. $file = readdir($dh);
  255. while ( $file !== false )
  256. {
  257. if ( $file == '.' || $file == '..' || $file == 'CVS' || $file == 'css-gen.cfg' )
  258. {
  259. $file = readdir($dh);
  260. continue;
  261. }
  262. if(!strstr(file_get_contents($dh . DIRECTORY_SEPARATOR . $file), '$language_data')) {
  263. $file = readdir($dh);
  264. continue;
  265. }
  266. $lang_files[] = $file;
  267. $file = readdir($dh);
  268. }
  269. closedir($dh);
  270. sort($lang_files);
  271. // Now installed languages are in $lang_files
  272. echo '<form action="cssgen.php?step=3" method="post" id="step2">
  273. What languages are you wanting to make this stylesheet for?<br /><br />
  274. Detected languages:<br />';
  275. foreach ( $lang_files as $lang )
  276. {
  277. $lang = substr($lang, 0, strpos($lang, '.'));
  278. if ($lang) {
  279. echo "<input type=\"checkbox\" name=\"langs[$lang]\" checked=\"checked\" />&nbsp;$lang<br />\n";
  280. }
  281. }
  282. 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";
  283. echo 'If you\'d like any other languages not detected here to be supported, please enter
  284. them here, one per line:<br /><textarea rows="4" cols="20" name="extra-langs"></textarea><br />
  285. ';
  286. echo '<br />Styles:
  287. <table>
  288. <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>
  289. <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>
  290. <tr><th>Keywords I (if, do, while etc)</th><td><input type="text" name="keywords-1" value="color: #a1a100;" /></td></tr>
  291. <tr><th>Keywords II (null, true, false etc)</th><td><input type="text" name="keywords-2" value="color: #000; font-weight: bold;" /></td></tr>
  292. <tr><th>Inbuilt Functions (echo, print etc)</th><td><input type="text" name="keywords-3" value="color: #000066;" /></td></tr>
  293. <tr><th>Data Types (int, boolean etc)</th><td><input type="text" name="keywords-4" value="color: #f63333;" /></td></tr>
  294. <tr><th>Comments (//, <!-- --> etc)</th><td><input type="text" name="comments" value="color: #808080;" /></td></tr>
  295. <tr><th>Escaped Characters (\n, \t etc)</th><td><input type="text" name="escaped-chars" value="color: #000033; font-weight: bold;" /></td></tr>
  296. <tr><th>Brackets ( ([{}]) etc)</th><td><input type="text" name="brackets" value="color: #66cc66;" /></td></tr>
  297. <tr><th>Strings ("foo" etc)</th><td><input type="text" name="strings" value="color: #ff0000;" /></td></tr>
  298. <tr><th>Numbers (1, -54, 2.5 etc)</th><td><input type="text" name="numbers" value="color: #ff33ff;" /></td></tr>
  299. <tr><th>Methods (Foo.bar() etc)</th><td><input type="text" name="methods" value="color: #006600;" /></td></tr>
  300. </table>';
  301. echo '<input type="hidden" name="geshi-path" value="' . realpath($geshi_path) . '" /><input type="hidden" name="geshi-lang-path" value="' . realpath($geshi_lang_path) . '" />
  302. <input type="submit" value="Step 3" /></form>';
  303. make_footer();
  304. }
  305. // Step 3
  306. elseif ( $step == 3 )
  307. {
  308. make_header('Step 3');
  309. echo '<p>Here is your completed stylesheet. Note that it may not be perfect - no regular expression styles are included for one thing,
  310. you\'ll have to add those yourself (php and xml are just two languages that use them), and line numbers are not included, however
  311. it includes most of the basic information.</p>';
  312. // Make the stylesheet
  313. $part_selector_1 = '';
  314. $part_selector_2 = '';
  315. $part_selector_3 = '';
  316. $langs = get_var('langs');
  317. $extra_langs = trim(get_var('extra-langs'));
  318. if ( $extra_langs != '' )
  319. {
  320. $l = explode("\r\n", $extra_langs);
  321. foreach ( $l as $lng )
  322. {
  323. $langs[$lng] = true;
  324. }
  325. }
  326. foreach ( $langs as $lang => $dummy )
  327. {
  328. $part_selector_1 .= ".$lang {PART}, ";
  329. $part_selector_2 .= ".$lang {PART1}, .$lang {PART2}, ";
  330. $part_selector_3 .= ".$lang {PART1}, .$lang {PART2}, .$lang {PART3}, ";
  331. }
  332. $part_selector_1 = substr($part_selector_1, 0, -2);
  333. $part_selector_2 = substr($part_selector_2, 0, -2);
  334. $part_selector_3 = substr($part_selector_3, 0, -2);
  335. $default_styles = get_var('default-styles');
  336. $ol_selector = str_replace('{PART}', 'ol', $part_selector_1);
  337. $overall_styles = get_var('overall');
  338. $overall_selector = str_replace('{PART}', '', $part_selector_1);
  339. $stylesheet = "/* GeSHi (c) Nigel McNie 2004 (http://qbnz.com/highlighter) */";
  340. if ( $overall != '' )
  341. {
  342. $stylesheet .= "\n$overall_selector {{$overall_styles}}";
  343. }
  344. if ( $default_styles != '' )
  345. {
  346. $default_selector = str_replace(array('{PART1}', '{PART2}'), array('.de1', '.de2'), $part_selector_2);
  347. $stylesheet .= "\n$default_selector {{$default_styles}}";
  348. }
  349. // Do keywords
  350. $keywords_1 = get_var('keywords-1');
  351. $keyword_selector_1 = str_replace('{PART}', '.kw1', $part_selector_1);
  352. if ( $keywords_1 != '' )
  353. {
  354. $stylesheet .= "\n$keyword_selector_1 {{$keywords_1}}";
  355. }
  356. $keywords_2 = get_var('keywords-2');
  357. $keyword_selector_2 = str_replace('{PART}', '.kw2', $part_selector_1);
  358. if ( $keywords_2 != '' )
  359. {
  360. $stylesheet .= "\n$keyword_selector_2 {{$keywords_2}}";
  361. }
  362. $keywords_3 = get_var('keywords-3');
  363. $keyword_selector_3 = str_replace('{PART}', '.kw3', $part_selector_1);
  364. if ( $keywords_3 != '' )
  365. {
  366. $stylesheet .= "\n$keyword_selector_3 {{$keywords_3}}";
  367. }
  368. $keywords_4 = get_var('keywords-4');
  369. $keyword_selector_4 = str_replace('{PART}', '.kw4', $part_selector_1);
  370. if ( $keywords_4 != '' )
  371. {
  372. $stylesheet .= "\n$keyword_selector_4 {{$keywords_4}}";
  373. }
  374. // Do other lexics
  375. $comments = get_var('comments');
  376. $comment_selector = str_replace(array('{PART1}', '{PART2}', '{PART3}'), array('.co1', '.co2', '.coMULTI'), $part_selector_3);
  377. if ( $comments != '' )
  378. {
  379. $stylesheet .= "\n$comment_selector {{$comments}}";
  380. }
  381. $esc = get_var('escaped-chars');
  382. $esc_selector = str_replace('{PART}', '.es0', $part_selector_1);
  383. if ( $esc != '' )
  384. {
  385. $stylesheet .= "\n$esc_selector {{$esc}}";
  386. }
  387. $brackets = get_var('brackets');
  388. $brk_selector = str_replace('{PART}', '.br0', $part_selector_1);
  389. if ( $brackets != '' )
  390. {
  391. $stylesheet .= "\n$brk_selector {{$brackets}}";
  392. }
  393. $strings = get_var('strings');
  394. $string_selector = str_replace('{PART}', '.st0', $part_selector_1);
  395. if ( $strings != '' )
  396. {
  397. $stylesheet .= "\n$string_selector {{$strings}}";
  398. }
  399. $numbers = get_var('numbers');
  400. $num_selector = str_replace('{PART}', '.nu0', $part_selector_1);
  401. if ( $numbers != '' )
  402. {
  403. $stylesheet .= "\n$num_selector {{$numbers}}";
  404. }
  405. $methods = get_var('methods');
  406. $method_selector = str_replace('{PART}', '.me0', $part_selector_1);
  407. if ( $methods != '' )
  408. {
  409. $stylesheet .= "\n$method_selector {{$methods}}";
  410. }
  411. echo "<pre>$stylesheet</pre>";
  412. make_footer();
  413. }
  414. ?>