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