-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrammar_markup.ts
More file actions
226 lines (219 loc) · 5.13 KB
/
grammar_markup.ts
File metadata and controls
226 lines (219 loc) · 5.13 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
import type {
SyntaxStyler,
AddSyntaxGrammar,
SyntaxGrammarRaw,
SyntaxGrammarToken,
SyntaxGrammar,
} from './syntax_styler.js';
/**
* Based on Prism (https://github.com/PrismJS/prism)
* by Lea Verou (https://lea.verou.me/)
*
* MIT license
*
* @see LICENSE
*/
export const add_grammar_markup: AddSyntaxGrammar = (syntax_styler) => {
const grammar_markup = {
comment: {
pattern: /<!--(?:(?!<!--)[\s\S])*?-->/,
greedy: true,
},
processing_instruction: {
pattern: /<\?[\s\S]+?\?>/,
greedy: true,
},
// https://www.w3.org/TR/xml/#NT-doctypedecl
doctype: {
pattern: /<!DOCTYPE[^>]*>/i,
greedy: true,
},
cdata: {
pattern: /<!\[CDATA\[[\s\S]*?\]\]>/i,
greedy: true,
},
tag: {
pattern:
/<\/?(?!\d)[^\s>/=$<%]+(?:\s(?:\s*[^\s>/=]+(?:\s*=\s*(?:"[^"]*"|'[^']*'|[^\s'">=]+(?=[\s>]))|(?=[\s/>])))+)?\s*\/?>/,
greedy: true,
inside: {
tag: {
pattern: /^<\/?[^\s>/]+/,
inside: {
punctuation: {
pattern: /^<\/?/,
alias: 'tag_punctuation',
},
namespace: /^[^\s>/:]+:/,
},
},
special_attr: [],
attr_value: {
pattern: /=\s*(?:"[^"]*"|'[^']*'|[^\s'">=]+)/,
inside: {
punctuation: [
{
pattern: /^=/,
alias: 'attr_equals',
},
{
pattern: /^(\s*)["']|["']$/,
lookbehind: true,
alias: 'attr_quote',
},
],
entity: undefined as any, // see below
},
},
punctuation: {
pattern: /\/?>/,
alias: 'tag_punctuation',
},
attr_name: {
pattern: /[^\s>/]+/,
inside: {
namespace: /^[^\s>/:]+:/,
},
},
},
},
entity: [
{
pattern: /&[\da-z]{1,8};/i,
alias: 'named_entity',
},
/&#x?[\da-f]{1,8};/i,
],
} satisfies SyntaxGrammarRaw;
grammar_markup.tag.inside.attr_value.inside.entity = grammar_markup.entity;
syntax_styler.add_lang('markup', grammar_markup, ['html', 'mathml', 'svg']);
syntax_styler.add_extended_lang('markup', 'xml', {}, ['ssml', 'atom', 'rss']);
};
/**
* Adds an inlined language to markup.
*
* An example of an inlined language is CSS with `<style>` tags.
*
* @param syntax_styler - the `SyntaxStyler` instance to modify
* @param tag_name - the name of the tag that contains the inlined language, treated as case insensitive
* @param lang - the language key
* @param inside_lang - the language to insert into, defaults to `'markup'`
*/
export const grammar_markup_add_inlined = (
syntax_styler: SyntaxStyler,
tag_name: string,
lang: string,
inside_lang = 'markup',
): void => {
const lang_key = 'lang_' + lang;
syntax_styler.grammar_insert_before(inside_lang, 'cdata', {
[tag_name]: {
pattern: RegExp(
/(<__[^>]*>)(?:<!\[CDATA\[(?:[^\]]|\](?!\]>))*\]\]>|(?!<!\[CDATA\[)[\s\S])*?(?=<\/__>)/.source.replace(
/__/g,
() => {
return tag_name;
},
),
'i',
),
lookbehind: true,
greedy: true,
inside: {
included_cdata: {
pattern: /<!\[CDATA\[[\s\S]*?\]\]>/i,
inside: {
cdata: /^<!\[CDATA\[|\]\]>$/i,
[lang_key]: {
pattern: /(^<!\[CDATA\[)[\s\S]+?(?=\]\]>$)/i,
lookbehind: true,
inside: syntax_styler.get_lang(lang),
},
},
},
[lang_key]: {
pattern: /[\s\S]+/,
inside: syntax_styler.get_lang(lang),
},
},
},
});
};
/**
* Adds a pattern to style languages embedded in HTML attributes.
*
* An example of an inlined language is CSS with `style` attributes.
*
* @param syntax_styler - the `SyntaxStyler` instance to modify
* @param attr_name - the name of the attribute that contains the inlined language, treated as case insensitive
* @param lang - the language key
*/
export const grammar_markup_add_attribute = (
syntax_styler: SyntaxStyler,
attr_name: string,
lang: string,
): void => {
// After normalization, grammar.tag is an array of SyntaxGrammarToken
const markup_grammar = syntax_styler.get_lang('markup');
const tag_patterns = markup_grammar.tag;
const tag_inside = tag_patterns![0]!.inside!;
tag_inside.special_attr!.push({
pattern: RegExp(
/(^|["'\s])/.source +
'(?:' +
attr_name +
')' +
/\s*=\s*(?:"[^"]*"|'[^']*'|[^\s'">=]+(?=[\s>]))/.source,
'i',
),
lookbehind: true,
greedy: false,
alias: [],
inside: {
attr_name: [
{
pattern: /^[^\s=]+/,
lookbehind: false,
greedy: false,
alias: [],
inside: null,
},
],
attr_value: [
{
pattern: /=[\s\S]+/,
lookbehind: false,
greedy: false,
alias: [],
inside: {
value: [
{
pattern: /(^=\s*(["']|(?!["'])))\S[\s\S]*(?=\2$)/,
lookbehind: true,
greedy: false,
alias: [lang, 'lang_' + lang], // TODO remove this alias?
inside: syntax_styler.get_lang(lang),
},
],
punctuation: [
{
pattern: /^=/,
lookbehind: false,
greedy: false,
alias: ['attr_equals'],
inside: null,
},
{
pattern: /"|'/,
lookbehind: false,
greedy: false,
alias: ['attr_quote'],
inside: null,
},
],
} as SyntaxGrammar,
},
],
} as SyntaxGrammar,
} satisfies SyntaxGrammarToken);
};