-
-
Notifications
You must be signed in to change notification settings - Fork 36
/
cooklang.js
145 lines (143 loc) · 3.81 KB
/
cooklang.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// @ts-nocheck
cooklang.displayName = 'cooklang'
cooklang.aliases = []
/** @type {import('../core.js').Syntax} */
export default function cooklang(Prism) {
;(function (Prism) {
// see https://github.com/cooklang/spec/blob/main/EBNF.md
var single_token_suffix = /(?:(?!\s)[\d$+<=a-zA-Z\x80-\uFFFF])+/.source
var multi_token_infix = /[^{}@#]+/.source
var multi_token_suffix = /\{[^}#@]*\}/.source
var multi_token = multi_token_infix + multi_token_suffix
var timer_units = /(?:h|hours|hrs|m|min|minutes)/.source
var amount_group_impl = {
pattern: /\{[^{}]*\}/,
inside: {
amount: {
pattern: /([\{|])[^{}|*%]+/,
lookbehind: true,
alias: 'number'
},
unit: {
pattern: /(%)[^}]+/,
lookbehind: true,
alias: 'symbol'
},
'servings-scaler': {
pattern: /\*/,
alias: 'operator'
},
'servings-alternative-separator': {
pattern: /\|/,
alias: 'operator'
},
'unit-separator': {
pattern: /(?:%|(\*)%)/,
lookbehind: true,
alias: 'operator'
},
punctuation: /[{}]/
}
}
Prism.languages.cooklang = {
comment: {
// [- comment -]
// -- comment
pattern: /\[-[\s\S]*?-\]|--.*/,
greedy: true
},
meta: {
// >> key: value
pattern: />>.*:.*/,
inside: {
property: {
// key:
pattern: /(>>\s*)[^\s:](?:[^:]*[^\s:])?/,
lookbehind: true
}
}
},
'cookware-group': {
// #...{...}, #...
pattern: new RegExp(
'#(?:' + multi_token + '|' + single_token_suffix + ')'
),
inside: {
cookware: {
pattern: new RegExp('(^#)(?:' + multi_token_infix + ')'),
lookbehind: true,
alias: 'variable'
},
'cookware-keyword': {
pattern: /^#/,
alias: 'keyword'
},
'quantity-group': {
pattern: new RegExp(/\{[^{}@#]*\}/),
inside: {
quantity: {
pattern: new RegExp(/(^\{)/.source + multi_token_infix),
lookbehind: true,
alias: 'number'
},
punctuation: /[{}]/
}
}
}
},
'ingredient-group': {
// @...{...}, @...
pattern: new RegExp(
'@(?:' + multi_token + '|' + single_token_suffix + ')'
),
inside: {
ingredient: {
pattern: new RegExp('(^@)(?:' + multi_token_infix + ')'),
lookbehind: true,
alias: 'variable'
},
'ingredient-keyword': {
pattern: /^@/,
alias: 'keyword'
},
'amount-group': amount_group_impl
}
},
'timer-group': {
// ~timer{...}
// eslint-disable-next-line regexp/sort-alternatives
pattern: /~(?!\s)[^@#~{}]*\{[^{}]*\}/,
inside: {
timer: {
pattern: /(^~)[^{]+/,
lookbehind: true,
alias: 'variable'
},
'duration-group': {
// {...}
pattern: /\{[^{}]*\}/,
inside: {
punctuation: /[{}]/,
unit: {
pattern: new RegExp(
/(%\s*)/.source + timer_units + /\b/.source
),
lookbehind: true,
alias: 'symbol'
},
operator: /%/,
duration: {
pattern: /\d+/,
alias: 'number'
}
}
},
'timer-keyword': {
pattern: /^~/,
alias: 'keyword'
}
}
}
}
})(Prism)
}