get_language_definitions.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. // Extract language definitions (JSON strings) from the large file that was
  31. // created using 'node launcher.js' and create a JSON file for each language.
  32. $f = file("languages.dat");
  33. $patches = array(
  34. // The expression \\/: causes issues for PREG due to the / and the : having special meaning, therefore we use \Q and
  35. // \E to have PREG treat them as literal characters
  36. "1c" => array(
  37. array("\\\\\\\\/:", "\\\\Q\\\\/:\\\\E"),
  38. ),
  39. // The expression []{}%#'" should be treated as a list of invalid characters, however the [] is special in PREG so
  40. // we use \Q and \E to have PREG treat them as literal characters
  41. "ada" => array(
  42. array("[]{}%#'\\\"", "\\\\Q[]{}%#'\\\"\\\\E"),
  43. ),
  44. // WTF, any ideas anyone?
  45. "mercury" => array(array("\\\\\\\/", "\\\\\\\\\\\/")),
  46. // The expression [^] is not allowed in PREG
  47. "lisp" => array(array("[^]", "[^|]")),
  48. // There's a typo in the Swift translation file
  49. "swift" => array(array(
  50. '{02B80}-9',
  51. '{02B8}0-9',
  52. )),
  53. );
  54. for ($i = 0; $i < count($f); $i += 2) {
  55. if (!isset($f[$i + 1])) {
  56. continue;
  57. }
  58. $languageName = trim($f[$i]);
  59. $jsonLangDef = $f[$i + 1];
  60. // The `-` character must be escaped in while in `[]`. This is enforced in PHP 7.3+
  61. // https://wiki.php.net/rfc/pcre2-migration
  62. // https://github.com/php/php-src/pull/2857
  63. $jsonLangDef = preg_replace('/(\[[^:]*?\w)(-)([^a-zA-Z0-9\\\\]+?)/um', '$1\\\\\\-$3', $jsonLangDef);
  64. if (!$languageName) {
  65. die(sprintf("ERROR: No language name on line %d\n", ($i + 1)));
  66. }
  67. if (!@json_decode($jsonLangDef)) {
  68. die(sprintf("ERROR: Invalid JSON data on line %d\n", ($i + 2)));
  69. }
  70. if (isset($patches[$languageName])) {
  71. foreach ($patches[$languageName] as $j => $patch) {
  72. $patched = str_replace($patch[0], $patch[1], $jsonLangDef);
  73. if ($jsonLangDef === $patched) {
  74. printf("Patch %d for %s was not applied and likely unnecessary\n", $j, $languageName);
  75. }
  76. $jsonLangDef = $patched;
  77. }
  78. }
  79. $jsonLangDef = json_encode(json_decode($jsonLangDef), JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT) . "\n";
  80. if (!file_put_contents("../Highlight/languages/{$languageName}.json", $jsonLangDef)) {
  81. die("ERROR: Couldn't write to file.\n");
  82. }
  83. }