summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--generator/generate.py80
1 files changed, 80 insertions, 0 deletions
diff --git a/generator/generate.py b/generator/generate.py
new file mode 100644
index 0000000..d94d085
--- /dev/null
+++ b/generator/generate.py
@@ -0,0 +1,80 @@
+#! /usr/bin/env python3
+
+import sys
+import os
+import subprocess
+
+def replace_references(lines, file_dir):
+ result = ""
+ for (i, line) in enumerate(lines):
+ if line.startswith("#include "):
+# include that file directly
+ # find the file as a path relative to the file we opened..
+ filepath = file_dir + line[len("#include "):].strip()
+ try:
+ f = open(filepath)
+ content = f.read()
+ result = result + content
+ except Exception as err:
+ print("Error reading file {}:{}".format(filepath, str(err)))
+ return None
+ elif line.startswith("#eval "):
+# include results of system'ing that line
+ try:
+ evald_text = subprocess.check_output(line[6:], shell=True).decode('utf-8')
+ result = result + '<pre>' + evald_text + '</pre>\n'
+ except Exception as e:
+ print("Error processing line {}:\n {}\nError: {}".format(i, line, e))
+ return None
+ else:
+# just include the line!
+ result = result + line
+ return result
+
+if __name__ == "__main__":
+ if len(sys.argv) != 2:
+ print("Usage: {} <page_to_compile>".format(sys.argv[0]))
+
+ md_source = sys.argv[1]
+ ridx = md_source.rfind('.')
+ if ridx >= len(md_source) - 5:
+ page_path = md_source[:ridx]
+ else:
+ page_path = md_source
+ page_path = page_path + ".html"
+ page_cwd = './'
+ if '/' in page_path:
+ page_cwd = page_path[:(md_source.rfind('/') + 1)]
+ page_path = page_path[(md_source.rfind('/') + 1):]
+
+ compiled_dest = './generated/{}{}'.format(page_cwd, page_path)
+ print("Source: {}\nPage path: {}\nDest file: {}".format(md_source, page_path, compiled_dest))
+ try:
+ md_file = open(md_source)
+ lines = md_file.readlines()
+
+ print("Compiling {}".format(md_source))
+
+ markdown_blob = replace_references(lines, page_cwd)
+ if markdown_blob == None:
+ print("Error compiling {}".format(md_source))
+ sys.exit(3)
+ print(markdown_blob)
+ # now shove it all through pandoc
+ pandoc_proc = subprocess.Popen(['pandoc'], stdout=subprocess.PIPE, stdin=subprocess.PIPE)
+ html = pandoc_proc.communicate(markdown_blob.encode('utf-8'))[0]
+ try:
+ if not os.path.isdir('./generated/' + page_cwd):
+ os.makedirs('./generated/' + page_cwd)
+ out_file = open(compiled_dest, 'w')
+ out_file.write(html.decode('utf-8'))
+ out_file.close()
+ except IOError as ioerr:
+ print("Unknown error writing result: " + str(ioerr))
+ sys.exit(2)
+ except IOError as ioerr:
+ if ioerr.errno == 2:
+ print("Source file '{}' does not exist".format(md_source))
+ else:
+ print("Unknown ioerr: " + str(ioerr))
+ sys.exit(1)