summaryrefslogtreecommitdiffstats
path: root/convert_gmi_html.py
blob: abfbe4afd971b5e686d650db85668b6c0232d111 (plain)
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
#!/usr/bin/env python3
"""Convert HTML blocks in .gmi files to gemtext using html2gmi."""

import html
import subprocess
import sys
from pathlib import Path

START_MARKER = "<!-- START_GEMINI_CONTENT -->"
END_MARKER = "<!-- END_GEMINI_CONTENT -->"
HTML2GMI = Path.home() / "go/bin/html2gmi"


def convert_file(path: Path) -> None:
    content = path.read_text()
    result = []
    pos = 0

    while True:
        start = content.find(START_MARKER, pos)
        if start == -1:
            result.append(content[pos:])
            break

        end = content.find(END_MARKER, start)
        if end == -1:
            result.append(content[pos:])
            break

        # Add content before the marker
        result.append(content[pos:start])

        # Convert HTML block to gemtext
        html_content = content[start + len(START_MARKER):end]
        converted = subprocess.run(
            [HTML2GMI],
            input=html_content,
            capture_output=True,
            text=True,
        )
        result.append(converted.stdout)

        pos = end + len(END_MARKER)

    # Unescape HTML entities in the entire file
    path.write_text(html.unescape("".join(result)))


def main() -> None:
    if len(sys.argv) < 2:
        print(f"Usage: {sys.argv[0]} <directory>", file=sys.stderr)
        sys.exit(1)

    directory = Path(sys.argv[1])
    for path in directory.rglob("*.gmi"):
        convert_file(path)


if __name__ == "__main__":
    main()