-
Notifications
You must be signed in to change notification settings - Fork 1
/
helpers.py
59 lines (43 loc) · 1.48 KB
/
helpers.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
import re
def escape_text(text):
"""Escape text so it is safe for use in AsciiDoc."""
return re.sub(
r"\(\((.+)\)\)",
r"\((\1))",
re.sub(r"\b(__\w+)", r"++\1++", str(text))
.replace("*", "++*++")
.replace(" \\\n", " ")
.replace("->", "\\->"),
)
def sanitize(identifier):
"""Escape a Doxygen ID so it is safe to use in AsciiDoc."""
return re.sub(r"__+", r"_", identifier)
def title(text, level, attributes=None):
"""Return text formatted as a title with the given level and attributes."""
if attributes is None:
attributes = {}
attrlist = []
if "id" in attributes:
attrlist.append(f"#{sanitize(attributes.pop('id'))}")
roles = []
if level > 5:
roles.append("h6")
if "role" in attributes:
roles.append(attributes.pop("role"))
if roles:
attrlist.append(f"role={' '.join(roles)}")
if "tag" in attributes:
attrlist.append(f"tag={escape_text(attributes.pop('tag'))}")
if "type" in attributes:
attrlist.append(f"type={escape_text(attributes.pop('type'))}")
for key, value in attributes.items():
attrlist.append(f'{escape_text(key)}="{escape_text(value)}"')
output = []
if attrlist:
output.append(f"[{','.join(attrlist)}]")
if level > 5:
output.append(f"*{escape_text(text)}*")
else:
marker = "=" * (level + 1)
output.append(f"{marker} {escape_text(text)}")
return "\n".join(output)