-
Notifications
You must be signed in to change notification settings - Fork 9
/
gen.py
executable file
·86 lines (66 loc) · 2.7 KB
/
gen.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#! /usr/bin/env python3
# coding: utf-8
import markdown
import os
import shutil
import subprocess
import sys
def main():
if len(sys.argv) > 1:
for i in range(len(sys.argv)-1):
if sys.argv[i+1] == "--output":
output = sys.argv[i+2]
else: # no argument given
print("""Argument required:
./gen.py --output [Output Directory]
""")
exit()
prjdir = os.path.dirname(__file__)
if os.path.isdir(output):
shutil.rmtree(output)
with open("%s/layouts/header.tpl" % prjdir, "r") as header_file:
header = header_file.read()
with open("%s/layouts/footer.tpl" % prjdir, "r") as footer_file:
footer = footer_file.read()
gen_html("%s/contents" % prjdir, output, header, footer)
def gen_html(walk_dir, outpath_root, header, footer):
# Compile .scss files
sass_cmd = "bundle exec sass --style expanded --update %s:%s" % (walk_dir, outpath_root)
print(sass_cmd)
subprocess.call(sass_cmd, shell=True)
for (dirpath, dirnames, filenames) in os.walk(walk_dir):
outpath = os.path.join(outpath_root, os.path.relpath(dirpath, walk_dir))
if not os.path.isdir(outpath):
os.makedirs(outpath)
for filename in filenames:
if filename.endswith(".scss"):
pass # Don't copy .scss files
elif filename.endswith(".odg"):
outfile = os.path.join(outpath, filename.replace(".odg", ".png"))
srcfile = os.path.join(dirpath, filename)
result = subprocess.call("unoconv -f png -o %s %s" % (outpath, srcfile), shell=True)
if result is 0:
print("Copied %s to %s" % (os.path.abspath(srcfile), os.path.abspath(outfile)))
else: # result is 1
print("Failed to copy %s to %s" % (os.path.abspath(srcfile), os.path.abspath(outfile)))
elif filename.endswith(".md") or filename.endswith(".tpl"):
with open(os.path.join(dirpath, filename), "r") as content_file:
content = content_file.read()
if filename.endswith(".md"):
content = markdown.markdown(content, output_format="html5")
if filename == "index.tpl":
outfile = os.path.join(outpath, filename.replace(".tpl", ".html"))
elif filename == "index.md":
outfile = os.path.join(outpath, filename.replace(".md", ".html"))
else: # if filename != "index.tpl"
outfile = os.path.join(outpath, filename.replace(".tpl", "").replace(".md", ""), "index.html")
if not os.path.isdir(os.path.dirname(outfile)):
os.makedirs(os.path.dirname(outfile))
html = "%s%s%s" % (header, content, footer)
with open(os.path.join(outfile), "w+") as output_file:
output_file.write(html)
print("Copied %s to %s" % (os.path.abspath(content_file.name), os.path.abspath(outfile)))
else:
shutil.copy(os.path.join(dirpath, filename), os.path.join(outpath, filename))
if __name__ == "__main__":
main()