aboutsummaryrefslogtreecommitdiff
path: root/highlight_code.py
blob: f023c0a77a3c2a20cc46836e94b83027f8cd1e67 (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
#!/usr/bin/env python3

import os
import re
import subprocess

code_format = "```{0}\n{1}\n```"
code_re = re.compile("<c (.*)>(.*)<\/c>")

def main():
    while True:
        try:
            s = input()
        except EOFError as e:
            break
        m = re.search(code_re, s)
        if m:
            pandoc = subprocess.Popen(["pandoc","-f", "markdown", "-"],
                universal_newlines=True, stdin=subprocess.PIPE,
                stdout=subprocess.PIPE)
            code, err = pandoc.communicate(input=code_format.format(m.group(1), m.group(2)))
            pandoc.kill()
            s = re.sub(code_re, code, s)
            s = s.replace('\n', '')
        print(s)

    
if __name__ == "__main__":
    main()