Repository navigation
Expand file tree
/
Copy pathbuild.rs
More file actions
337 lines (312 loc) · 8.4 KB
/
Copy pathbuild.rs
File metadata and controls
337 lines (312 loc) · 8.4 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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
use std::{
env,
fs::{read_to_string, File},
io::{BufWriter, Write},
path::{Path, PathBuf},
};
// gen_html_entities {{{
fn gen_html_entities() {
let path = Path::new(&env::var("OUT_DIR").unwrap()).join("html_entities.rs");
let mut file = BufWriter::new(File::create(&path).unwrap());
let manifest_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
let source = manifest_dir.join("build/html_entities.txt");
let mut m = phf_codegen::Map::new();
let data = read_to_string(source).unwrap();
for line in data.lines() {
let line = line.trim();
if line.is_empty() || line.starts_with('#') {
continue;
}
let mut parts = line.split_whitespace();
if let (Some(entity), Some(value)) = (parts.next(), parts.next()) {
m.entry(entity, format!("\"{}\"", value));
}
}
write!(
&mut file,
"static HTML_ENTITIES: phf::Map<&'static str, &'static str> = {}",
m.build(),
)
.unwrap();
writeln!(&mut file, ";").unwrap();
}
// }}}
// gen_unicode_case_foldings {{{
fn gen_unicode_case_foldings() {
let path = Path::new(&env::var("OUT_DIR").unwrap()).join("unicode_case_foldings.rs");
let mut file = BufWriter::new(File::create(&path).unwrap());
let manifest_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
let source = manifest_dir.join("build/unicode_case_foldings.txt");
let mut m = phf_codegen::Map::new();
let data = read_to_string(source).unwrap();
for line in data.lines() {
let line = line.trim();
if line.is_empty() || line.starts_with('#') {
continue;
}
let mut parts = line.split_whitespace();
if let (Some(from), Some(to)) = (parts.next(), parts.next()) {
m.entry(decode_rust_u(from), format!("\"{}\"", to));
}
}
write!(
&mut file,
"static UNICODE_CASE_FOLDINGS: phf::Map<char, &'static str> = {}",
m.build(),
)
.unwrap();
writeln!(&mut file, ";").unwrap();
}
// }}} gen_unicode_case_foldings
// gen_html_attributes {{{
//
fn gen_html_attributes() {
let path = Path::new(&env::var("OUT_DIR").unwrap()).join("html_attributes.rs");
let mut file = BufWriter::new(File::create(&path).unwrap());
// Global attributes (HTML Living Standard / HTML5 global attributes set).
// Keep these as raw literals; we will ASCII-sort later.
let default_attrs: Vec<&str> = vec![
"accesskey",
"autocapitalize",
"autofocus",
"class",
"contenteditable",
"dir",
"draggable",
"enterkeyhint",
"hidden",
"id",
"inert",
"inputmode",
"is",
"itemid",
"itemprop",
"itemref",
"itemscope",
"itemtype",
"lang",
"part",
"role",
"slot",
"spellcheck",
"style",
"tabindex",
"title",
"translate",
];
// Element-specific attributes (fill this based on HTML5 spec).
// name: element name in uppercase for const prefix, e.g. "PARAGRAPH"
// attrs: attributes specific to that element, WITHOUT global/default ones.
//
// Example shown for <p>. (In practice <p> has no special attributes beyond global ones.)
const ELEMENT_ATTRS: &[(&str, &[&str])] = &[
("PARAGRAPH", &[]),
("BLOCKQUOTE", &["cite"]),
(
"THEMATIC_BREAK",
&["align", "color", "noshade", "size", "width"],
),
("LIST", &["reversed", "type"]),
("LIST_ITEM", &["value"]),
(
"LINK",
&[
"download",
"href",
"lang",
"media",
"ping",
"referrerpolicy",
"rel",
"shape",
"target",
],
),
(
"IMAGE",
&[
"align",
"border",
"crossorigin",
"decoding",
"height",
"importance",
"intrinsicsize",
"ismap",
"loading",
"referrerpolicy",
"sizes",
"srcset",
"usemap",
"width",
],
),
(
"TABLE",
&[
"align",
"bgcolor",
"border",
"cellpadding",
"cellspacing",
"frame",
"rules",
"summary",
"width",
],
),
(
"TABLE_HEADER",
&["align", "bgcolor", "char", "charoff", "valign"],
),
(
"TABLE_ROW",
&["align", "bgcolor", "char", "charoff", "valign"],
),
(
"TABLE_CELL",
&[
"abbr", "align", "axis", "bgcolor", "char", "charoff", "colspan", "headers",
"height", "rowspan", "scope", "valign", "width",
],
),
];
// DEFAULT_ATTRS
{
let mut attrs = default_attrs.clone();
attrs.sort_unstable(); // ASCII order
attrs.dedup();
file.write_all("/// List of HTML attributes common for all elements.\n".as_bytes())
.unwrap();
file.write_all("pub const DEFAULT_ATTRS: &str = \"".as_bytes())
.unwrap();
file.write_all(attrs.join(",").as_bytes()).unwrap();
file.write_all("\";\n\n".as_bytes()).unwrap();
}
// Per-element filters
for (name, specific) in ELEMENT_ATTRS {
// Merge default + specific, sort ASCII, dedup
let mut merged: Vec<&str> = Vec::with_capacity(default_attrs.len() + specific.len());
merged.extend(default_attrs.iter().copied());
merged.extend(specific.iter().copied());
merged.sort_unstable();
merged.dedup();
writeln!(
&mut file,
"/// List of HTML attributes for {} elements.",
name.to_ascii_lowercase()
)
.unwrap();
write!(
&mut file,
"pub const {}_ATTRS : &str = \"{}\";\n\n",
name,
merged.join(",")
)
.unwrap();
}
}
// }}} gen_html_attributes
// allowed_block_tags {{{
//
fn gen_allowed_block_tags() {
let path = Path::new(&env::var("OUT_DIR").unwrap()).join("allowed_block_tags.rs");
let mut file = BufWriter::new(File::create(&path).unwrap());
let mut m = phf_codegen::Map::new();
let block_tags = vec![
"address",
"article",
"aside",
"base",
"basefont",
"blockquote",
"body",
"caption",
"center",
"col",
"colgroup",
"dd",
"details",
"dialog",
"dir",
"div",
"dl",
"dt",
"fieldset",
"figcaption",
"figure",
"footer",
"form",
"frame",
"frameset",
"h1",
"h2",
"h3",
"h4",
"h5",
"h6",
"head",
"header",
"hr",
"html",
"iframe",
"legend",
"li",
"link",
"main",
"menu",
"menuitem",
"meta",
"nav",
"noframes",
"ol",
"optgroup",
"option",
"p",
"param",
"search",
"section",
"summary",
"table",
"tbody",
"td",
"tfoot",
"th",
"thead",
"title",
"tr",
"track",
"ul",
];
for tag in block_tags {
m.entry(tag, "true");
}
write!(
&mut file,
"static ALLOWED_BLOCK_TAGS: phf::Map<&'static str, bool> = {}",
m.build(),
)
.unwrap();
writeln!(&mut file, ";").unwrap();
}
// }}} gen_html_attributes
fn decode_rust_u(s: &str) -> char {
let hex = s
.strip_prefix(r"\u{")
.and_then(|t| t.strip_suffix('}'))
.ok_or_else(|| format!("bad s: {s}"))
.unwrap();
let cp = u32::from_str_radix(hex, 16)
.map_err(|_| format!("bad hex: {s}"))
.unwrap();
let ch = char::from_u32(cp)
.ok_or_else(|| format!("invalid Unicode scalar: {s}"))
.unwrap();
ch
}
fn main() {
gen_html_entities();
gen_unicode_case_foldings();
gen_html_attributes();
gen_allowed_block_tags();
}