-
Notifications
You must be signed in to change notification settings - Fork 1
/
sphinx-table-to-table-list.py
executable file
·55 lines (45 loc) · 1.19 KB
/
sphinx-table-to-table-list.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
#!/usr/bin/env python3
import sys
def split_by_columns(line, widths):
parts = []
while line:
w = widths[0]
# lst
if len(widths) == 1:
w = 1000
parts.append(line[:w].rstrip())
line = line[w + 1:]
widths = widths[1:]
return parts
lines = sys.stdin.read().strip().splitlines()
header = lines[0]
assert '= =' in header
widths = [len(x) for x in header.split(' ')]
# skip header
lines = lines[1:]
# skip footer
if '= =' in lines[-1]:
lines = lines[:-1]
output = []
for line in lines:
columns = split_by_columns(line, widths)
nonempty = len([col for col in columns if col])
if len(widths) == nonempty:
output.append(columns)
else:
for i, column in enumerate(columns):
if column:
output[-1][i] += ' ' + column
print('.. list-table::')
has_header = False
if output[1][0] == '=' * widths[0]:
print(' :header-rows: 1')
output = output[:1] + output[2:]
has_header = True
print()
for i, columns in enumerate(output):
for j, value in enumerate(columns):
c = '*' if j == 0 else ' '
print(f' {c} - {value}')
if i == 0 and has_header:
print()