summaryrefslogtreecommitdiff
path: root/process.py
blob: e2b9051f4b75c7daff1a994a299cad3f6a2b2114 (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
import re
from sys import argv
from os import popen
from markdown import markdown
import subprocess

PROCESSORS = {
    "lit": lambda c: c,
    "ex": lambda c: popen(c).read(),
    "exmd": lambda c: markdown(popen(c).read())
}

def run_pi(input: str) -> str:
    RE_PI = r"(?sm)<\?(?P<name>\w+)\W(?P<body>.*?)\?>"
    def run_pi_match(m: re.Match):
        name = m.group("name")
        body = m.group("body")
        print(f"({name})$ {body}")
        output = PROCESSORS[name](body)
        return output
    return re.sub(RE_PI, run_pi_match, input)

def template(input: str) -> str:
    RE_TITLE = r"<h1>(?P<title>.*?)</h1>"
    title_match = re.match(RE_TITLE, input)
    title = f"{title_match.group(1)} - metamuffin's website" if title_match != None else "metamuffin's website"
    tpl = open("template.html", "r").read()
    return tpl.replace("CONTENT", input).replace("TITLE", title)

doc = open(argv[1], "r").read()
doc = markdown(doc)
doc = template(doc)
doc = run_pi(doc)
open(argv[2], "w+").write(doc)