-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparse.py
319 lines (230 loc) · 8.97 KB
/
parse.py
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
from util import *
from fmt import *
from collections import namedtuple
from bs4 import BeautifulSoup
from pathlib import Path
from slugify import slugify
import re
specdir = Path("contrib")
output_json = Path("spec-json")
# Match a list of one-or-more keywords such as the string `"foo"; "bar";`
# Each keyword is alpha-numeric and may (rarely) contain a hyphen.
KEYWORDS_PATTERN = re.compile(r'"[a-zA-Z0-9/-]+"(; "[a-zA-Z0-9/-]+")*')
# Match a element exceptions such as the string "element (if ...)'
EXCEPTION_PATTERN = re.compile(r'([a-zA-Z0-9-]+) \(if [a-zA-Z0-9\' -]+\)')
with Path("COPYING.txt").open("r") as fp:
COPYING = fp.read().split("\n\n")
with (specdir / "timestamp").open("r") as fp:
COPYING.append("Based on current published specifications accessed " + fp.read().strip())
COPYING = [x.replace("\n", " ").strip() for x in COPYING]
# Global attributes common to all HTML elements
# source: https://html.spec.whatwg.org/multipage/dom.html#global-attributes
# plus class, id, slot, role (ARIA)
global_attributes = \
[
"accesskey",
"autocapitalize",
"class",
"contenteditable",
"dir",
"draggable",
"enterkeyhint",
"hidden",
"id",
"inputmode",
"is",
"itemid",
"itemprop",
"itemref",
"itemscope",
"itemtype",
"lang",
"nonce",
"role", # ARIA
"slot",
"spellcheck",
"style",
"tabindex",
"title",
"translate",
]
t_element = namedtuple("Element", ["name", "desc", "categories", "attributes", "children"])
t_category = namedtuple("Category", ["name", "elements", "elements_maybe", "exceptions"])
t_attribute = namedtuple("Attributes", ["name", "elements", "desc", "value_type", "value_keywords"])
t_event_handler = namedtuple("EventHandlers", ["name", "applies_to"])
def gen_elements(element):
if element == "autonomous custom elements":
pass
elif ", " in element:
# e.g. h1, h2, h3, h4, h5, h6
for e in element.split(", "):
yield from gen_elements(e)
elif ";" in element:
for e in element.split(";"):
yield from gen_elements(e.strip())
elif " " in element:
# e.g. MathML math, SVG svg, ElementSpec element
yield element.split(" ")[1]
else:
yield element
def gen_attributes(attributes):
for attribute in attributes.split(";"):
attr = attribute.strip("*").strip()
if attr == "type\nsrcset":
# Fix a bug in the spec formatting
# https://github.com/whatwg/html/pull/4543
yield "type"
yield "srcset"
elif attr == "globals":
yield from global_attributes
else:
yield attr
def gen_categories(categories):
for category in categories.split(";"):
category = category.strip().strip("*")
if category == "empty":
continue
yield category
def gen_keywords(keywords):
"""Given a `keywords` string such as `"foo"; "bar"`, yield each keyword.
Otherwise, yield nothing."""
if KEYWORDS_PATTERN.fullmatch(keywords):
yield from map(lambda x: x.strip().strip("\""), keywords.split(";"))
def parse_index_elements(soup):
rows = soup.find("h3", {"id": "elements-3"}).findNext("tbody").find_all("tr")
for row in rows:
cells = row.find_all(["th", "td"])
cells = [x.get_text() for x in cells]
assert len(cells) == 7
element, desc, categories, _, children, attributes, _ = cells
elements = gen_elements(element)
categories = set(gen_categories(categories))
attributes = set(gen_attributes(attributes))
children = set(gen_categories(children))
for i in sorted(elements):
yield t_element(i, desc.strip(), categories, attributes, children)
def parse_index_categories(soup):
rows = soup.find("h3", {"id": "element-content-categories"}).findNext("tbody").find_all("tr")
for row in rows:
cells = row.find_all(["th", "td"])
cells = [x.get_text() for x in cells]
assert len(cells) == 3
category, elements, exceptions = cells
exceptions = "; ".join(map(lambda x: x.strip(), exceptions.split(";")))
if category.strip().endswith("*"):
exceptions += "; The tabindex attribute can also make any element into interactive content."
category = category.strip().strip("*")
elements = set(gen_elements(elements))
if exceptions == "—":
exceptions = ""
elements_maybe = parse_element_exceptions_string(exceptions)
yield t_category(category, elements, elements_maybe, exceptions)
def parse_index_attributes(soup):
rows = soup.find("h3", {"id": "attributes-3"}).findNext("tbody").find_all("tr")
for row in rows:
cells = row.find_all(["th", "td"])
cells = [x.get_text() for x in cells]
assert len(cells) == 4
attribute, elements, desc, value = cells
value_desc = " ".join([x.strip().strip("*") for x in value.split("\n")])
value_desc = value_desc.strip()
if value.strip().endswith("*"):
value_desc += ". The actual rules are more complicated than indicated"
value_keywords = set(gen_keywords(value_desc))
if value_keywords:
value_desc = "Keywords"
elements = set(map(lambda x: x.strip(";\n "), gen_elements(elements)))
yield t_attribute(attribute.strip(), elements, desc.strip(), value_desc, value_keywords)
def parse_index_event_handlers(soup):
rows = soup.find("table", {"id": "ix-event-handlers"}).findNext("tbody").find_all("tr")
for row in rows:
cells = row.find_all(["th", "td"])
cells = [x.get_text() for x in cells]
assert len(cells) == 4
attribute, elements, _, _ = cells
yield t_event_handler(attribute.strip(), elements.strip())
def parse_input_type_keywords(soup):
rows = soup.find("table", {"id": "attr-input-type-keywords"}).findNext("tbody").find_all("tr")
for row in rows:
cells = row.find_all(["th", "td"])
cells = [x.get_text() for x in cells]
keyword, *_ = cells
yield keyword.strip()
def parse_aria_roles(soup):
concrete_roles = {
"widget",
"document_structure_roles",
"landmark_roles",
"live_region_roles",
"window_roles",
}
for role in concrete_roles:
rows = soup.find("section", {"id": role}).findNext("ul").find_all("li")
for row in rows:
keyword = row.find("code").get_text()
yield keyword.strip()
def parse_element_exceptions_string(xs):
# e.g. "element (if ...); ...' -> [element, ...]
if not xs: return
if ";" in xs:
xs = xs.split(";")
else:
xs = [xs]
for x in xs:
x = x.strip()
matches = EXCEPTION_PATTERN.fullmatch(x)
if matches:
yield matches.group(1)
def parse_element_types(soup):
rows = soup.find("h4", {"id": "elements-2"}).findNext("dl")
result = {}
for dt, dd in grouper(rows, 2):
elements = dd.find_all("code")
if not elements: continue
dfn = dt.find("dfn").get_text()
dfn = slugify(dfn)
if dfn not in result:
result[dfn] = []
for element in elements:
name = element.get_text()
result[dfn].append(name)
return result
def element_wrapper(element_name):
"""NOTE: Not injection safe"""
def f(content):
return "<%s>%s</%s>" % (element_name, content, element_name)
return f
with (specdir / "indices.html").open("r") as fp:
g_soup = BeautifulSoup(fp, "lxml")
g_elements = parse_index_elements(g_soup)
g_categories = parse_index_categories(g_soup)
g_attributes = list(parse_index_attributes(g_soup)) # excl. event handlers
g_event_handlers = list(parse_index_event_handlers(g_soup))
with (specdir / "input.html").open("r") as fp:
g_soup = BeautifulSoup(fp, "lxml")
g_attributes.append(t_attribute("type", set(["input"]),
"Type of form control", "An input type e.g. \"text\"", set(parse_input_type_keywords(g_soup))))
with (specdir / "aria.html").open("r") as fp:
g_soup = BeautifulSoup(fp, "lxml")
g_attributes.append(t_attribute("role", set(["HTML"]),
"ARIA semantic role", "A concrete ARIA role", set(parse_aria_roles(g_soup))))
with (specdir / "syntax.html").open("r") as fp:
g_soup = BeautifulSoup(fp, "lxml")
g_element_types = parse_element_types(g_soup)
META={
"copyright": COPYING
}
g_elements = dictify_namedtuples(g_elements, meta=META)
g_categories = dictify_namedtuples(g_categories, meta=META)
g_attributes = dictify_namedtuples(g_attributes, merge=False, meta=META)
g_event_handlers = dictify_namedtuples(g_event_handlers, meta=META)
outputs = [
("elements", g_elements),
("categories", g_categories),
("attributes", g_attributes),
("event-handlers", g_event_handlers),
("element-types", g_element_types),
]
for k, v in outputs:
with (output_json / (k + ".json")).open("wb") as fp:
fp.write("".join(pformat(v)).encode("utf-8"))