summaryrefslogtreecommitdiff
path: root/process.py
blob: 6cef2a873dc308297c187b61677c0e3f85e706ac (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
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<n>\w+)\W(?P<b>.*?)\?\>"
    def run_pi_match(m: re.Match):
        name = m.group("n")
        body = m.group("b")
        print(f"({name})$ {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)