prism-smarty.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /* TODO
  2. Add support for variables inside double quoted strings
  3. Add support for {php}
  4. */
  5. (function(Prism) {
  6. var smarty_pattern = /\{\*[\w\W]+?\*\}|\{[\w\W]+?\}/g;
  7. var smarty_litteral_start = '{literal}';
  8. var smarty_litteral_end = '{/literal}';
  9. var smarty_litteral_mode = false;
  10. Prism.languages.smarty = Prism.languages.extend('markup', {
  11. 'smarty': {
  12. pattern: smarty_pattern,
  13. inside: {
  14. 'delimiter': {
  15. pattern: /^\{|\}$/i,
  16. alias: 'punctuation'
  17. },
  18. 'string': /(["'])(\\?.)*?\1/,
  19. 'number': /\b-?(0x[\dA-Fa-f]+|\d*\.?\d+([Ee]-?\d+)?)\b/,
  20. 'variable': [
  21. /\$(?!\d)\w+/,
  22. /#(?!\d)\w+#/,
  23. {
  24. pattern: /(\.|->)(?!\d)\w+/,
  25. lookbehind: true
  26. },
  27. {
  28. pattern: /(\[)(?!\d)\w+(?=\])/,
  29. lookbehind: true
  30. }
  31. ],
  32. 'function': [
  33. {
  34. pattern: /(\|\s*)@?(?!\d)\w+/,
  35. lookbehind: true
  36. },
  37. /^\/?(?!\d)\w+/,
  38. /(?!\d)\w+(?=\()/
  39. ],
  40. 'attr-name': {
  41. // Value is made optional because it may have already been tokenized
  42. pattern: /\w+\s*=\s*(?:(?!\d)\w+)?/,
  43. inside: {
  44. "variable": {
  45. pattern: /(=\s*)(?!\d)\w+/,
  46. lookbehind: true
  47. },
  48. "punctuation": /=/
  49. }
  50. },
  51. 'punctuation': /[\[\]().,=\|:`]|\->/,
  52. 'operator': [
  53. /[+\-*\/%]|===?|[!<>]=?|&&|\|\|/,
  54. /\bis\s+(?:not\s+)?(?:div|even|odd)(?:\s+by)?\b/,
  55. /\b(?:eq|neq?|gt|lt|gt?e|lt?e|not|mod|or|and)\b/
  56. ],
  57. 'keyword': /\b(?:false|off|on|no|true|yes)\b/
  58. }
  59. }
  60. });
  61. // Comments are inserted at top so that they can
  62. // surround markup
  63. Prism.languages.insertBefore('smarty', 'tag', {
  64. 'smarty-comment': {
  65. pattern: /\{\*[\w\W]*?\*\}/,
  66. alias: ['smarty','comment']
  67. }
  68. });
  69. // Tokenize all inline Smarty expressions
  70. Prism.hooks.add('before-highlight', function(env) {
  71. if (env.language !== 'smarty') {
  72. return;
  73. }
  74. env.tokenStack = [];
  75. env.backupCode = env.code;
  76. env.code = env.code.replace(smarty_pattern, function(match) {
  77. // Smarty tags inside {literal} block are ignored
  78. if(match === smarty_litteral_end) {
  79. smarty_litteral_mode = false;
  80. }
  81. if(!smarty_litteral_mode) {
  82. if(match === smarty_litteral_start) {
  83. smarty_litteral_mode = true;
  84. }
  85. env.tokenStack.push(match);
  86. return '___SMARTY' + env.tokenStack.length + '___';
  87. }
  88. return match;
  89. });
  90. });
  91. // Restore env.code for other plugins (e.g. line-numbers)
  92. Prism.hooks.add('before-insert', function(env) {
  93. if (env.language === 'smarty') {
  94. env.code = env.backupCode;
  95. delete env.backupCode;
  96. }
  97. });
  98. // Re-insert the tokens after highlighting
  99. // and highlight them with defined grammar
  100. Prism.hooks.add('after-highlight', function(env) {
  101. if (env.language !== 'smarty') {
  102. return;
  103. }
  104. for (var i = 0, t; t = env.tokenStack[i]; i++) {
  105. env.highlightedCode = env.highlightedCode.replace('___SMARTY' + (i + 1) + '___', Prism.highlight(t, env.grammar, 'smarty'));
  106. }
  107. env.element.innerHTML = env.highlightedCode;
  108. });
  109. }(Prism));