prism-smarty.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. (function (Prism) {
  2. Prism.languages.smarty = {
  3. 'comment': {
  4. pattern: /^\{\*[\s\S]*?\*\}/,
  5. greedy: true
  6. },
  7. 'embedded-php': {
  8. pattern: /^\{php\}[\s\S]*?\{\/php\}/,
  9. greedy: true,
  10. inside: {
  11. 'smarty': {
  12. pattern: /^\{php\}|\{\/php\}$/,
  13. inside: null // see below
  14. },
  15. 'php': {
  16. pattern: /[\s\S]+/,
  17. alias: 'language-php',
  18. inside: Prism.languages.php
  19. }
  20. }
  21. },
  22. 'string': [
  23. {
  24. pattern: /"(?:\\.|[^"\\\r\n])*"/,
  25. greedy: true,
  26. inside: {
  27. 'interpolation': {
  28. pattern: /\{[^{}]*\}|`[^`]*`/,
  29. inside: {
  30. 'interpolation-punctuation': {
  31. pattern: /^[{`]|[`}]$/,
  32. alias: 'punctuation'
  33. },
  34. 'expression': {
  35. pattern: /[\s\S]+/,
  36. inside: null // see below
  37. }
  38. }
  39. },
  40. 'variable': /\$\w+/
  41. }
  42. },
  43. {
  44. pattern: /'(?:\\.|[^'\\\r\n])*'/,
  45. greedy: true
  46. },
  47. ],
  48. 'keyword': {
  49. pattern: /(^\{\/?)[a-z_]\w*\b(?!\()/i,
  50. lookbehind: true,
  51. greedy: true
  52. },
  53. 'delimiter': {
  54. pattern: /^\{\/?|\}$/,
  55. greedy: true,
  56. alias: 'punctuation'
  57. },
  58. 'number': /\b0x[\dA-Fa-f]+|(?:\b\d+(?:\.\d*)?|\B\.\d+)(?:[Ee][-+]?\d+)?/,
  59. 'variable': [
  60. /\$(?!\d)\w+/,
  61. /#(?!\d)\w+#/,
  62. {
  63. pattern: /(\.|->|\w\s*=)(?!\d)\w+\b(?!\()/,
  64. lookbehind: true
  65. },
  66. {
  67. pattern: /(\[)(?!\d)\w+(?=\])/,
  68. lookbehind: true
  69. }
  70. ],
  71. 'function': {
  72. pattern: /(\|\s*)@?[a-z_]\w*|\b[a-z_]\w*(?=\()/i,
  73. lookbehind: true
  74. },
  75. 'attr-name': /\b[a-z_]\w*(?=\s*=)/i,
  76. 'boolean': /\b(?:false|no|off|on|true|yes)\b/,
  77. 'punctuation': /[\[\](){}.,:`]|->/,
  78. 'operator': [
  79. /[+\-*\/%]|==?=?|[!<>]=?|&&|\|\|?/,
  80. /\bis\s+(?:not\s+)?(?:div|even|odd)(?:\s+by)?\b/,
  81. /\b(?:and|eq|gt?e|gt|lt?e|lt|mod|neq?|not|or)\b/
  82. ]
  83. };
  84. Prism.languages.smarty['embedded-php'].inside.smarty.inside = Prism.languages.smarty;
  85. Prism.languages.smarty.string[0].inside.interpolation.inside.expression.inside = Prism.languages.smarty;
  86. var string = /"(?:\\.|[^"\\\r\n])*"|'(?:\\.|[^'\\\r\n])*'/;
  87. var smartyPattern = RegExp(
  88. // comments
  89. /\{\*[\s\S]*?\*\}/.source +
  90. '|' +
  91. // php tags
  92. /\{php\}[\s\S]*?\{\/php\}/.source +
  93. '|' +
  94. // smarty blocks
  95. /\{(?:[^{}"']|<str>|\{(?:[^{}"']|<str>|\{(?:[^{}"']|<str>)*\})*\})*\}/.source
  96. .replace(/<str>/g, function () { return string.source; }),
  97. 'g'
  98. );
  99. // Tokenize all inline Smarty expressions
  100. Prism.hooks.add('before-tokenize', function (env) {
  101. var smartyLiteralStart = '{literal}';
  102. var smartyLiteralEnd = '{/literal}';
  103. var smartyLiteralMode = false;
  104. Prism.languages['markup-templating'].buildPlaceholders(env, 'smarty', smartyPattern, function (match) {
  105. // Smarty tags inside {literal} block are ignored
  106. if (match === smartyLiteralEnd) {
  107. smartyLiteralMode = false;
  108. }
  109. if (!smartyLiteralMode) {
  110. if (match === smartyLiteralStart) {
  111. smartyLiteralMode = true;
  112. }
  113. return true;
  114. }
  115. return false;
  116. });
  117. });
  118. // Re-insert the tokens after tokenizing
  119. Prism.hooks.add('after-tokenize', function (env) {
  120. Prism.languages['markup-templating'].tokenizePlaceholders(env, 'smarty');
  121. });
  122. }(Prism));