From f5ceaf774ea7d2d3cb05f736238045744ae1490f Mon Sep 17 00:00:00 2001 From: iximeow Date: Mon, 1 Jan 2018 01:23:01 -0800 Subject: add a thing to include or generate markdown at compile time this is how i'll string in sane highlighting for code, assembly, etc --- generator/generate.py | 80 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 generator/generate.py 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 + '
' + evald_text + '
\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: {} ".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) -- cgit v1.1