OILS / doctools / fmt_check.py View on Github | oilshell.org

117 lines, 72 significant
1#!/usr/bin/env python3
2"""fmt_check.py
3
4Check that the output HTML obeys the following rules:
5
6 - No orphaned backticks '`' should be part of a `inline code block`
7 (ie. any backticks not in a <code> block is treated as an error)
8 - Lines in a <code> should be shorter than 70 chars (else they overflow)
9"""
10
11import html.parser
12import sys
13
14from doctools.util import log
15
16
17class TagAwareHTMLParser(html.parser.HTMLParser):
18
19 def __init__(self, file):
20 super().__init__()
21 self.tag_stack = []
22 self.file = file
23
24 def location_str(self):
25 line, col = self.getpos()
26 return '%s:%d:%d' % (self.file, line, col)
27
28 def handle_starttag(self, tag, _attrs):
29 # Skip self-closing elements
30 if tag in ('meta', 'img'):
31 return
32
33 self.tag_stack.append(tag)
34
35 def handle_endtag(self, tag):
36 popped = self.tag_stack.pop()
37 if tag != popped:
38 print('%s [WARN] Mismatched tag!' % self.location_str(),
39 'Expected </%s> but got </%s>' % (popped, tag))
40
41
42class CheckBackticks(TagAwareHTMLParser):
43
44 def __init__(self, file):
45 super().__init__(file)
46 self.has_error = False
47
48 def handle_data(self, text):
49 # Ignore eg, <code> tags
50 if len(self.tag_stack) and (self.tag_stack[-1]
51 not in ("p", "h1", "h2", "h3", "a")):
52 return
53
54 idx = text.find('`')
55 if idx == -1:
56 return
57
58 print('%s [ERROR] Found stray backtick %r' %
59 (self.location_str(), text))
60
61 self.has_error = True
62
63
64class CheckCodeLines(TagAwareHTMLParser):
65 # Found when the display is 801px in width
66 MAX_LINE_LENGTH = 70
67
68 def __init__(self, file):
69 super().__init__(file)
70 self.has_error = False
71
72 def handle_data(self, text):
73 # Ignore eg, <code> tags
74 if len(self.tag_stack) and self.tag_stack[-1] != 'code':
75 return
76
77 for i, line in enumerate(text.splitlines()):
78 if len(line) > self.MAX_LINE_LENGTH:
79 print('%s [ERROR] Line %d of <code> is too long: %r' %
80 (self.location_str(), i + 1, line))
81 self.has_error = True
82
83
84def FormatCheck(filename):
85 backticks = CheckBackticks(filename)
86 with open(filename, "r") as f:
87 backticks.feed(f.read())
88
89 lines = CheckCodeLines(filename)
90 with open(filename, "r") as f:
91 lines.feed(f.read())
92
93 return backticks.has_error or lines.has_error
94
95
96def main(argv):
97 action = argv[1]
98
99 any_error = False
100 for path in argv[1:]:
101 if not path.endswith('.html'):
102 raise RuntimeError('Expected %r to be a .html file' % filename)
103
104 this_error = FormatCheck(path)
105 any_error = any_error or this_error
106 log("%s %s" % ("ER" if this_error else "OK", path))
107
108 if any_error:
109 raise RuntimeError("Formatting errors found")
110
111
112if __name__ == '__main__':
113 try:
114 main(sys.argv)
115 except RuntimeError as e:
116 print('FATAL: %s' % e, file=sys.stderr)
117 sys.exit(1)