demo.php 7.6 KB

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