Skip to content

Commit 18345d6

Browse files
Create convert-manual-images-to-markdown.py
1 parent 6cc0c7b commit 18345d6

1 file changed

Lines changed: 37 additions & 0 deletions

File tree

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/usr/bin/env python3
2+
"""Convert <img> tags to Markdown images for IDE/GitHub preview compatibility."""
3+
import re
4+
from pathlib import Path
5+
6+
DOCS = Path(__file__).resolve().parents[1] / "docs"
7+
8+
IMG = re.compile(
9+
r'<img src="\./screenshots/([^"]+)" alt="([^"]*)"(?:\s+width="\d+")?\s*/>\s*\n*',
10+
re.MULTILINE,
11+
)
12+
13+
14+
def convert(path: Path) -> int:
15+
text = path.read_text(encoding="utf-8")
16+
n = 0
17+
18+
def repl(m: re.Match) -> str:
19+
nonlocal n
20+
n += 1
21+
file, alt = m.group(1), m.group(2).strip() or m.group(1)
22+
return f"![{alt}](screenshots/{file})\n\n"
23+
24+
text = IMG.sub(repl, text)
25+
path.write_text(text, encoding="utf-8", newline="\n")
26+
return n
27+
28+
29+
def main() -> None:
30+
for name in ("USER_MANUAL.md", "INSTALL.md"):
31+
p = DOCS / name
32+
if p.is_file():
33+
print(f"{name}: converted {convert(p)} images")
34+
35+
36+
if __name__ == "__main__":
37+
main()

0 commit comments

Comments
 (0)