UNPKG

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