line-numbers.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. /* Copyright (c) 2018 Geert Bergman (geertscrivo.nl), highlight.php
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. *
  7. * 1. Redistributions of source code must retain the above copyright notice,
  8. * this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright notice,
  10. * this list of conditions and the following disclaimer in the documentation
  11. * and/or other materials provided with the distribution.
  12. * 3. Neither the name of "highlight.js", "highlight.php", nor the names of its
  13. * contributors may be used to endorse or promote products derived from this
  14. * software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  20. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  21. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  22. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  23. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  24. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  25. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  26. * POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. require_once "../Highlight/Autoloader.php";
  29. require_once "../HighlightUtilities/functions.php";
  30. spl_autoload_register("Highlight\\Autoloader::load");
  31. ?>
  32. <html lang="en">
  33. <head>
  34. <title>highlight.php line number example</title>
  35. <link rel="stylesheet" type="text/css" href="../styles/default.css">
  36. <style type="text/css">
  37. table {
  38. border-collapse: collapse;
  39. border-spacing: 0;
  40. font-family: sans-serif;
  41. width: 100%;
  42. }
  43. th, td {
  44. border: none;
  45. overflow: auto;
  46. }
  47. td[data-line-number] {
  48. border-right: 1px solid grey;
  49. padding: 0 15px;
  50. text-align: right;
  51. width: 1%;
  52. }
  53. td[data-line-number]::before {
  54. color: rgba(0, 0, 0, 0.5);
  55. content: attr(data-line-number);
  56. display: block;
  57. }
  58. .blob-code {
  59. padding: 5px 15px;
  60. }
  61. </style>
  62. </head>
  63. <body>
  64. <h1>Render highlight.php with line numbers</h1>
  65. <p>
  66. Here's a demo of rendering the result of highlight.php with line numbers. With the <code>HighlightUtilities\splitCodeIntoArray()</code> function.
  67. </p>
  68. <?php
  69. $hl = new Highlight\Highlighter();
  70. $snippet = file_get_contents("../test/detect/php/default.txt");
  71. $result = $hl->highlight("php", $snippet);
  72. $lines = HighlightUtilities\splitCodeIntoArray($result->value);
  73. ?>
  74. <table>
  75. <tbody>
  76. <?php foreach ($lines as $number => $line): ?>
  77. <tr>
  78. <td id="L<?= $number; ?>" data-line-number="<?= $number; ?>"></td>
  79. <td id="LC<?= $number; ?>" class="blob-code">
  80. <pre><code><?= $line; ?></code></pre>
  81. </td>
  82. </tr>
  83. <?php endforeach; ?>
  84. </tbody>
  85. </table>
  86. </body>
  87. </html>