-
-
Notifications
You must be signed in to change notification settings - Fork 36
/
soy.js
107 lines (104 loc) · 3.1 KB
/
soy.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
// @ts-nocheck
import refractorMarkupTemplating from './markup-templating.js'
soy.displayName = 'soy'
soy.aliases = []
/** @type {import('../core.js').Syntax} */
export default function soy(Prism) {
Prism.register(refractorMarkupTemplating)
;(function (Prism) {
var stringPattern = /(["'])(?:\\(?:\r\n|[\s\S])|(?!\1)[^\\\r\n])*\1/
var numberPattern = /\b\d+(?:\.\d+)?(?:[eE][+-]?\d+)?\b|\b0x[\dA-F]+\b/
Prism.languages.soy = {
comment: [
/\/\*[\s\S]*?\*\//,
{
pattern: /(\s)\/\/.*/,
lookbehind: true,
greedy: true
}
],
'command-arg': {
pattern:
/(\{+\/?\s*(?:alias|call|delcall|delpackage|deltemplate|namespace|template)\s+)\.?[\w.]+/,
lookbehind: true,
alias: 'string',
inside: {
punctuation: /\./
}
},
parameter: {
pattern: /(\{+\/?\s*@?param\??\s+)\.?[\w.]+/,
lookbehind: true,
alias: 'variable'
},
keyword: [
{
pattern:
/(\{+\/?[^\S\r\n]*)(?:\\[nrt]|alias|call|case|css|default|delcall|delpackage|deltemplate|else(?:if)?|fallbackmsg|for(?:each)?|if(?:empty)?|lb|let|literal|msg|namespace|nil|@?param\??|rb|sp|switch|template|xid)/,
lookbehind: true
},
/\b(?:any|as|attributes|bool|css|float|html|in|int|js|list|map|null|number|string|uri)\b/
],
delimiter: {
pattern: /^\{+\/?|\/?\}+$/,
alias: 'punctuation'
},
property: /\w+(?==)/,
variable: {
pattern: /\$[^\W\d]\w*(?:\??(?:\.\w+|\[[^\]]+\]))*/,
inside: {
string: {
pattern: stringPattern,
greedy: true
},
number: numberPattern,
punctuation: /[\[\].?]/
}
},
string: {
pattern: stringPattern,
greedy: true
},
function: [
/\w+(?=\()/,
{
pattern: /(\|[^\S\r\n]*)\w+/,
lookbehind: true
}
],
boolean: /\b(?:false|true)\b/,
number: numberPattern,
operator: /\?:?|<=?|>=?|==?|!=|[+*/%-]|\b(?:and|not|or)\b/,
punctuation: /[{}()\[\]|.,:]/
}
// Tokenize all inline Soy expressions
Prism.hooks.add('before-tokenize', function (env) {
var soyPattern = /\{\{.+?\}\}|\{.+?\}|\s\/\/.*|\/\*[\s\S]*?\*\//g
var soyLitteralStart = '{literal}'
var soyLitteralEnd = '{/literal}'
var soyLitteralMode = false
Prism.languages['markup-templating'].buildPlaceholders(
env,
'soy',
soyPattern,
function (match) {
// Soy tags inside {literal} block are ignored
if (match === soyLitteralEnd) {
soyLitteralMode = false
}
if (!soyLitteralMode) {
if (match === soyLitteralStart) {
soyLitteralMode = true
}
return true
}
return false
}
)
})
// Re-insert the tokens after tokenizing
Prism.hooks.add('after-tokenize', function (env) {
Prism.languages['markup-templating'].tokenizePlaceholders(env, 'soy')
})
})(Prism)
}