compare.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. /* Copyright (c)
  3. * - 2013-2019, Geert Bergman (geert@scrivo.nl), highlight.php
  4. * - 2014, Daniel Lynge, highlight.php (contributor)
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice,
  10. * this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. * 3. Neither the name of "highlight.js", "highlight.php", nor the names of its
  15. * contributors may be used to endorse or promote products derived from this
  16. * software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  19. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  22. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  23. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  24. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  25. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  26. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  27. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  28. * POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. $start = microtime(true);
  31. require_once "../Highlight/Autoloader.php";
  32. spl_autoload_register("Highlight\\Autoloader::load");
  33. $hl = new Highlight\Highlighter();
  34. $hl->setAutodetectLanguages($hl->listLanguages());
  35. ?>
  36. <html>
  37. <head>
  38. <link rel="stylesheet" type="text/css" href="../styles/default.css">
  39. <script src="highlight.pack.js"></script>
  40. <script>
  41. function testDetection() {
  42. var table = document.getElementById('test');
  43. var rws = table.getElementsByTagName('TR');
  44. for (var i = 1; i < rws.length; i++) {
  45. var tds = rws[i].getElementsByTagName('TD');
  46. var p1a = tds[1].getElementsByTagName('P')[0];
  47. var p1b = tds[1].getElementsByTagName('P')[1];
  48. var p2a = tds[2].getElementsByTagName('P')[0];
  49. var p2b = tds[2].getElementsByTagName('P')[1];
  50. var code1 = tds[1].getElementsByTagName('DIV')[0];
  51. var code2 = tds[2].getElementsByTagName('CODE')[0];
  52. p2a.innerHTML = code2.result.language + ": " + code2.result.re;
  53. p2b.innerHTML = code2.second_best.language + ": " +
  54. code2.second_best.re;
  55. if (code1.innerHTML != code2.innerHTML
  56. || p1a.innerHTML != p2a.innerHTML
  57. // || p1b.innerHTML != p2b.innerHTML
  58. ) {
  59. console.log(code1.innerHTML);
  60. console.log(code2.innerHTML);
  61. rws[i].style.backgroundColor = "#ffcccc";
  62. tds[0].style.backgroundColor = "#ffcccc";
  63. tds[0].lastChild.innerHTML = "failed";
  64. }
  65. }
  66. }
  67. hljs.tabReplace = ' ';
  68. hljs.initHighlightingOnLoad();
  69. window.addEventListener("load", testDetection ); // capture phase
  70. </script>
  71. <style type="text/css">
  72. table { font-family: sans-serif; }
  73. table { border-spacing: 0px; border-collapse:collapse; width: 100%}
  74. th, td { border: solid grey 1px; overflow: auto; max-width:500px}
  75. td p { margin: 0px; }
  76. pre code, pre div { padding: 0.5em; background: #F0F0F0; }
  77. td.signal { padding: 0.5em; background-color: #ccffcc; }
  78. pre { margin: 0px; }
  79. </style>
  80. </head>
  81. <body>
  82. <table id="test">
  83. <tr>
  84. <th>result</th>
  85. <th>highlight.php</th>
  86. <th>highlight.js</th>
  87. </tr>
  88. <?php
  89. foreach ($hl->listLanguages() as $languageId) {
  90. $snippet = file_get_contents("../test/detect/{$languageId}/default.txt");
  91. $r = $hl->highlightAuto($snippet); ?>
  92. <tr>
  93. <td class="signal"><p><?=$r->language; ?></p><p>pass</p></td>
  94. <td>
  95. <p><?=$r->language; ?>: <?=$r->relevance; ?></p>
  96. <p><?=$r->secondBest->language; ?>: <?=$r->secondBest->relevance; ?></p>
  97. <pre><div class=" hljs <?=$r->language; ?>"><?= $r->value; ?></div></pre>
  98. </td>
  99. <td class="js">
  100. <p>&nbsp;</p>
  101. <p>&nbsp;</p>
  102. <pre><code><?=htmlentities($snippet); ?></code></pre>
  103. </td>
  104. </tr>
  105. <?php
  106. }
  107. ?>
  108. </body>
  109. </html>
  110. <?php
  111. error_log("Highlighting took " . (microtime(true) - $start) . " seconds");
  112. ?>