demo.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. <?php
  2. /* Copyright (c)
  3. * - 2006-2013, Ivan Sagalaev (maniac@softwaremaniacs.org), highlight.js
  4. * (original author)
  5. * - 2013-2015, Geert Bergman (geert@scrivo.nl), highlight.php
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright notice,
  12. * this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright notice,
  14. * this list of conditions and the following disclaimer in the documentation
  15. * and/or other materials provided with the distribution.
  16. * 3. Neither the name of "highlight.js", "highlight.php", nor the names of its
  17. * contributors may be used to endorse or promote products derived from this
  18. * software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  21. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  24. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  25. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  26. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  27. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  30. * POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. set_time_limit(60);
  33. $start = microtime(true);
  34. require_once("../Highlight/Autoloader.php");
  35. spl_autoload_register("Highlight\\Autoloader::load");
  36. $styles = Array();
  37. $d = dir("..".DIRECTORY_SEPARATOR."styles");
  38. while (false !== ($e = $d->read())) {
  39. if ($e[0] !== "." && $e !== "default.css" && strpos($e, ".css") !== false) {
  40. $styles[] = $e;
  41. }
  42. }
  43. sort($styles);
  44. use Highlight\Highlighter;
  45. $hl = new Highlighter();
  46. $hl->setAutodetectLanguages($hl->listLanguages());
  47. $tableRows = "";
  48. $failed = array();
  49. foreach ($hl->listLanguages() as $name) {
  50. $sn = $name;
  51. $snippet = file_get_contents("../test/detect/{$sn}/default.txt");
  52. $r = $hl->highlightAuto($snippet);
  53. $passed = ($r->language === $name);
  54. $res = "<div class=\"test\"><var class=\"".($passed?"passed":"failed").
  55. "\">{$r->language}</var>"." ({$r->relevance})<br>";
  56. if (isset($r->secondBest)) {
  57. $res .= "{$r->secondBest->language}"." ({$r->secondBest->relevance})";
  58. }
  59. $tableRows .= "<tr><th>{$name}{$res}</th><td class=\"{$name}\">
  60. <pre><code class=\"hljs {$name}\">{$r->value}</code></pre></td></th>";
  61. if (!$passed) {
  62. $failed[] = $name;
  63. }
  64. }
  65. if (count($failed)) {
  66. $testResult = "<p id=\"summary\" class=\"failed\">Failed tests: ".
  67. implode(", ", $failed);
  68. } else {
  69. $testResult = "<p id=\"summary\" class=\"passed\">All tests passed";
  70. }
  71. $testResult .= "</p><p>Highlighting took ".
  72. (microtime(true)-$start)." seconds</p>";
  73. $d->close();
  74. ?>
  75. <!DOCTYPE html>
  76. <head>
  77. <title>highlight.js test</title>
  78. <meta charset="utf-8">
  79. <link rel="stylesheet" title="Default" href="../styles/default.css">
  80. <?php foreach ($styles as $style) { ?>
  81. <link rel="alternate stylesheet" title="<?php echo $style?>"
  82. href="../styles/<?php echo $style?>">
  83. <?php } ?>
  84. <style>
  85. /* Base styles */
  86. body {
  87. font: small Arial, sans-serif;
  88. }
  89. h2 {
  90. font: bold 100% Arial, sans-serif;
  91. margin-top: 2em;
  92. margin-bottom: 0.5em;
  93. }
  94. table {
  95. width: 100%;
  96. padding: 0;
  97. border-collapse: collapse;
  98. }
  99. th {
  100. width: 12em;
  101. padding: 0; margin: 0;
  102. }
  103. td {
  104. padding-bottom: 1em;
  105. }
  106. td, th {
  107. vertical-align: top;
  108. text-align: left;
  109. }
  110. pre {
  111. margin: 0;
  112. font-size: medium;
  113. }
  114. .hljs-debug {
  115. color: red;
  116. }
  117. /* Style switcher */
  118. ul#switch {
  119. width: 66em;
  120. -webkit-column-width: 15em;
  121. -webkit-column-gap: 2em;
  122. -moz-column-width: 15em;
  123. -moz-column-gap: 2em;
  124. -o-column-width: 15em;
  125. -o-column-gap: 2em;
  126. column-width: 15em;
  127. column-gap: 2em;
  128. list-style: none;
  129. overflow: auto;
  130. padding: 0;
  131. margin: 0;
  132. }
  133. ul#switch li {
  134. -webkit-column-break-inside: avoid;
  135. -moz-column-break-inside: avoid;
  136. -o-column-break-inside: avoid;
  137. column-break-inside: avoid;
  138. padding: 0.1em;
  139. margin: 0.1em 1em 0.1em 0;
  140. background: #EEE;
  141. cursor: pointer;
  142. }
  143. ul#switch li.current {
  144. background: #CCC;
  145. }
  146. /* Tests */
  147. .test {
  148. color: #888;
  149. font-weight: normal;
  150. margin: 2em 0 0 0;
  151. }
  152. .test var {
  153. font-style: normal;
  154. }
  155. .passed {
  156. color: green;
  157. }
  158. .failed, .failed a {
  159. color: red;
  160. }
  161. .code {
  162. font: medium monospace;
  163. }
  164. .code .hljs-keyword {
  165. font-weight: bold;
  166. }
  167. /* Export form */
  168. #export_from, #export_to {
  169. width: 98%;
  170. }
  171. address {
  172. margin-top: 4em;
  173. }
  174. </style>
  175. <script>
  176. // Stylesheet switcher © Vladimir Epifanov <voldmar@voldmar.ru>
  177. (function(container_id) {
  178. if (window.addEventListener) {
  179. var attach = function(el, ev, handler) {
  180. el.addEventListener(ev, handler, false);
  181. }
  182. } else if (window.attachEvent) {
  183. var attach = function(el, ev, handler) {
  184. el.attachEvent('on' + ev, handler);
  185. }
  186. } else {
  187. var attach = function(el, ev, handler) {
  188. ev['on' + ev] = handler;
  189. }
  190. }
  191. attach(window, 'load', function() {
  192. var current = null;
  193. var info = {};
  194. var links = document.getElementsByTagName('link');
  195. var ul = document.createElement('ul');
  196. for (var i = 0; (link = links[i]); i++) {
  197. if ((link.getAttribute('rel').indexOf('style') != -1) && link.title) {
  198. var title = link.title;
  199. info[title] = {
  200. 'link': link,
  201. 'li': document.createElement('li')
  202. };
  203. ul.appendChild(info[title].li);
  204. info[title].li.title = title;
  205. info[title].link.disabled = true;
  206. info[title].li.appendChild(document.createTextNode(title));
  207. attach(info[title].li, 'click', (function (el) {
  208. return function() {
  209. current.li.className = '';
  210. current.link.disabled = true;
  211. current = el;
  212. current.li.className = 'current';
  213. current.link.disabled = false;
  214. }
  215. })(info[title]));
  216. }
  217. }
  218. current = info['Default'];
  219. current.li.className = 'current';
  220. current.link.disabled = false;
  221. ul.id = 'switch';
  222. container = document.getElementById(container_id);
  223. container.appendChild(ul);
  224. });
  225. })('styleswitcher');
  226. </script>
  227. <body>
  228. <p>This is a demo/test page showing all languages supported by
  229. <a href="https://github.com/scrivo/highlight.php">highlight.php</a>.
  230. Most snippets do not contain working code :-).
  231. <div id="styleswitcher">
  232. <h2>Styles</h2>
  233. </div>
  234. <h2>Automatically detected languages</h2>
  235. <?php echo $testResult;?>
  236. <table id="autotest"><?php echo $tableRows;?></table>
  237. </body>
  238. </html>