forked from DFHack/dfhack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
authors-rst.py
executable file
·72 lines (64 loc) · 3.1 KB
/
authors-rst.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
#!/usr/bin/env python3
""" Overly-complicated script to check formatting/sorting in Authors.rst """
import os, re, sys
def main():
success = [True]
def error(line, msg, **kwargs):
info = ''
for k in kwargs:
info += ' %s %s:' % (k, kwargs[k])
print('line %i:%s %s' % (line, info, msg))
if os.environ.get('GITHUB_ACTIONS'):
print('::error file=docs/about/Authors.rst,line=%i::%s %s' % (line, info.lstrip(), msg))
success[0] = False
with open('docs/about/Authors.rst', 'rb') as f:
lines = list(map(lambda line: line.decode('utf8').replace('\n', ''), f.readlines()))
if lines[1].startswith('='):
if len(lines[0]) != len(lines[1]):
error(2, 'Length of header does not match underline')
if lines[1].replace('=', ''):
error(2, 'Invalid header')
first_div_index = list(filter(lambda pair: pair[1].startswith('==='), enumerate(lines[2:])))[0][0] + 2
first_div = lines[first_div_index]
div_indices = []
for i, line in enumerate(lines[first_div_index:]):
line_number = i + first_div_index + 1
if '\t' in line:
error(line_number, 'contains tabs')
if line.startswith('==='):
div_indices.append(i + first_div_index)
if not re.match(r'^=+( =+)+$', line):
error(line_number, 'bad table divider')
if line != lines[first_div_index]:
error(line_number, 'malformed table divider')
if line == lines[first_div_index + i - 1]:
error(line_number, 'duplicate of previous line')
if len(div_indices) < 3:
error(len(lines), 'missing table divider(s)')
for i in div_indices[3:]:
error(i + 1, 'extra table divider')
col_ranges = []
i = 0
while True:
j = first_div.find(' ', i)
col_ranges.append(slice(i, j if j > 0 else None))
if j == -1:
break
i = j + 1
for i, line in enumerate(lines[div_indices[1] + 1:div_indices[2]]):
line_number = i + div_indices[1] + 2
for c, col in enumerate(col_ranges):
cell = line[col]
if cell.startswith(' '):
error(line_number, 'text does not start in correct location', column=c+1)
# check for text extending into next column if this isn't the last column
if col.stop is not None and col.stop < len(line) and line[col.stop] != ' ':
error(line_number, 'text extends into next column', column=c+1)
if i > 0:
prev_line = lines[div_indices[1] + i]
if line.lower()[col_ranges[0]] < prev_line.lower()[col_ranges[0]]:
error(line_number, 'not sorted: should come before line %i ("%s" before "%s")' %
(line_number - 1, line[col_ranges[0]].rstrip(' '), prev_line[col_ranges[0]].rstrip(' ')))
return success[0]
if __name__ == '__main__':
sys.exit(int(not main()))