prism-soy.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. (function (Prism) {
  2. var stringPattern = /(["'])(?:\\(?:\r\n|[\s\S])|(?!\1)[^\\\r\n])*\1/;
  3. var numberPattern = /\b\d+(?:\.\d+)?(?:[eE][+-]?\d+)?\b|\b0x[\dA-F]+\b/;
  4. Prism.languages.soy = {
  5. 'comment': [
  6. /\/\*[\s\S]*?\*\//,
  7. {
  8. pattern: /(\s)\/\/.*/,
  9. lookbehind: true,
  10. greedy: true
  11. }
  12. ],
  13. 'command-arg': {
  14. pattern: /(\{+\/?\s*(?:alias|call|delcall|delpackage|deltemplate|namespace|template)\s+)\.?[\w.]+/,
  15. lookbehind: true,
  16. alias: 'string',
  17. inside: {
  18. 'punctuation': /\./
  19. }
  20. },
  21. 'parameter': {
  22. pattern: /(\{+\/?\s*@?param\??\s+)\.?[\w.]+/,
  23. lookbehind: true,
  24. alias: 'variable'
  25. },
  26. 'keyword': [
  27. {
  28. pattern: /(\{+\/?[^\S\r\n]*)(?:\\[nrt]|alias|call|case|css|default|delcall|delpackage|deltemplate|else(?:if)?|fallbackmsg|for(?:each)?|if(?:empty)?|lb|let|literal|msg|namespace|nil|@?param\??|rb|sp|switch|template|xid)/,
  29. lookbehind: true
  30. },
  31. /\b(?:any|as|attributes|bool|css|float|html|in|int|js|list|map|null|number|string|uri)\b/
  32. ],
  33. 'delimiter': {
  34. pattern: /^\{+\/?|\/?\}+$/,
  35. alias: 'punctuation'
  36. },
  37. 'property': /\w+(?==)/,
  38. 'variable': {
  39. pattern: /\$[^\W\d]\w*(?:\??(?:\.\w+|\[[^\]]+\]))*/,
  40. inside: {
  41. 'string': {
  42. pattern: stringPattern,
  43. greedy: true
  44. },
  45. 'number': numberPattern,
  46. 'punctuation': /[\[\].?]/
  47. }
  48. },
  49. 'string': {
  50. pattern: stringPattern,
  51. greedy: true
  52. },
  53. 'function': [
  54. /\w+(?=\()/,
  55. {
  56. pattern: /(\|[^\S\r\n]*)\w+/,
  57. lookbehind: true
  58. }
  59. ],
  60. 'boolean': /\b(?:false|true)\b/,
  61. 'number': numberPattern,
  62. 'operator': /\?:?|<=?|>=?|==?|!=|[+*/%-]|\b(?:and|not|or)\b/,
  63. 'punctuation': /[{}()\[\]|.,:]/
  64. };
  65. // Tokenize all inline Soy expressions
  66. Prism.hooks.add('before-tokenize', function (env) {
  67. var soyPattern = /\{\{.+?\}\}|\{.+?\}|\s\/\/.*|\/\*[\s\S]*?\*\//g;
  68. var soyLitteralStart = '{literal}';
  69. var soyLitteralEnd = '{/literal}';
  70. var soyLitteralMode = false;
  71. Prism.languages['markup-templating'].buildPlaceholders(env, 'soy', soyPattern, function (match) {
  72. // Soy tags inside {literal} block are ignored
  73. if (match === soyLitteralEnd) {
  74. soyLitteralMode = false;
  75. }
  76. if (!soyLitteralMode) {
  77. if (match === soyLitteralStart) {
  78. soyLitteralMode = true;
  79. }
  80. return true;
  81. }
  82. return false;
  83. });
  84. });
  85. // Re-insert the tokens after tokenizing
  86. Prism.hooks.add('after-tokenize', function (env) {
  87. Prism.languages['markup-templating'].tokenizePlaceholders(env, 'soy');
  88. });
  89. }(Prism));