aboutsummaryrefslogtreecommitdiff
path: root/fix.py
blob: 319d172f79753716206f86c2f407ca17306c5f1b (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
#!/usr/bin/env python3
import os
import re
import shutil


def fix():
    shutil.rmtree("content", ignore_errors=True)
    os.makedirs("content")

    for directory, _, files in os.walk("md"):
        for file in files:
            if file.endswith("gmi"):
                new_dir = f"content/{directory[3:]}"
                with open(f"{directory}/{file}", "r") as old_file:
                    old_content = old_file.read()
                old_content_lines = old_content.splitlines()

                meta_line = old_content_lines[2]
                match = re.fullmatch(r'''title: "([^"]*)" date: "(....-..-..)" categories:''', meta_line)
                title = match.group(1)
                date = match.group(2)

                separator_line = old_content_lines[1:].index('-'*80) + 1

                content = f"# {title}\n"
                content += f"{date}\n"
                content += "\n".join(old_content_lines[separator_line + 1:])

                os.makedirs(new_dir, exist_ok=True)
                with open(f"{new_dir}/{file}", "w") as new_file:
                    new_file.write(content)
            else:
                # FIXME
                pass


if __name__ == "__main__":
    fix()