aboutsummaryrefslogtreecommitdiff
path: root/kludge-camelcase-oopsies.py
blob: ab5658dc250af1032ec0c4ce600ed5140e603761 (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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/usr/bin/env python3
#
# Kludges specific to converting the Cryptech wiki

import os, re

brute_force = (
    ("{filename}DNSSEC/Requirements",   "{filename}DNSSEC-Requirements"),
    ("[OpenDNSSEC](OpenDNSSEC)",        "[OpenDNSSEC]({filename}OpenDNSSEC.md)"),
    ("[PKCS11Proxy](PKCS11Proxy)",      "[PKCS11Proxy]({filename}PKCS11Proxy.md)"),
    ('[Collection about side-channel attacks and detection, mitigation methods"]({filename}SideChannel".md)',
     '[Collection about side-channel attacks and detection, mitigation methods]({filename}SideChannel.md)'),
    ("[Joachim Strömbergson](Joachim Strömbergson)", "[Joachim Strömbergson]({filename}Joachim Strömbergson.md)"),
    ('EDAToolchainSurvey"', 'EDAToolchainSurvey'),
    ("[for Tor?](1024)", "\[ 1024 for Tor? \]"),
    ("[done, nothing using it yet](Mechanism)", "\[Mechanism done, nothing using it yet\]"),
    ("[but waiting on a BSD-friendly license](Done,)", "\[Done, but waiting on a BSD-friendly license\]"),
    ("[done](Mechanism)", "\[Mechanism done\]"),
    ("[Done]", "\[Done\]"),
    ("[done]", "\[done\]"),
)

camel_regexp = re.compile(r"\[(.+?)\]\((?:{filename})?\1.md\)")
foot_regexp = re.compile(r"\[\(([0-9]+)\)\]\(=#fn[0-9]+\)")

def camel_replacer(dn, m):
    if os.path.exists(os.path.join(dn, m.group(1) + ".md")):
        return m.group(0)
    else:
        return m.group(1)

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)
        dn = os.path.dirname(fn)
        with open(fn) as f:
            lines = f.readlines()
        changes = False
        for i, line in enumerate(lines):
            for old, new in brute_force:
                line = line.replace(old, new)
            line = foot_regexp.sub(r"\1.", line)
            line = camel_regexp.sub(lambda m: camel_replacer(dn, m), line)
            if line != lines[i]:
                changes = True
                lines[i] = line
        if changes:
            tn = fn + ".tmp"
            with open(tn, "w") as f:
                for line in lines:
                    f.write(line)
            os.rename(tn, fn)