example.php 8.3 KB

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