prism-smarty.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /* TODO
  2. Add support for variables inside double quoted strings
  3. Add support for {php}
  4. */
  5. (function(Prism) {
  6. Prism.languages.smarty = {
  7. 'comment': /\{\*[\s\S]*?\*\}/,
  8. 'delimiter': {
  9. pattern: /^\{|\}$/i,
  10. alias: 'punctuation'
  11. },
  12. 'string': /(["'])(?:\\.|(?!\1)[^\\\r\n])*\1/,
  13. 'number': /\b0x[\dA-Fa-f]+|(?:\b\d+\.?\d*|\B\.\d+)(?:[Ee][-+]?\d+)?/,
  14. 'variable': [
  15. /\$(?!\d)\w+/,
  16. /#(?!\d)\w+#/,
  17. {
  18. pattern: /(\.|->)(?!\d)\w+/,
  19. lookbehind: true
  20. },
  21. {
  22. pattern: /(\[)(?!\d)\w+(?=\])/,
  23. lookbehind: true
  24. }
  25. ],
  26. 'function': [
  27. {
  28. pattern: /(\|\s*)@?(?!\d)\w+/,
  29. lookbehind: true
  30. },
  31. /^\/?(?!\d)\w+/,
  32. /(?!\d)\w+(?=\()/
  33. ],
  34. 'attr-name': {
  35. // Value is made optional because it may have already been tokenized
  36. pattern: /\w+\s*=\s*(?:(?!\d)\w+)?/,
  37. inside: {
  38. "variable": {
  39. pattern: /(=\s*)(?!\d)\w+/,
  40. lookbehind: true
  41. },
  42. "operator": /=/
  43. }
  44. },
  45. 'punctuation': [
  46. /[\[\]().,:`]|->/
  47. ],
  48. 'operator': [
  49. /[+\-*\/%]|==?=?|[!<>]=?|&&|\|\|?/,
  50. /\bis\s+(?:not\s+)?(?:div|even|odd)(?:\s+by)?\b/,
  51. /\b(?:eq|neq?|gt|lt|gt?e|lt?e|not|mod|or|and)\b/
  52. ],
  53. 'keyword': /\b(?:false|off|on|no|true|yes)\b/
  54. };
  55. // Tokenize all inline Smarty expressions
  56. Prism.hooks.add('before-tokenize', function(env) {
  57. var smartyPattern = /\{\*[\s\S]*?\*\}|\{[\s\S]+?\}/g;
  58. var smartyLitteralStart = '{literal}';
  59. var smartyLitteralEnd = '{/literal}';
  60. var smartyLitteralMode = false;
  61. Prism.languages['markup-templating'].buildPlaceholders(env, 'smarty', smartyPattern, function (match) {
  62. // Smarty tags inside {literal} block are ignored
  63. if(match === smartyLitteralEnd) {
  64. smartyLitteralMode = false;
  65. }
  66. if(!smartyLitteralMode) {
  67. if(match === smartyLitteralStart) {
  68. smartyLitteralMode = true;
  69. }
  70. return true;
  71. }
  72. return false;
  73. });
  74. });
  75. // Re-insert the tokens after tokenizing
  76. Prism.hooks.add('after-tokenize', function(env) {
  77. Prism.languages['markup-templating'].tokenizePlaceholders(env, 'smarty');
  78. });
  79. }(Prism));