source: lab.git/geshi/index.php @ 38785a4

trunk
Last change on this file since 38785a4 was 38785a4, checked in by mitty <mitty@…>, 13 years ago
  • fix directory name

git-svn-id: https://lab.mitty.jp/svn/lab/trunk@78 7d2118f6-f56c-43e7-95a2-4bb3031d96e7

  • Property mode set to 100755
File size: 8.6 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        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';
37    } else {
38        $fill_source = true;
39    }
40
41    // Here's a free demo of how GeSHi works.
42
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
46    // live site.
47    $geshi = new GeSHi($_POST['source'], $_POST['language']);
48
49    // Use the PRE_VALID header. This means less output source since we don't have to output &nbsp;
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 &nbsp
54    //            thus producing much larger overhead. You can set the tab width though.
55    $geshi->set_header_type(GESHI_HEADER_PRE_VALID);
56
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();
60
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);
63
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);
68
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:
73    //
74    // <pre style="[set_overall_style styles]"><ol>
75    // <li style="[set_line_style styles]"><div style="[set_code_style styles]>...</div></li>
76    // ...
77    // </ol></pre>
78    $geshi->set_line_style('color: #003030;', 'font-weight: bold; color: #006060;', true);
79    $geshi->set_code_style('color: #000020;', true);
80
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;');
85
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
88    // appear inside it.
89    //$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');
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;');
91
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;');
95} else {
96    // make sure we don't preselect any language
97    $_POST['language'] = null;
98}
99?>
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">
103<head>
104    <title>GeSHi - Generic Syntax Highlighter</title>
105    <style type="text/css">
106    <!--
107    <?php
108    if (isset($_POST['submit'])) {
109        // Output the stylesheet. Note it doesn't output the <style> tag
110        echo $geshi->get_stylesheet(true);
111    }
112    ?>
113    html {
114        background-color: #f0f0f0;
115    }
116    body {
117        font-family: Verdana, Arial, sans-serif;
118        margin: 10px;
119        border: 2px solid #e0e0e0;
120        background-color: #fcfcfc;
121        padding: 5px;
122    }
123    h2 {
124        margin: .1em 0 .2em .5em;
125        border-bottom: 1px solid #b0b0b0;
126        color: #b0b0b0;
127        font-weight: normal;
128        font-size: 150%;
129    }
130    h3 {
131        margin: .1em 0 .2em .5em;
132        color: #b0b0b0;
133        font-weight: normal;
134        font-size: 120%;
135    }
136    #footer {
137        text-align: center;
138        font-size: 80%;
139        color: #a9a9a9;
140    }
141    #footer a {
142        color: #9999ff;
143    }
144    textarea {
145        border: 1px solid #b0b0b0;
146        font-size: 90%;
147        color: #333;
148        margin-left: 20px;
149    }
150    select, input {
151        margin-left: 20px;
152    }
153    p {
154        font-size: 90%;
155        margin-left: .5em;
156    }
157    -->
158    </style>
159</head>
160<body>
161<!--
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
164include_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
166have that language file highlighted in PHP.</p>
167-->
168<?php
169if (isset($_POST['submit'])) {
170    // The fun part :)
171    echo $geshi->parse_code();
172    echo '<hr />';
173    echo '</body>';
174    echo '</html>';
175    exit;
176}
177?>
178<h2>GeSHi - Generic Syntax Highlighter</h2>
179<ul>
180  <li><a href="docs/geshi-doc.html">GeSHi Documentation</a></li>
181  <li><a href="docs/api/">GeSHi API</a></li>
182</ul>
183<form action="<?php echo basename($_SERVER['PHP_SELF']); ?>" method="post">
184<h3>Source to highlight</h3>
185<p>
186<textarea rows="30" cols="120" name="source" id="source"><?php echo $fill_source ? htmlspecialchars($_POST['source']) : '' ?></textarea>
187</p>
188<h3>Choose a language</h3>
189<p>
190<select name="language" id="language">
191<?php
192if (!($dir = @opendir(dirname(__FILE__) . '/geshi'))) {
193    if (!($dir = @opendir(dirname(__FILE__) . '/../geshi'))) {
194        echo '<option>No languages available!</option>';
195    }
196}
197$languages = array();
198while ($file = readdir($dir)) {
199    if ( $file[0] == '.' || strpos($file, '.', 1) === false) {
200        continue;
201    }
202    $lang = substr($file, 0,  strpos($file, '.'));
203    $languages[] = $lang;
204}
205closedir($dir);
206sort($languages);
207foreach ($languages as $lang) {
208    if (isset($_POST['language']) && $_POST['language'] == $lang) {
209        $selected = 'selected="selected"';
210    } else {
211        $selected = '';
212    }
213    echo '<option value="' . $lang . '" '. $selected .'>' . $lang . "</option>\n";
214}
215
216?>
217</select>
218</p>
219<p>
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" />
222</p>
223</form>
224<div id="footer">GeSHi &copy; Nigel McNie, 2004, released under the GNU GPL<br />
225For a better demonstration, check out the <a href="http://qbnz.com/highlighter/demo.php">online demo</a>
226</div>
227</body>
228</html>
Note: See TracBrowser for help on using the repository browser.