get_language_definitions.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. /* Copyright (c)
  3. * - 2013-2014, 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. /**
  32. * Extract language definitions (JSON strings) from the large file that was
  33. * created using 'get_language_definitions.php' and create a JSON file for
  34. * each language.
  35. */
  36. $f = file("languages.dat");
  37. $patches = Array(
  38. // Somehwere somehow the first percentage sign was lost
  39. "dos" => Array(Array("\"%[^ ]", "\"%%[^ ]")),
  40. // WTF, any ideas anyone?
  41. "mercury" => Array(Array("\\\\\\\/", "\\\\\\\\\\\/")),
  42. // The expression [^] is not allowed in PREG
  43. "lisp" => Array(Array("[^]", "[^|]")),
  44. // Just being plain lazy
  45. "xml" => Array(Array("subLanguage\":\"\"", "subLanguage\":\"javascript\"")),
  46. );
  47. for ($i=0; $i<count($f); $i+=2) {
  48. if (isset($f[$i+1])) {
  49. $fl = trim($f[$i]);
  50. $json = $f[$i+1];
  51. if (!$fl) {
  52. die("ERROR: No language name on line ".($i+1).".<br />\n");
  53. }
  54. if (!@json_decode($json)) {
  55. die("ERROR: invalid JSON data on line ".($i+2).".<br />\n");
  56. }
  57. if (isset($patches[$fl])) {
  58. foreach ($patches[$fl] as $patch) {
  59. $json = str_replace($patch[0], $patch[1], $json);
  60. echo "{$patch[0]}, {$patch[1]}\n{$json}";
  61. }
  62. }
  63. echo "Creating language file '{$fl}.json'.<br />\n";
  64. if (!file_put_contents("../Highlight/languages/{$fl}.json", $json)) {
  65. die("ERROR: Couldn't write to file.<br />\n");;
  66. }
  67. }
  68. }