UNPKG

2.04 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 // Tokenize all inline Smarty expressions
59 Prism.hooks.add('before-tokenize', function(env) {
60 var smartyPattern = /\{\*[\s\S]*?\*\}|\{[\s\S]+?\}/g;
61 var smartyLitteralStart = '{literal}';
62 var smartyLitteralEnd = '{/literal}';
63 var smartyLitteralMode = false;
64
65 Prism.languages['markup-templating'].buildPlaceholders(env, 'smarty', smartyPattern, function (match) {
66 // Smarty tags inside {literal} block are ignored
67 if(match === smartyLitteralEnd) {
68 smartyLitteralMode = false;
69 }
70
71 if(!smartyLitteralMode) {
72 if(match === smartyLitteralStart) {
73 smartyLitteralMode = true;
74 }
75
76 return true;
77 }
78 return false;
79 });
80 });
81
82 // Re-insert the tokens after tokenizing
83 Prism.hooks.add('after-tokenize', function(env) {
84 Prism.languages['markup-templating'].tokenizePlaceholders(env, 'smarty');
85 });
86
87}(Prism));
\No newline at end of file