summaryrefslogtreecommitdiff
path: root/kludges.py
blob: c0de7e132f66a8e4625158a4ed848810e55af850 (plain) (blame)
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
#!/usr/bin/env python3
#
# Kludges specific to converting the Cryptech wiki

import os, re, json

with open("repositories.json") as f:
    repo_or = "|".join(os.path.splitext(r)[0] for r in json.load(f))

plain = "](https://git.cryptech.is"

regexp = re.compile(r"(\(https://git\.cryptech\.is/(?:" + repo_or + "))([(/)])")

for root, dirs, files in os.walk("pelican/content"):
    for fn in files:
        if not fn.endswith(".md"):
            continue
        fn = os.path.join(root, fn)
        with open(fn) as f:
            lines = f.readlines()
        if not any(plain in line for line in lines):
            continue
        changes = 0
        for i, line in enumerate(lines):
            if plain not in line:
                continue
            lines[i], n = regexp.subn(
                lambda m: m.group(1) + (".git/tree" if m.group(2) == "/" else ".git") + m.group(2),
                line)
            changes += n
        if changes:
            tn = fn + ".tmp"
            with open(tn, "w") as f:
                for line in lines:
                    f.write(line)
            os.rename(tn, fn)