export.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. "use strict";
  2. require(["dojo/node!fs", "dojox/json/ref", "dojo/_base/kernel"], function(fs, ref, kernel) {
  3. const nodeRequire = kernel.global.require && kernel.global.require.nodeRequire;
  4. const HIGHLIGHT_DIR = dojo.config.highlightJsDir;
  5. const CWD = dojo.config.cwd;
  6. const LANGS_W_DEPS = ['arduino.js'];
  7. const cloneDeep = nodeRequire(`${CWD}/lodash.cloneDeep.js`);
  8. const hljs = nodeRequire(`${HIGHLIGHT_DIR}/highlight.js`);
  9. /**
  10. * Translate any RegExp objects that may exist in a language definition into a string representation.
  11. *
  12. * @param {Object} lang
  13. * @param {number} nestingLevel
  14. */
  15. function regexToStr(lang, nestingLevel = 0) {
  16. // Max recursion level
  17. if (nestingLevel > 15) {
  18. return;
  19. }
  20. for (const key in lang) {
  21. if (lang[key] instanceof RegExp) {
  22. lang[key] = lang[key].source;
  23. } else if (typeof lang[key] === 'object') {
  24. regexToStr(lang[key], nestingLevel + 1);
  25. }
  26. }
  27. }
  28. /**
  29. * PCRE does not support the `\uXXXX` syntax, so we must use `\x{XXXX}` instead.
  30. *
  31. * @param {string} s
  32. *
  33. * @see https://www.regular-expressions.info/unicode.html#codepoint
  34. *
  35. * @returns {string}
  36. */
  37. function jsUnicodeToPhpUnicode(s) {
  38. return s.replace(/\\u([0-9A-Fa-f]+)/g, "\\x{$1}");
  39. }
  40. /**
  41. * Load a language and export it as a translated string to STDOUT.
  42. *
  43. * @param {string} lang
  44. */
  45. function exportLang(lang) {
  46. const x = nodeRequire(`${HIGHLIGHT_DIR}/languages/${lang}.js`);
  47. const l = cloneDeep(x(hljs));
  48. regexToStr(l);
  49. hljs.registerLanguage(lang, x);
  50. console.log(lang);
  51. console.log(jsUnicodeToPhpUnicode(dojox.json.ref.toJson(l)));
  52. }
  53. fs.readdir(`${HIGHLIGHT_DIR}/languages/`,function(err, files) {
  54. if (err) {
  55. throw err;
  56. }
  57. // Load all of the languages that don't extend other languages
  58. files.forEach(function(file) {
  59. if (file === ".DS_Store" || LANGS_W_DEPS.indexOf(file) >= 0) {
  60. return;
  61. }
  62. exportLang(file.replace(/\.js$/, ""));
  63. });
  64. // These languages extend other languages, so we need to make sure that
  65. // they are loaded *after* all the standard languages are loaded.
  66. LANGS_W_DEPS.forEach(function(file) {
  67. exportLang(file.replace(/\.js$/, ""));
  68. });
  69. });
  70. });