compare.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. /* Copyright (c)
  3. * - 2013-2015, Geert Bergman (geert@scrivo.nl), highlight.php
  4. * - 2014, Daniel Lynge, highlight.php (contributor)
  5. * All rights reserved.
  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. $start = microtime(true);
  32. require_once("../Highlight/Autoloader.php");
  33. spl_autoload_register("Highlight\\Autoloader::load");
  34. $hl = new Highlight\Highlighter();
  35. $hl->setAutodetectLanguages($hl->listLanguages());
  36. ?>
  37. <html>
  38. <head>
  39. <link rel="stylesheet" type="text/css" href="../styles/default.css">
  40. <script src="highlight.pack.js"></script>
  41. <script>
  42. function testDetection() {
  43. var table = document.getElementById('test');
  44. var rws = table.getElementsByTagName('TR');
  45. for (var i = 1; i < rws.length; i++) {
  46. var tds = rws[i].getElementsByTagName('TD');
  47. var p1a = tds[1].getElementsByTagName('P')[0];
  48. var p1b = tds[1].getElementsByTagName('P')[1];
  49. var p2a = tds[2].getElementsByTagName('P')[0];
  50. var p2b = tds[2].getElementsByTagName('P')[1];
  51. var code1 = tds[1].getElementsByTagName('DIV')[0];
  52. var code2 = tds[2].getElementsByTagName('CODE')[0];
  53. p2a.innerHTML = code2.result.language + ": " + code2.result.re;
  54. p2b.innerHTML = code2.second_best.language + ": " +
  55. code2.second_best.re;
  56. if (code1.innerHTML != code2.innerHTML
  57. || p1a.innerHTML != p2a.innerHTML
  58. // || p1b.innerHTML != p2b.innerHTML
  59. ) {
  60. console.log(code1.innerHTML);
  61. console.log(code2.innerHTML);
  62. rws[i].style.backgroundColor = "#ffcccc";
  63. tds[0].style.backgroundColor = "#ffcccc";
  64. tds[0].lastChild.innerHTML = "failed";
  65. }
  66. }
  67. }
  68. hljs.tabReplace = ' ';
  69. hljs.initHighlightingOnLoad();
  70. window.addEventListener("load", testDetection ); // capture phase
  71. </script>
  72. <style type="text/css">
  73. table { font-family: sans-serif; }
  74. table { border-spacing: 0px; border-collapse:collapse; width: 100%}
  75. th, td { border: solid grey 1px; overflow: auto; max-width:500px}
  76. td p { margin: 0px; }
  77. pre code, pre div { padding: 0.5em; background: #F0F0F0; }
  78. td.signal { padding: 0.5em; background-color: #ccffcc; }
  79. pre { margin: 0px; }
  80. </style>
  81. </head>
  82. <body>
  83. <table id="test">
  84. <tr>
  85. <th>result</th>
  86. <th>highlight.php</th>
  87. <th>highlight.js</th>
  88. </tr>
  89. <?php
  90. foreach ($hl->listLanguages() as $languageId) {
  91. $snippet = file_get_contents("../test/detect/{$languageId}/default.txt");
  92. $r = $hl->highlightAuto($snippet);
  93. ?>
  94. <tr>
  95. <td class="signal"><p><?=$r->language?></p><p>pass</p></td>
  96. <td>
  97. <p><?=$r->language?>: <?=$r->relevance?></p>
  98. <p><?=$r->secondBest->language?>: <?=$r->secondBest->relevance?></p>
  99. <pre><div class=" hljs <?=$r->language?>"><?= $r->value?></div></pre>
  100. </td>
  101. <td class="js">
  102. <p>&nbsp;</p>
  103. <p>&nbsp;</p>
  104. <pre><code><?=htmlentities($snippet)?></code></pre>
  105. </td>
  106. </tr>
  107. <?php
  108. }
  109. ?>
  110. </body>
  111. </html>
  112. <?php
  113. error_log("Highlighting took ".(microtime(true)-$start)." seconds");
  114. ?>