source: lab/trunk/genshi/index.php @ 76

Last change on this file since 76 was 76, checked in by mitty, 13 years ago
  • change fancy line number to 10
  • change background color of codes to white
  • comment out copyright header
  • change title
  • comment out explanation of this script
  • suppress post form on source posted
  • add links to documents
  • Property svn:executable set to *
File size: 8.5 KB
Line 
1<?php
2/**
3 * GeSHi example script
4 *
5 * Just point your browser at this script (with geshi.php in the parent directory,
6 * and the language files in subdirectory "../geshi/")
7 *
8 * @author  Nigel McNie
9 * @version $Id: example.php 1512 2008-07-21 21:05:40Z benbe $
10 */
11header('Content-Type: text/html; charset=utf-8');
12
13error_reporting(E_ALL);
14
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.
18if (is_readable('../geshi.php')) {
19    $path = '../';
20} elseif (is_readable('geshi.php')) {
21    $path = './';
22} else {
23    die('Could not find geshi.php - make sure it is in your include path!');
24}
25require $path . 'geshi.php';
26
27$fill_source = false;
28if (isset($_POST['submit'])) {
29    if (get_magic_quotes_gpc()) {
30        $_POST['source'] = stripslashes($_POST['source']);
31    }
32    if (!strlen(trim($_POST['source']))) {
33        $_POST['language'] = preg_replace('#[^a-zA-Z0-9\-_]#', '', $_POST['language']);
34        $_POST['source'] = implode('', @file($path . 'geshi/' . $_POST['language'] . '.php'));
35        $_POST['language'] = 'php';
36    } else {
37        $fill_source = true;
38    }
39
40    // Here's a free demo of how GeSHi works.
41
42    // First the initialisation: source code to highlight and the language to use. Make sure
43    // you sanitise correctly if you use $_POST of course - this very script has had a security
44    // advisory against it in the past because of this. Please try not to use this script on a
45    // live site.
46    $geshi = new GeSHi($_POST['source'], $_POST['language']);
47
48    // Use the PRE_VALID header. This means less output source since we don't have to output &nbsp;
49    // everywhere. Of course it also means you can't set the tab width.
50    // HEADER_PRE_VALID puts the <pre> tag inside the list items (<li>) thus producing valid HTML markup.
51    // HEADER_PRE puts the <pre> tag around the list (<ol>) which is invalid in HTML 4 and XHTML 1
52    // HEADER_DIV puts a <div> tag arount the list (valid!) but needs to replace whitespaces with &nbsp
53    //            thus producing much larger overhead. You can set the tab width though.
54    $geshi->set_header_type(GESHI_HEADER_PRE_VALID);
55
56    // Enable CSS classes. You can use get_stylesheet() to output a stylesheet for your code. Using
57    // CSS classes results in much less output source.
58    $geshi->enable_classes();
59
60    // Enable line numbers. We want fancy line numbers, and we want every 5th line number to be fancy
61    $geshi->enable_line_numbers(GESHI_FANCY_LINE_NUMBERS, 10);
62
63    // Set the style for the PRE around the code. The line numbers are contained within this box (not
64    // XHTML compliant btw, but if you are liberally minded about these things then you'll appreciate
65    // the reduced source output).
66    $geshi->set_overall_style('font: normal normal 90% monospace; color: #000066; border: 1px solid #d0d0d0; background-color: #ffffff;', false);
67
68    // Set the style for line numbers. In order to get style for line numbers working, the <li> element
69    // is being styled. This means that the code on the line will also be styled, and most of the time
70    // you don't want this. So the set_code_style reverts styles for the line (by using a <div> on the line).
71    // So the source output looks like this:
72    //
73    // <pre style="[set_overall_style styles]"><ol>
74    // <li style="[set_line_style styles]"><div style="[set_code_style styles]>...</div></li>
75    // ...
76    // </ol></pre>
77    $geshi->set_line_style('color: #003030;', 'font-weight: bold; color: #006060;', true);
78    $geshi->set_code_style('color: #000020;', true);
79
80    // Styles for hyperlinks in the code. GESHI_LINK for default styles, GESHI_HOVER for hover style etc...
81    // note that classes must be enabled for this to work.
82    $geshi->set_link_styles(GESHI_LINK, 'color: #000060;');
83    $geshi->set_link_styles(GESHI_HOVER, 'background-color: #f0f000;');
84
85    // Use the header/footer functionality. This puts a div with content within the PRE element, so it is
86    // affected by the styles set by set_overall_style. So if the PRE has a border then the header/footer will
87    // appear inside it.
88    //$geshi->set_header_content('<SPEED> <TIME> GeSHi &copy; 2004-2007, Nigel McNie, 2007-2008 Benny Baumann. View source of example.php for example of using GeSHi');
89    //$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;');
90
91    // You can use <TIME> and <VERSION> as placeholders
92    $geshi->set_footer_content('Parsed in <TIME> seconds at <SPEED>, using GeSHi <VERSION>');
93    $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;');
94} else {
95    // make sure we don't preselect any language
96    $_POST['language'] = null;
97}
98?>
99<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
100     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
101<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
102<head>
103    <title>GeSHi - Generic Syntax Highlighter</title>
104    <style type="text/css">
105    <!--
106    <?php
107    if (isset($_POST['submit'])) {
108        // Output the stylesheet. Note it doesn't output the <style> tag
109        echo $geshi->get_stylesheet(true);
110    }
111    ?>
112    html {
113        background-color: #f0f0f0;
114    }
115    body {
116        font-family: Verdana, Arial, sans-serif;
117        margin: 10px;
118        border: 2px solid #e0e0e0;
119        background-color: #fcfcfc;
120        padding: 5px;
121    }
122    h2 {
123        margin: .1em 0 .2em .5em;
124        border-bottom: 1px solid #b0b0b0;
125        color: #b0b0b0;
126        font-weight: normal;
127        font-size: 150%;
128    }
129    h3 {
130        margin: .1em 0 .2em .5em;
131        color: #b0b0b0;
132        font-weight: normal;
133        font-size: 120%;
134    }
135    #footer {
136        text-align: center;
137        font-size: 80%;
138        color: #a9a9a9;
139    }
140    #footer a {
141        color: #9999ff;
142    }
143    textarea {
144        border: 1px solid #b0b0b0;
145        font-size: 90%;
146        color: #333;
147        margin-left: 20px;
148    }
149    select, input {
150        margin-left: 20px;
151    }
152    p {
153        font-size: 90%;
154        margin-left: .5em;
155    }
156    -->
157    </style>
158</head>
159<body>
160<!--
161<h2>GeSHi Example Script</h2>
162<p>To use this script, make sure that <strong>geshi.php</strong> is in the parent directory or in your
163include_path, and that the language files are in a subdirectory of GeSHi's directory called <strong>geshi/</strong>.</p>
164<p>Enter your source and a language to highlight the source in and submit, or just choose a language to
165have that language file highlighted in PHP.</p>
166-->
167<?php
168if (isset($_POST['submit'])) {
169    // The fun part :)
170    echo $geshi->parse_code();
171    echo '<hr />';
172    echo '</body>';
173    echo '</html>';
174    exit;
175}
176?>
177<h2>GeSHi - Generic Syntax Highlighter</h2>
178<ul>
179  <li><a href="docs/geshi-doc.html">GeSHi Documentation</a></li>
180  <li><a href="docs/api/">GeSHi API</a></li>
181</ul>
182<form action="<?php echo basename($_SERVER['PHP_SELF']); ?>" method="post">
183<h3>Source to highlight</h3>
184<p>
185<textarea rows="30" cols="120" name="source" id="source"><?php echo $fill_source ? htmlspecialchars($_POST['source']) : '' ?></textarea>
186</p>
187<h3>Choose a language</h3>
188<p>
189<select name="language" id="language">
190<?php
191if (!($dir = @opendir(dirname(__FILE__) . '/geshi'))) {
192    if (!($dir = @opendir(dirname(__FILE__) . '/../geshi'))) {
193        echo '<option>No languages available!</option>';
194    }
195}
196$languages = array();
197while ($file = readdir($dir)) {
198    if ( $file[0] == '.' || strpos($file, '.', 1) === false) {
199        continue;
200    }
201    $lang = substr($file, 0,  strpos($file, '.'));
202    $languages[] = $lang;
203}
204closedir($dir);
205sort($languages);
206foreach ($languages as $lang) {
207    if (isset($_POST['language']) && $_POST['language'] == $lang) {
208        $selected = 'selected="selected"';
209    } else {
210        $selected = '';
211    }
212    echo '<option value="' . $lang . '" '. $selected .'>' . $lang . "</option>\n";
213}
214
215?>
216</select>
217</p>
218<p>
219<input type="submit" name="submit" value="Highlight Source" />
220<input type="submit" name="clear" onclick="document.getElementById('source').value='';document.getElementById('language').value='';return false" value="clear" />
221</p>
222</form>
223<div id="footer">GeSHi &copy; Nigel McNie, 2004, released under the GNU GPL<br />
224For a better demonstration, check out the <a href="http://qbnz.com/highlighter/demo.php">online demo</a>
225</div>
226</body>
227</html>
Note: See TracBrowser for help on using the repository browser.