|
30 | 30 | * POSSIBILITY OF SUCH DAMAGE. |
31 | 31 | */ |
32 | 32 |
|
33 | | -set_time_limit(0); |
34 | | -$start = microtime(true); |
35 | | - |
36 | 33 | require_once "../Highlight/Autoloader.php"; |
| 34 | +require_once "../HighlightUtilities/functions.php"; |
37 | 35 | spl_autoload_register("Highlight\\Autoloader::load"); |
38 | 36 |
|
39 | | -$styles = array(); |
40 | | -$d = dir(".." . DIRECTORY_SEPARATOR . "styles"); |
41 | | -while (($e = $d->read()) !== false) { |
42 | | - if ($e[0] !== "." && $e !== "default.css" && strpos($e, ".css") !== false) { |
43 | | - $styles[] = $e; |
44 | | - } |
45 | | -} |
46 | | -sort($styles); |
47 | | - |
48 | 37 | use Highlight\Highlighter; |
49 | 38 |
|
50 | 39 | $hl = new Highlighter(); |
51 | 40 | $hl->setAutodetectLanguages($hl->listLanguages()); |
52 | 41 |
|
53 | | -$tableRows = ""; |
54 | | -$failed = array(); |
55 | | - |
56 | | -foreach ($hl->listLanguages() as $name) { |
57 | | - $sn = $name; |
58 | | - $snippet = file_get_contents("../test/detect/{$sn}/default.txt"); |
59 | | - $r = $hl->highlightAuto($snippet); |
60 | | - $passed = ($r->language === $name); |
61 | | - $res = "<div class=\"test\"><var class=\"" . ($passed ? "passed" : "failed") . |
62 | | - "\">{$r->language}</var>" . " ({$r->relevance})<br>"; |
63 | | - if (isset($r->secondBest)) { |
64 | | - $res .= "{$r->secondBest->language}" . " ({$r->secondBest->relevance})"; |
65 | | - } |
66 | | - $tableRows .= "<tr><th>{$name}{$res}</th><td class=\"{$name}\"> |
67 | | - <pre><code class=\"hljs {$name}\">{$r->value}</code></pre></td></th>"; |
68 | | - if (!$passed) { |
69 | | - $failed[] = $name; |
70 | | - } |
| 42 | +function getLanguageRaw($lang) |
| 43 | +{ |
| 44 | + return file_get_contents("../test/detect/{$lang}/default.txt"); |
71 | 45 | } |
72 | 46 |
|
73 | | -if (count($failed)) { |
74 | | - $testResult = "<p id=\"summary\" class=\"failed\">Failed tests: " . |
75 | | - implode(", ", $failed); |
76 | | -} else { |
77 | | - $testResult = "<p id=\"summary\" class=\"passed\">All tests passed"; |
| 47 | +function getLanguageDemo($lang) |
| 48 | +{ |
| 49 | + $snippet = getLanguageRaw($lang); |
| 50 | + |
| 51 | + if ($snippet === false) { |
| 52 | + die("Language not found"); |
| 53 | + } |
| 54 | + |
| 55 | + global $hl; |
| 56 | + |
| 57 | + $start = microtime(true); |
| 58 | + $result = $hl->highlightAuto($snippet); |
| 59 | + $totalTime = microtime(true) - $start; |
| 60 | + |
| 61 | + return strtr( |
| 62 | + ' |
| 63 | + <link rel="stylesheet" type="text/css" href="../styles/default.css"> |
| 64 | + <style>* { margin: 0; padding: 0 }</style> |
| 65 | + <p>Result: {result} <small>[Expected: {expected}; Actual: {actual} ({actRel}); Second Best: {second} ({secRel})]</small></p> |
| 66 | + <p>Total Time: {time} secs</p> |
| 67 | + <pre><code class="{actual} hljs">{code}</code></pre> |
| 68 | + ', |
| 69 | + array( |
| 70 | + '{result}' => $result->language === $lang ? 'Passed' : 'FAILED', |
| 71 | + '{expected}' => $lang, |
| 72 | + '{actual}' => $result->language, |
| 73 | + '{actRel}' => $result->relevance, |
| 74 | + '{second}' => $result->secondBest->language, |
| 75 | + '{secRel}' => $result->secondBest->relevance, |
| 76 | + '{code}' => $result->value, |
| 77 | + '{time}' => sprintf('%.3f', $totalTime), |
| 78 | + ) |
| 79 | + ); |
78 | 80 | } |
79 | 81 |
|
80 | | -$testResult .= "</p><p>Highlighting took " . |
81 | | - (microtime(true) - $start) . " seconds</p>"; |
82 | | - |
83 | | -$d->close(); |
84 | | - |
85 | | -?> |
86 | | -<!DOCTYPE html> |
87 | | -<head> |
88 | | - <title>highlight.js test</title> |
89 | | - <meta charset="utf-8"> |
| 82 | +if (isset($_GET['lang'])) { |
| 83 | + $lang = $_GET['lang']; |
90 | 84 |
|
91 | | - <link rel="stylesheet" title="Default" href="../styles/default.css"> |
92 | | -<?php foreach ($styles as $style) { |
93 | | - ?> |
94 | | - <link rel="alternate stylesheet" title="<?php echo $style; ?>" |
95 | | - href="../styles/<?php echo $style; ?>"> |
96 | | -<?php |
97 | | -} ?> |
98 | | - |
99 | | - <style> |
100 | | - /* Base styles */ |
101 | | - body { |
102 | | - font: small Arial, sans-serif; |
103 | | - } |
104 | | - h2 { |
105 | | - font: bold 100% Arial, sans-serif; |
106 | | - margin-top: 2em; |
107 | | - margin-bottom: 0.5em; |
108 | | - } |
109 | | - table { |
110 | | - width: 100%; |
111 | | - padding: 0; |
112 | | - border-collapse: collapse; |
113 | | - } |
114 | | - th { |
115 | | - width: 12em; |
116 | | - padding: 0; margin: 0; |
117 | | - } |
118 | | - td { |
119 | | - padding-bottom: 1em; |
120 | | - } |
121 | | - td, th { |
122 | | - vertical-align: top; |
123 | | - text-align: left; |
124 | | - } |
125 | | - pre { |
126 | | - margin: 0; |
127 | | - font-size: medium; |
128 | | - } |
129 | | - .hljs-debug { |
130 | | - color: red; |
131 | | - } |
132 | | - /* Style switcher */ |
133 | | - ul#switch { |
134 | | - width: 66em; |
135 | | - -webkit-column-width: 15em; |
136 | | - -webkit-column-gap: 2em; |
137 | | - -moz-column-width: 15em; |
138 | | - -moz-column-gap: 2em; |
139 | | - -o-column-width: 15em; |
140 | | - -o-column-gap: 2em; |
141 | | - column-width: 15em; |
142 | | - column-gap: 2em; |
143 | | - list-style: none; |
144 | | - overflow: auto; |
145 | | - padding: 0; |
146 | | - margin: 0; |
147 | | - } |
148 | | - ul#switch li { |
149 | | - -webkit-column-break-inside: avoid; |
150 | | - -moz-column-break-inside: avoid; |
151 | | - -o-column-break-inside: avoid; |
152 | | - column-break-inside: avoid; |
153 | | - padding: 0.1em; |
154 | | - margin: 0.1em 1em 0.1em 0; |
155 | | - background: #EEE; |
156 | | - cursor: pointer; |
157 | | - } |
158 | | - ul#switch li.current { |
159 | | - background: #CCC; |
160 | | - } |
161 | | - /* Tests */ |
162 | | - .test { |
163 | | - color: #888; |
164 | | - font-weight: normal; |
165 | | - margin: 2em 0 0 0; |
| 85 | + if (isset($_GET['raw'])) { |
| 86 | + echo getLanguageRaw($lang); |
| 87 | + die(); |
166 | 88 | } |
167 | | - .test var { |
168 | | - font-style: normal; |
169 | | - } |
170 | | - .passed { |
171 | | - color: green; |
172 | | - } |
173 | | - .failed, .failed a { |
174 | | - color: red; |
175 | | - } |
176 | | - .code { |
177 | | - font: medium monospace; |
178 | | - } |
179 | | - .code .hljs-keyword { |
180 | | - font-weight: bold; |
181 | | - } |
182 | | - /* Export form */ |
183 | | - #export_from, #export_to { |
184 | | - width: 98%; |
185 | | - } |
186 | | - address { |
187 | | - margin-top: 4em; |
188 | | - } |
189 | | - </style> |
190 | | - |
191 | | - <script> |
192 | | - // Stylesheet switcher © Vladimir Epifanov < [email protected]> |
193 | | - (function(container_id) { |
194 | | - if (window.addEventListener) { |
195 | | - var attach = function(el, ev, handler) { |
196 | | - el.addEventListener(ev, handler, false); |
197 | | - } |
198 | | - } else if (window.attachEvent) { |
199 | | - var attach = function(el, ev, handler) { |
200 | | - el.attachEvent('on' + ev, handler); |
201 | | - } |
202 | | - } else { |
203 | | - var attach = function(el, ev, handler) { |
204 | | - ev['on' + ev] = handler; |
205 | | - } |
206 | | - } |
207 | | - |
208 | | - attach(window, 'load', function() { |
209 | | - var current = null; |
210 | | - |
211 | | - var info = {}; |
212 | | - var links = document.getElementsByTagName('link'); |
213 | | - var ul = document.createElement('ul'); |
214 | | - |
215 | | - for (var i = 0; (link = links[i]); i++) { |
216 | | - if ((link.getAttribute('rel').indexOf('style') != -1) && link.title) { |
217 | | - var title = link.title; |
218 | | - |
219 | | - info[title] = { |
220 | | - 'link': link, |
221 | | - 'li': document.createElement('li') |
222 | | - }; |
223 | 89 |
|
224 | | - ul.appendChild(info[title].li); |
225 | | - info[title].li.title = title; |
226 | | - |
227 | | - info[title].link.disabled = true; |
228 | | - |
229 | | - info[title].li.appendChild(document.createTextNode(title)); |
230 | | - |
231 | | - attach(info[title].li, 'click', (function (el) { |
232 | | - return function() { |
233 | | - current.li.className = ''; |
234 | | - current.link.disabled = true; |
235 | | - current = el; |
236 | | - current.li.className = 'current'; |
237 | | - current.link.disabled = false; |
238 | | - } |
239 | | - })(info[title])); |
240 | | - } |
241 | | - } |
242 | | - |
243 | | - current = info['Default']; |
244 | | - current.li.className = 'current'; |
245 | | - current.link.disabled = false; |
246 | | - |
247 | | - ul.id = 'switch'; |
248 | | - container = document.getElementById(container_id); |
249 | | - container.appendChild(ul); |
250 | | - }); |
251 | | - })('styleswitcher'); |
252 | | - </script> |
253 | | -<body> |
254 | | - |
255 | | -<p>This is a demo/test page showing all languages supported by |
256 | | -<a href="https://github.com/scrivo/highlight.php">highlight.php</a>. |
257 | | -Most snippets do not contain working code :-). |
258 | | - |
259 | | -<div id="styleswitcher"> |
260 | | - <h2>Styles</h2> |
261 | | -</div> |
262 | | - |
263 | | -<h2>Automatically detected languages</h2> |
| 90 | + echo getLanguageDemo($_GET['lang']); |
| 91 | + die(); |
| 92 | +} |
264 | 93 |
|
265 | | -<?php echo $testResult; ?> |
266 | | -<table id="autotest"><?php echo $tableRows; ?></table> |
| 94 | +$styles = HighlightUtilities\getAvailableStyleSheets(); |
| 95 | +sort($styles); |
267 | 96 |
|
268 | | -</body> |
| 97 | +$languageCount = count($hl->listLanguages()); |
| 98 | +?> |
| 99 | +<!DOCTYPE html> |
| 100 | +<html lang="en"> |
| 101 | + <head> |
| 102 | + <title>highlight.js test</title> |
| 103 | + <meta charset="utf-8"> |
| 104 | + |
| 105 | + <link rel="stylesheet" title="Default" href="../styles/default.css"> |
| 106 | + <?php foreach ($styles as $style): ?> |
| 107 | + <link rel="alternate stylesheet" title="<?= $style; ?>" href="../styles/<?= $style; ?>.css"> |
| 108 | + <?php endforeach; ?> |
| 109 | + |
| 110 | + <style> |
| 111 | + iframe { |
| 112 | + border: 0; |
| 113 | + width: 100%; |
| 114 | + } |
| 115 | + |
| 116 | + .style-selector { |
| 117 | + display: flex; |
| 118 | + flex-wrap: wrap; |
| 119 | + } |
| 120 | + |
| 121 | + .style-selector label { |
| 122 | + width: 25%; |
| 123 | + } |
| 124 | + </style> |
| 125 | + </head> |
| 126 | + |
| 127 | + <body> |
| 128 | + <h1>highlight.php Auto-Detection</h1> |
| 129 | + |
| 130 | + <p> |
| 131 | + This is a demo/test page showing all languages supported by |
| 132 | + <a href="https://github.com/scrivo/highlight.php">highlight.php</a>. Most snippets do not contain working |
| 133 | + code :-). |
| 134 | + </p> |
| 135 | + |
| 136 | + <p> |
| 137 | + This page will take an <strong>EXTREMELY</strong> long time to load since it is automatically detecting |
| 138 | + <?= $languageCount; ?> languages. Automatic detection happens in a brute force fashion meaning loading this |
| 139 | + page will cause <?= pow($languageCount, 2); ?> iterations. |
| 140 | + </p> |
| 141 | + |
| 142 | + <p>For example, this page takes approximately 9 minutes to load completely for @allejo</p> |
| 143 | + |
| 144 | + <div> |
| 145 | + <fieldset> |
| 146 | + <legend>Stylesheet</legend> |
| 147 | + |
| 148 | + <div class="style-selector"> |
| 149 | + <?php foreach ($styles as $style): ?> |
| 150 | + <label> |
| 151 | + <input |
| 152 | + type="radio" |
| 153 | + name="stylesheet" |
| 154 | + value="<?= $style; ?>" |
| 155 | + <?= $style !== 'default' ?: 'checked'; ?> |
| 156 | + /> |
| 157 | + <?= $style; ?> |
| 158 | + </label> |
| 159 | + <?php endforeach; ?> |
| 160 | + </div> |
| 161 | + </fieldset> |
| 162 | + </div> |
| 163 | + |
| 164 | + <h2>Automatically detected languages</h2> |
| 165 | + |
| 166 | + <?php foreach ($hl->listLanguages() as $language): ?> |
| 167 | + <section> |
| 168 | + <h3><?= $language; ?></h3> |
| 169 | + <iframe src="demo.php?lang=<?= $language; ?>"></iframe> |
| 170 | + </section> |
| 171 | + <?php endforeach; ?> |
| 172 | + </body> |
269 | 173 | </html> |
0 commit comments