UNPKG

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