5 * Just point your browser at this script (with geshi.php in the parent directory,
6 * and the language files in subdirectory "../geshi/")
9 * @version $Id: example.php 1512 2008-07-21 21:05:40Z benbe $
11 header('Content-Type: text/html; charset=utf-8');
13 error_reporting(E_ALL);
15 // Rudimentary checking of where GeSHi is. In a default install it will be in ../, but
16 // it could be in the current directory if the include_path is set. There's nowhere else
17 // we can reasonably guess.
18 if (is_readable('../geshi.php')) {
20 } elseif (is_readable('geshi.php')) {
23 die('Could not find geshi.php - make sure it is in your include path!');
25 require $path . 'geshi.php';
28 if (isset($_POST['submit'])) {
29 if (get_magic_quotes_gpc()) {
30 $_POST['source'] = stripslashes($_POST['source']);
32 if (!strlen(trim($_POST['source']))) {
33 if(! isset($_POST['language'])) { $_POST['language'] = 'php'; }
34 $_POST['language'] = preg_replace('#[^a-zA-Z0-9\-_]#', '', $_POST['language']);
35 $_POST['source'] = implode('', @file($path . 'geshi/' . $_POST['language'] . '.php'));
36 $_POST['language'] = 'php';
41 // Here's a free demo of how GeSHi works.
43 // First the initialisation: source code to highlight and the language to use. Make sure
44 // you sanitise correctly if you use $_POST of course - this very script has had a security
45 // advisory against it in the past because of this. Please try not to use this script on a
47 $geshi = new GeSHi($_POST['source'], $_POST['language']);
49 // Use the PRE_VALID header. This means less output source since we don't have to output
50 // everywhere. Of course it also means you can't set the tab width.
51 // HEADER_PRE_VALID puts the <pre> tag inside the list items (<li>) thus producing valid HTML markup.
52 // HEADER_PRE puts the <pre> tag around the list (<ol>) which is invalid in HTML 4 and XHTML 1
53 // HEADER_DIV puts a <div> tag arount the list (valid!) but needs to replace whitespaces with  
54 // thus producing much larger overhead. You can set the tab width though.
55 $geshi->set_header_type(GESHI_HEADER_PRE_VALID);
57 // Enable CSS classes. You can use get_stylesheet() to output a stylesheet for your code. Using
58 // CSS classes results in much less output source.
59 $geshi->enable_classes();
61 // Enable line numbers. We want fancy line numbers, and we want every 5th line number to be fancy
62 $geshi->enable_line_numbers(GESHI_FANCY_LINE_NUMBERS, 10);
64 // Set the style for the PRE around the code. The line numbers are contained within this box (not
65 // XHTML compliant btw, but if you are liberally minded about these things then you'll appreciate
66 // the reduced source output).
67 $geshi->set_overall_style('font: normal normal 90% monospace; color: #000066; border: 1px solid #d0d0d0; background-color: #ffffff;', false);
69 // Set the style for line numbers. In order to get style for line numbers working, the <li> element
70 // is being styled. This means that the code on the line will also be styled, and most of the time
71 // you don't want this. So the set_code_style reverts styles for the line (by using a <div> on the line).
72 // So the source output looks like this:
74 // <pre style="[set_overall_style styles]"><ol>
75 // <li style="[set_line_style styles]"><div style="[set_code_style styles]>...</div></li>
78 $geshi->set_line_style('color: #003030;', 'font-weight: bold; color: #006060;', true);
79 $geshi->set_code_style('color: #000020;', true);
81 // Styles for hyperlinks in the code. GESHI_LINK for default styles, GESHI_HOVER for hover style etc...
82 // note that classes must be enabled for this to work.
83 $geshi->set_link_styles(GESHI_LINK, 'color: #000060;');
84 $geshi->set_link_styles(GESHI_HOVER, 'background-color: #f0f000;');
86 // Use the header/footer functionality. This puts a div with content within the PRE element, so it is
87 // affected by the styles set by set_overall_style. So if the PRE has a border then the header/footer will
89 //$geshi->set_header_content('<SPEED> <TIME> GeSHi © 2004-2007, Nigel McNie, 2007-2008 Benny Baumann. View source of example.php for example of using GeSHi');
90 //$geshi->set_header_content_style('font-family: sans-serif; color: #808080; font-size: 70%; font-weight: bold; background-color: #f0f0ff; border-bottom: 1px solid #d0d0d0; padding: 2px;');
92 // You can use <TIME> and <VERSION> as placeholders
93 $geshi->set_footer_content('Parsed in <TIME> seconds at <SPEED>, using GeSHi <VERSION>');
94 $geshi->set_footer_content_style('font-family: sans-serif; color: #808080; font-size: 70%; font-weight: bold; background-color: #f0f0ff; border-top: 1px solid #d0d0d0; padding: 2px;');
96 // make sure we don't preselect any language
97 $_POST['language'] = null;
100 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
101 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
102 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
104 <title>GeSHi - Generic Syntax Highlighter</title>
105 <style type="text/css">
108 if (isset($_POST['submit'])) {
109 // Output the stylesheet. Note it doesn't output the <style> tag
110 echo $geshi->get_stylesheet(true);
114 background-color: #f0f0f0;
117 font-family: Verdana, Arial, sans-serif;
119 border: 2px solid #e0e0e0;
120 background-color: #fcfcfc;
124 margin: .1em 0 .2em .5em;
125 border-bottom: 1px solid #b0b0b0;
131 margin: .1em 0 .2em .5em;
145 border: 1px solid #b0b0b0;
162 <h2>GeSHi Example Script</h2>
163 <p>To use this script, make sure that <strong>geshi.php</strong> is in the parent directory or in your
164 include_path, and that the language files are in a subdirectory of GeSHi's directory called <strong>geshi/</strong>.</p>
165 <p>Enter your source and a language to highlight the source in and submit, or just choose a language to
166 have that language file highlighted in PHP.</p>
169 if (isset($_POST['submit'])) {
171 echo $geshi->parse_code();
178 <h2>GeSHi - Generic Syntax Highlighter</h2>
180 <li><a href="docs/geshi-doc.html">GeSHi Documentation</a></li>
181 <li><a href="docs/api/">GeSHi API</a></li>
183 <form action="<?php echo basename($_SERVER['PHP_SELF']); ?>" method="post">
184 <h3>Source to highlight</h3>
186 <textarea rows="30" cols="120" name="source" id="source"><?php echo $fill_source ? htmlspecialchars($_POST['source']) : '' ?></textarea>
188 <h3>Choose a language</h3>
190 <select name="language" id="language">
192 if (!($dir = @opendir(dirname(__FILE__) . '/geshi'))) {
193 if (!($dir = @opendir(dirname(__FILE__) . '/../geshi'))) {
194 echo '<option>No languages available!</option>';
197 $languages = array();
198 while ($file = readdir($dir)) {
199 if ( $file[0] == '.' || strpos($file, '.', 1) === false) {
202 $lang = substr($file, 0, strpos($file, '.'));
203 $languages[] = $lang;
207 foreach ($languages as $lang) {
208 if (isset($_POST['language']) && $_POST['language'] == $lang) {
209 $selected = 'selected="selected"';
213 echo '<option value="' . $lang . '" '. $selected .'>' . $lang . "</option>\n";
220 <input type="submit" name="submit" value="Highlight Source" />
221 <input type="submit" name="clear" onclick="document.getElementById('source').value='';document.getElementById('language').value='';return false" value="clear" />
224 <div id="footer">GeSHi © Nigel McNie, 2004, released under the GNU GPL<br />
225 For a better demonstration, check out the <a href="http://qbnz.com/highlighter/demo.php">online demo</a>