import re import sys import os def print_usage(): print(f"usage: python {sys.argv[0]} INPUT") sys.exit(1) argc = len(sys.argv) if argc < 2: print_usage() def error(message): print(f"error: {message}:", file=sys.stderr) sys.exit(1) path = sys.argv[1] if not os.path.isfile(path): error(f"{path} does not exist.") def read_metadata(path): pattern = re.compile(r"^\+{3}\n((?:.*|\n)*)\n\+{3}") metadata = {} with open(path) as f: match = re.search(pattern, f.read()) if not match: error(f"no metadata found at start of {path}") lines = match.group(1).split('\n') for l in lines: pair = [s.strip() for s in l.split('=')] metadata[pair[0]] = pair[1] return metadata def strip_metadata(path): pattern = re.compile(r"(\+{3}\n(?:.*|\n)*\n\+{3})") with open(path) as f: return re.sub(pattern, "", f.read()).strip() def generate_header(metadata): if metadata is None: error("no metadata, can't generate header") # TODO: is the description meta tag length-restricted? description = metadata["description"] keywords = metadata["keywords"] # if "description" or "keywords" don't exist we can't do anything: crash out if description is None or keywords is None: error("empty metadata parameters, can't generate header") header = """
""" header = header.replace("{{DESCRIPTION}}", description) header = header.replace("{{KEYWORDS}}", keywords) return header html = strip_metadata(path) metadata = read_metadata(path) header = generate_header(metadata) # Inject the generated element between the opening and elements. pattern = r"()(?:\n|.)*()" replacement = f"\\1\\n{header}\\n\\2" html = re.sub(pattern, replacement, html) print(html)