-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcompile.py
71 lines (57 loc) · 2.19 KB
/
compile.py
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# Requires commonmark (pip install CommonMark)
from os import listdir, mkdir
from os.path import isfile, isdir, join
import commonmark
import shutil
import re
from distutils.dir_util import copy_tree
from railroad_diagrams import parseDiagram
import io
railroadPattern = re.compile('```Railroad:(.*)```', re.MULTILINE)
with open("template.html", "r") as templateFile:
template = templateFile.read()
def diagramProcessor(match):
diagram = parseDiagram(match.group(1))
output = io.StringIO()
diagram.writeSvg(output.write)
return output.getvalue()
def processRailroadPatterns(content):
current = 0
fromIndex = content.find('```Railroad')
fileOutput = io.StringIO()
while fromIndex >= 0:
toIndex = content.find('```', fromIndex + 11)
diagramContent = content[fromIndex + 11:toIndex]
diagram = parseDiagram(diagramContent)
output = io.StringIO()
diagram.writeSvg(output.write)
fileOutput.write(content[current:fromIndex])
fileOutput.write(output.getvalue())
current = toIndex + 3
fromIndex = content.find('```Railroad', current)
fileOutput.write(content[current:])
return fileOutput.getvalue()
def compileFile(inputFilename, outputFilename, up):
with open(inputFilename, "r") as myfile:
contentLines = myfile.readlines()
content = "".join(contentLines)
#content = railroadPattern.sub(diagramProcessor, content)
content = processRailroadPatterns(content)
title = contentLines[0][1:].strip()
html = commonmark.commonmark(content)
result = template.replace("{content}", html).replace("{up}", up).replace("{title}", title)
with open(outputFilename, "w") as myfile:
myfile.write(result)
def compileDir(inputDir, outputDir, up):
if not isdir(outputDir):
mkdir(outputDir)
for f in listdir(inputDir):
input = join(inputDir, f)
output = join(outputDir, f)
if isfile(input) and input[-3:] == ".md":
compileFile(input, output.replace(".md", ".html"), up)
elif isdir(input):
compileDir(input, output, "../" + up)
shutil.rmtree("html", True)
copy_tree("template", "html")
compileDir("src", "html", "")