import sys import re import os ##NOTE: The script uploaded to the website is what I am currently using, but will most likely change since then, and I don't plan on updating this. directory = os.fsencode(os.path.dirname(os.path.realpath(__file__))+"\\toCompile") code_pattern = "&&&CODE&&&" active_pattern = "&&active&&" def compile(source, target): source_name = source.split('/')[1] temp_target = "temp/" + source_name print("Opening file",source) source_file = open(source, "r") print("creating file",temp_target) temp_target_file = open(temp_target, "w+").close() print("Reading",source_name) lines = source_file.readlines() for line in lines: if line.find(code_pattern) == 0: print("found code:") line_content = line.split(code_pattern)[1].strip() print("",line_content) line_args = line_content.split("|") temp_target_file = open(temp_target, "a") temp_target_file.write(handle_args(line_args)) temp_target_file.close() else: temp_target_file = open(temp_target, "a") temp_target_file.write(line) temp_target_file.close() source_file.close() print("Initial pass complete") print("Starting second pass") target_file = open(target, "w+") temp_target_file = open(temp_target, "r") lines = temp_target_file.readlines() for line in lines: if active_pattern in line: if source_name in line: print("Setting active") line = line.replace(active_pattern, "active") else: print("Setting inactive") line = line.replace(active_pattern, "") target_file.write(line) temp_target_file.close() target_file.close() print("second pass complete") def handle_args(args): if args[0] == "file": print(" inserting file",args[1]) file = open(args[1],"r") data = file.read() file.close() return data if __name__=="__main__": print(sys.argv) if len(sys.argv) == 1: for file in os.listdir(directory): filename = os.fsdecode(file) print(filename) if filename.endswith(".html"): source_filename = "toCompile/" + filename target_filename = "compiled/" + filename compile(source_filename, target_filename) else: source_filename = "toCompile/" + sys.argv[1] target_filename = "compiled/" + sys.argv[1] compile(source_filename, target_filename)