# Source for file example.php

Documentation is available at example.php

    1     <?php
    2
    3     /**
    4      * GeSHi example script
    5      * --------------------
    6      * Just point your browser at this script (with geshi.php in the parent directory,
    7      * and the language files in subdirectory "../geshi/")
    8      */
    9
    10    if (!@include '../geshi.php') {
    11        if (!@include 'geshi.php') {
    12            die('Could not find geshi.php - make sure it is in your include path!');
    13        } else {
    14            $path = './';
    15        }
    16    } else {
    17        $path = '../';
    18    }
    19
    20    if ( isset($_POST['submit']) )
    21    {
    22        if ( get_magic_quotes_gpc() ) $_POST['source'] = stripslashes($_POST['source']);
    23        if ( !strlen(trim($_POST['source'])) )
    24        {
    25            $_POST['language'] = preg_replace('#[^a-zA-Z0-9\-_]#', '', $_POST['language']);
    26            $_POST['source'] = implode('', @file($path . 'geshi/' . $_POST['language'] . '.php'));
    27            $_POST['language'] = 'php';
    28        }
    29        $geshi = new GeSHi($_POST['source'], $_POST['language']);
    30        $geshi->set_header_type(GESHI_HEADER_PRE);
    31        $geshi->enable_classes();
    32        $geshi->enable_line_numbers(GESHI_FANCY_LINE_NUMBERS, 5);
    33        $geshi->set_overall_style('color: #000066; border: 1px solid #d0d0d0; background-color: #f0f0f0;', true);
    34        // Note the use of set_code_style to revert colours...
    35        $geshi->set_line_style('font: normal normal 95% \'Courier New\', Courier, monospace; color: #003030;', 'font-weight: bold; color: #006060;', true);
    36        $geshi->set_code_style('color: #000020;', 'color: #000020;');
    37
    38        $geshi->set_link_styles(GESHI_LINK, 'color: #000060;');
    39        $geshi->set_link_styles(GESHI_HOVER, 'background-color: #f0f000;');
    40
    41        $geshi->set_header_content('GeSHi &copy; 2004, Nigel McNie. View source of example.php for example of using GeSHi');
    42        $geshi->set_header_content_style('font-family: Verdana, Arial, sans-serif; color: #808080; font-size: 70%; font-weight: bold; background-color: #f0f0ff; border-bottom: 1px solid #d0d0d0; padding: 2px;');
    43
    44        $geshi->set_footer_content('Parsed in <TIME> seconds,  using GeSHi <VERSION>');
    45        $geshi->set_footer_content_style('font-family: Verdana, Arial, sans-serif; color: #808080; font-size: 70%; font-weight: bold; background-color: #f0f0ff; border-top: 1px solid #d0d0d0; padding: 2px;');
    46    }
    47    ?>
    48    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    49         "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    50    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    51    <head>
    52    	<title>GeSHi examples</title>
    53    	<style type="text/css">
    54    	<!--
    55    	<?php
    56        if ( isset($_POST['submit']) )
    57        {
    58            echo $geshi->get_stylesheet();
    59        }
    60        ?>
    61    	html {
    62    		background-color: #f0f0f0;
    63    	}
    64    	body {
    65    		font-family: Verdana, Arial, sans-serif;
    66    		margin: 10px;
    67    		border: 2px solid #e0e0e0;
    68    		background-color: #fcfcfc;
    69    		padding: 5px;
    70    	}
    71    	h2 {
    72    		margin: .1em 0 .2em .5em;
    73    		border-bottom: 1px solid #b0b0b0;
    74    		color: #b0b0b0;
    75    		font-weight: normal;
    76    		font-size: 150%;
    77    	}
    78    	h3 {
    79    		margin: .1em 0 .2em .5em;
    80    		color: #b0b0b0;
    81    		font-weight: normal;
    82    		font-size: 120%;
    83    	}
    84    	#footer {
    85    		text-align: center;
    86    		font-size: 80%;
    87    		color: #a9a9a9;
    88    	}
    89    	#footer a {
    90    		color: #9999ff;
    91    	}
    92    	textarea {
    93    		border: 1px solid #b0b0b0;
    94    		font-size: 90%;
    95    		color: #333;
    96    		margin-left: 20px;
    97    	}
    98    	select, input {
    99    		margin-left: 20px;
    100   	}
    101   	p {
    102   		font-size: 90%;
    103   		margin-left: .5em;
    104   	}
    105   	-->
    106   	</style>
    107   </head>
    108   <body>
    109   <h2>GeSHi Example Script</h2>
    110   <p>To use this script, make sure that <strong>geshi.php</strong> is in the parent directory, and that
    111   the language files are in a subdirectory of the parent directory called <strong>geshi/</strong></p>
    112   <p>Enter your source and a language to highlight the source in and submit, or just choose a language to
    113   have that language file highlighted in PHP.</p>
    114   <?php
    115   if ( isset($_POST['submit']) )
    116   {
    117       echo $geshi->parse_code();
    118       echo '<hr />';
    119   }
    120   ?>
    121   <form action="example.php" method="post">
    122   <h3>Source to highlight</h3>
    123   <textarea rows="10" cols="60" name="source"></textarea>
    124   <h3>Choose a language</h3>
    125   <select name="language">
    126   <?php
    127   if (!($dir = @opendir(dirname(__FILE__) . '/geshi'))) {
    128       if (!($dir = @opendir(dirname(__FILE__) . '/../geshi'))) {
    129           echo '<option>No languages available!</option>';
    130       }
    131   }
    132   $languages = array();
    133   while ( $file = readdir($dir) )
    134   {
    135       if ( $file == '..' || $file == '.' || !stristr($file, '.') || $file == 'css-gen.cfg' ) continue;
    136       $lang = substr($file, 0,  strpos($file, '.'));
    137       $languages[] = $lang;
    138   }
    139   closedir($dir);
    140   sort($languages);
    141   foreach ($languages as $lang) {
    142       echo '<option value="' . $lang . '">' . $lang . "</option>\n";
    143   }
    144
    145   ?>
    146   </select><br />
    147   <input type="submit" name="submit" value="Highlight Source">
    148   </form>
    149   <div id="footer">GeSHi &copy; Nigel McNie, 2004, released under the GNU GPL<br />
    150   For a better demonstration, check out the <a href="http://qbnz.com/highlighter/demo.php">online demo</a>
    151   </body>
    152   </html>

Documentation generated on Thu, 22 Sep 2005 13:47:53 +1200 by phpDocumentor 1.2.3