generated from martinthomson/internet-draft-template
-
Notifications
You must be signed in to change notification settings - Fork 3
/
fix-sub.py
executable file
·78 lines (66 loc) · 2.11 KB
/
fix-sub.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
#!/usr/bin/env python3
import fileinput
import re
import sys
chars = "-+=()0123456789imps"
subtr = "₋₊₌₍₎₀₁₂₃₄₅₆₇₈₉ᵢₘₚₛ"
suptr = "⁻⁺⁼⁽⁾⁰¹²³⁴⁵⁶⁷⁸⁹ⁱᵐᵖˢ"
blockcode = re.compile(r"^(~~~~*) *(\w+)$")
inlinecode = re.compile(r"(?:^|(?<=[^\\]))`")
sub = re.compile(r"(?:<sub>([" + chars + r"]+)</sub>|(?<=[\w\)])_([" + chars + r"]))")
sup = re.compile(r"(?:<sup>([" + chars + r"]+)</sup>|(?<=[\w\)])\^([" + chars + r"]))")
def warn(msg, **kwargs):
print(msg, file=sys.stderr, **kwargs)
def code_off(code, linenum):
if code:
warn(f"Warning: Unterminated inline code block detected on line {linenum}.")
return False
def tr_once(line, pattern, target):
result = ""
lastend = 0
for m in pattern.finditer(line):
result += line[lastend:m.start()]
for c in (m[1] or m[2]):
i = chars.find(c)
result += target[i]
lastend = m.end()
result += line[lastend:]
return result
def tr(line):
line = line.replace("\\*", "·").replace("\\+", "⊕").replace("...", "…")
line = tr_once(line, sub, subtr)
line = tr_once(line, sup, suptr)
return line.replace("\\_", "_").replace("\\^", "^")
def trcode(line, code):
result = ""
lastend = 0
for m in inlinecode.finditer(line):
span = line[lastend:m.start()]
if code:
span = tr(span)
code = not code
result += span + "`"
lastend = m.end()
result += tr(line[lastend:]) if code else line[lastend:]
return (result, code)
linenum = 0
blockend = None
code = False
pseudocode = False
for line in fileinput.input():
linenum += 1
if blockend is None:
m = blockcode.match(line)
if m:
code = code_off(code, linenum)
blockend = m[1]
pseudocode = m[2] == "pseudocode"
elif line.strip() == "":
code = code_off(code, linenum)
else:
(line, code) = trcode(line, code)
elif line.startswith(blockend):
blockend = None
elif pseudocode:
line = tr(line)
print(line, end="")