forked from axboe/liburing
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfix.py
More file actions
executable file
·102 lines (83 loc) · 2.98 KB
/
Copy pathfix.py
File metadata and controls
executable file
·102 lines (83 loc) · 2.98 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/bin/python3
import re
import sys
from pathlib import Path
import subprocess
import os.path
_CODE = re.compile(
r"(?P<esc>\\.)" # backslash escape: \[ \] \` \* ...
r"|(?P<fence>^```.*?^```\s*$)" # fenced code block
r"|(?P<inline>`[^`\n]*`)", # inline code span
re.DOTALL | re.MULTILINE,
)
def fixup_links_skipping_code(md: str, fixup) -> str:
"""Run `fixup` (your existing [text] -> [text](url) function) on prose only."""
out = []
last = 0
for m in _CODE.finditer(md):
# prose before this code region: safe to transform
out.append(fixup(md[last:m.start()]))
# code region: pass through untouched
out.append(m.group(0))
last = m.end()
out.append(fixup(md[last:])) # trailing prose
return "".join(out)
def fix_man_link(match: re.Match) -> str:
fn_name = match.group(1)
man_link_map = {}
path = man_link_map.get(fn_name, f"man2/{fn_name}.2.html")
url = f"https://man7.org/linux/man-pages/{path}"
return f"[{fn_name}]({url})"
def fix_man_links(text: str) -> str:
pattern = re.compile(r'\[(?![^\]]*\]\()(?!io_uring)([^\]]+)\]')
found = pattern.findall(text)
# print(found)
return pattern.sub(fix_man_link, text)
def process(text: str) -> str:
# 1. Remove the "# NAME" line entirely
text = re.sub(r'^# NAME\s*\n', '', text, flags=re.MULTILINE)
# 2. Extract the short description line:
# e.g. "io_uring_for_each_cqe - iterate pending completion events"
m = re.search(r'\A(.*?)\s*-\s*(.*)$', text, flags=re.MULTILINE)
if m:
short = m.group(2).strip()
if not short.startswith('io_uring'):
short = short[0].upper() + short[1:]
text = text[:m.start()] + short + text[m.end():]
# 3. Remove everything from "# SYNOPSIS" up to "# DESCRIPTION"
text = re.sub(
r'# SYNOPSIS[\s\S]*?# DESCRIPTION',
'# DESCRIPTION',
text,
flags=re.MULTILINE
)
# 4. Transform SEE ALSO entries:
# **symbol**(3) → [symbol]
def fix_see_also(match):
symbol = match.group(1)
if symbol == "io_uring_clone_buffers":
return f"[{symbol}()]"
return f'[{symbol}]'
text = re.sub(
r'\*\*([A-Za-z0-9_]+)\*\*\((\d+)\)',
fix_see_also,
text
)
text = fixup_links_skipping_code(text, fix_man_links)
# text = fix_man_links(text)
text = re.sub(r'\*\* ', r'**\\', text)
return text
if __name__ == "__main__":
for path in sys.argv[1:]:
p = Path(path)
name = p.name.rsplit('.')[0]
if name == 'IO_URING_CHECK_VERSION':
continue
out_name = f"liburing-rs/docs/{name}.md"
subprocess.run(['pandoc', '-f', 'man', '-t', 'commonmark', '--lua-filter=tag-c.lua', '-o', out_name, path], stdout=True)
p = Path(out_name)
original = p.read_text()
cleaned = process(original)
# print(cleaned)
p.write_text(cleaned)
print(f"Processed {p}")