source: lab.git/genshi/index.php @ 6c31c22

trunk
Last change on this file since 6c31c22 was 6c31c22, checked in by mitty <mitty@…>, 14 years ago
  • rename filename and add executable

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

  • Property mode set to 100755
File size: 8.3 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, 5);
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: #f0f0f0;', 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 examples</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<h2>GeSHi Example Script</h2>
161<p>To use this script, make sure that <strong>geshi.php</strong> is in the parent directory or in your
162include_path, and that the language files are in a subdirectory of GeSHi's directory called <strong>geshi/</strong>.</p>
163<p>Enter your source and a language to highlight the source in and submit, or just choose a language to
164have that language file highlighted in PHP.</p>
165<?php
166if (isset($_POST['submit'])) {
167    // The fun part :)
168    echo $geshi->parse_code();
169    echo '<hr />';
170}
171?>
172<form action="<?php echo basename($_SERVER['PHP_SELF']); ?>" method="post">
173<h3>Source to highlight</h3>
174<p>
175<textarea rows="10" cols="60" name="source" id="source"><?php echo $fill_source ? htmlspecialchars($_POST['source']) : '' ?></textarea>
176</p>
177<h3>Choose a language</h3>
178<p>
179<select name="language" id="language">
180<?php
181if (!($dir = @opendir(dirname(__FILE__) . '/geshi'))) {
182    if (!($dir = @opendir(dirname(__FILE__) . '/../geshi'))) {
183        echo '<option>No languages available!</option>';
184    }
185}
186$languages = array();
187while ($file = readdir($dir)) {
188    if ( $file[0] == '.' || strpos($file, '.', 1) === false) {
189        continue;
190    }
191    $lang = substr($file, 0,  strpos($file, '.'));
192    $languages[] = $lang;
193}
194closedir($dir);
195sort($languages);
196foreach ($languages as $lang) {
197    if (isset($_POST['language']) && $_POST['language'] == $lang) {
198        $selected = 'selected="selected"';
199    } else {
200        $selected = '';
201    }
202    echo '<option value="' . $lang . '" '. $selected .'>' . $lang . "</option>\n";
203}
204
205?>
206</select>
207</p>
208<p>
209<input type="submit" name="submit" value="Highlight Source" />
210<input type="submit" name="clear" onclick="document.getElementById('source').value='';document.getElementById('language').value='';return false" value="clear" />
211</p>
212</form>
213<div id="footer">GeSHi &copy; Nigel McNie, 2004, released under the GNU GPL<br />
214For a better demonstration, check out the <a href="http://qbnz.com/highlighter/demo.php">online demo</a>
215</div>
216</body>
217</html>
Note: See TracBrowser for help on using the repository browser.