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

PROCESSORS = {
    "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")
        output = PROCESSORS[name](body)
        return output
    return re.sub(RE_PI, run_pi_match, input)

def template(input: str) -> str:
    tpl = open("template.html", "r").read()
    return tpl.replace("CONTENT", input).replace("TITLE", "TODO TITLE")

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