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