1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml" lang="en">
  3.  
  4. <head>
  5. <title>Chris Shiflett: highlight.php</title>
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  7. <style type="text/css" media="screen">
  8. body {
  9. margin: 2em;
  10. padding: 0;
  11. border: 0;
  12. font: 1em verdana, helvetica, sans-serif;
  13. color: #000;
  14. background: #fff;
  15. text-align: center;
  16. }
  17. ol.code {
  18. width: 90%;
  19. margin: 0 5%;
  20. padding: 0;
  21. font-size: 0.75em;
  22. line-height: 1.8em;
  23. overflow: hidden;
  24. color: #939399;
  25. text-align: left;
  26. list-style-position: inside;
  27. border: 1px solid #d3d3d0;
  28. }
  29. ol.code li {
  30. float: left;
  31. clear: both;
  32. width: 99%;
  33. white-space: nowrap;
  34. margin: 0;
  35. padding: 0 0 0 1%;
  36. background: #fff;
  37. }
  38. ol.code li.even { background: #f3f3f0; }
  39. ol.code li code {
  40. font: 1.2em courier, monospace;
  41. color: #c30;
  42. white-space: pre;
  43. padding-left: 0.5em;
  44. }
  45. .code .comment { color: #939399; }
  46. .code .default { color: #44c; }
  47. .code .keyword { color: #373; }
  48. .code .string { color: #c30; }
  49. </style>
  50. <script>
  51. function addLoadEvent(func) {
  52. var oldonload = window.onload;
  53. if (typeof window.onload != 'function') window.onload = func;
  54. else {
  55. window.onload = function() {
  56. if (oldonload) oldonload();
  57. func();
  58. }
  59. }
  60. }
  61. function initCodeHashHighlight() {
  62. if (!document.getElementById) return;
  63. if (!location.hash) return;
  64. var target = document.getElementById(location.hash.substr(1));
  65. if (!target) return;
  66. target.style.backgroundColor = '#FFFF99';
  67. }
  68. addLoadEvent(initCodeHashHighlight);
  69. </script>
  70. </head>
  71.  
  72. <body>
  73. <?php
  74.  
  75. /* Example Listing */
  76. $code = file_get_contents('./highlight.php');
  77.  
  78. ini_set('highlight.comment', 'comment');
  79. ini_set('highlight.default', 'default');
  80. ini_set('highlight.keyword', 'keyword');
  81. ini_set('highlight.string', 'string');
  82. ini_set('highlight.html', 'html');
  83.  
  84. $code = highlight_string($code, TRUE);
  85.  
  86. /* Clean Up */
  87. $code = substr($code, 33, -15);
  88. $code = str_replace('&nbsp;', ' ', $code);
  89. $code = str_replace('&amp;', '&#38;', $code);
  90. $code = str_replace('<br />', "\n", $code);
  91. $code = str_replace('<span style="color: ', '<span class="', $code);
  92. $code = trim($code);
  93.  
  94. /* Normalize Newlines */
  95. $code = str_replace("\r", "\n", $code);
  96. $code = preg_replace("!\n\n\n+!", "\n\n", $code);
  97.  
  98. $lines = explode("\n", $code);
  99.  
  100. /* Previous Style */
  101. $previous = FALSE;
  102.  
  103. /* Output Listing */
  104. echo " <ol class=\"code\">\n";
  105. foreach ($lines as $key => $line) {
  106. if (substr($line, 0, 7) == '</span>') {
  107. $previous = FALSE;
  108. $line = substr($line, 7);
  109. }
  110.  
  111. if (empty($line)) {
  112. $line = '&#160;';
  113. }
  114.  
  115. if ($previous) {
  116. $line = "<span class=\"$previous\">" . $line;
  117. }
  118.  
  119. /* Set Previous Style */
  120. if (strpos($line, '<span') !== FALSE) {
  121. switch (substr($line, strrpos($line, '<span') + 13, 1)) {
  122. case 'c':
  123. $previous = 'comment';
  124. break;
  125. case 'd':
  126. $previous = 'default';
  127. break;
  128. case 'k':
  129. $previous = 'keyword';
  130. break;
  131. case 's':
  132. $previous = 'string';
  133. break;
  134. }
  135. }
  136.  
  137. /* Unset Previous Style Unless Span Continues */
  138. if (substr($line, -7) == '</span>') {
  139. $previous = FALSE;
  140. } elseif ($previous) {
  141. $line .= '</span>';
  142. }
  143.  
  144. $num = $key + 1;
  145. if ($key % 2) {
  146. echo " <li id=\"line-$num\" class=\"even\"><code>$line</code></li>\n";
  147. } else {
  148. echo " <li id=\"line-$num\"><code>$line</code></li>\n";
  149. }
  150. }
  151. echo " </ol>\n";
  152.  
  153. ?>
  154. </body>
  155.  
  156. </html>