Repository navigation
Expand file tree
/
Copy pathcheck_jargon.py
More file actions
104 lines (86 loc) · 3.28 KB
/
Copy pathcheck_jargon.py
File metadata and controls
104 lines (86 loc) · 3.28 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
"""Report guide jargon: python3 scripts/check_jargon.py [--max N] [--json].
Without --max this only reports counts. LD and Policy are exempt from limits.
"""
import argparse
from html import unescape
from html.parser import HTMLParser
import json
from pathlib import Path
import re
JARGON = re.compile(r"\b(frameworks?|contentions?|warrants?|impact calculus)\b", re.I)
EXEMPT = {"lincoln-douglas-debate", "policy-debate"}
class PageText(HTMLParser):
def __init__(self):
super().__init__(convert_charrefs=False)
self.in_body = False
self.hidden = None
self.is_json_ld = False
self.visible = []
self.json_ld = []
self.script = []
def handle_starttag(self, tag, attrs):
if tag == "body":
self.in_body = True
if self.in_body and self.hidden is None:
self.visible.append(" ")
if tag in {"script", "style"}:
self.hidden = tag
self.is_json_ld = (
tag == "script"
and dict(attrs).get("type", "").lower() == "application/ld+json"
)
self.script = []
def handle_endtag(self, tag):
if tag == self.hidden:
if self.is_json_ld:
self.json_ld.append(json.dumps(
json.loads("".join(self.script)), ensure_ascii=False,
))
self.hidden = None
self.is_json_ld = False
if tag == "body":
self.in_body = False
if self.in_body and self.hidden is None:
self.visible.append(" ")
def handle_data(self, data):
if self.is_json_ld:
self.script.append(data)
elif self.in_body and self.hidden is None:
self.visible.append(data)
def handle_entityref(self, name):
self.handle_data(f"&{name};")
def handle_charref(self, name):
self.handle_data(f"&#{name};")
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument(
"--max", type=int, dest="maximum",
help="fail if either count exceeds N outside LD and Policy",
)
parser.add_argument("--json", action="store_true", help="print JSON instead of a table")
args = parser.parse_args()
if args.maximum is not None and args.maximum < 0:
parser.error("--max must be non-negative")
rows = []
for path in sorted((Path(__file__).resolve().parent.parent / "public").glob("*/index.html")):
page = PageText()
page.feed(path.read_text(encoding="utf-8"))
page.close()
rows.append({
"slug": path.parent.name,
"visible": len(JARGON.findall(unescape("".join(page.visible)))),
"json_ld": len(JARGON.findall(" ".join(page.json_ld))),
})
if args.json:
print(json.dumps(rows, ensure_ascii=False, indent=2))
else:
width = max([len("slug")] + [len(row["slug"]) for row in rows])
print(f"{'slug':<{width}} visible json-ld")
for row in rows:
print(f"{row['slug']:<{width}} {row['visible']:>7} {row['json_ld']:>7}")
return int(args.maximum is not None and any(
row["slug"] not in EXEMPT and max(row["visible"], row["json_ld"]) > args.maximum
for row in rows
))
if __name__ == "__main__":
raise SystemExit(main())