diff --git a/generate_headers.py b/generate_headers.py new file mode 100644 index 0000000..4ceb378 --- /dev/null +++ b/generate_headers.py @@ -0,0 +1,87 @@ +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) diff --git a/makefile b/makefile index f7ddfb6..f995c56 100644 --- a/makefile +++ b/makefile @@ -1,6 +1,7 @@ SRC_DIR = src IMG_DIR = img OUT_DIR = site +TMP_DIR = tmp ROOT_DIR = $(SRC_DIR)/root @@ -31,7 +32,10 @@ html: $(HTML_TARGETS) $(CSS_TARGETS) | $(OUT_DIR) cp $(IMG_DIR)/*.png $(OUT_DIR)/ cp $(IMG_DIR)/*.jpg $(OUT_DIR)/ -$(OUT_DIR)/%.html: $(ROOT_DIR)/%.html $(HTML_INCLUDES) $(BLOG_INDEX) | $(OUT_DIR) +$(TMP_DIR)/%.html: $(ROOT_DIR)/%.html | $(TMP_DIR) + python generate_headers.py $< > $@ + +$(OUT_DIR)/%.html: $(TMP_DIR)/%.html $(HTML_INCLUDES) $(BLOG_INDEX) | $(OUT_DIR) python ppp.py $< $(HTML_INCLUDES) $(BLOG_INDEX) > $@ $(OUT_DIR)/%.css: $(ROOT_DIR)/%.css | $(OUT_DIR) @@ -40,6 +44,9 @@ $(OUT_DIR)/%.css: $(ROOT_DIR)/%.css | $(OUT_DIR) $(OUT_DIR): mkdir -p $@ +$(TMP_DIR): + mkdir -p $@ + $(BLOG_INDEX): mkdir -p $(BLOG_OUT_DIR) make --directory $(BLOG_BASE_DIR) html diff --git a/src/root/about.html b/src/root/about.html index c71086f..e46f746 100644 --- a/src/root/about.html +++ b/src/root/about.html @@ -1,10 +1,12 @@ ++++ +description = computer whisperer +keywords = Cat Flynn, Ktyl, About, Computer Whisperer ++++ + - -#include header.html - -