feat: inject toml-specified headers (#36)
This commit is contained in:
parent
c65854df9f
commit
0c069241cd
|
@ -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 = """<head>
|
||||
<meta charset="UTF-8"/>
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0 minimum-scale=1" />
|
||||
<meta name="description" content="{{DESCRIPTION}}" />
|
||||
<meta name="keywords" content="{{KEYWORDS}}"/>
|
||||
<link rel="canonical" href="https://ktyl.dev/">
|
||||
<link rel="stylesheet" href="styles.css" />
|
||||
<link rel="icon" type="image/x-icon" href="favicon.ico" />
|
||||
|
||||
<script src="https://kit.fontawesome.com/ad50bb2e16.js" crossorigin="anonymous"></script>
|
||||
</head>"""
|
||||
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 <head> element between the opening <html> and <body> elements.
|
||||
pattern = r"(<html>)(?:\n|.)*(<body>)"
|
||||
replacement = f"\\1\\n{header}\\n\\2"
|
||||
html = re.sub(pattern, replacement, html)
|
||||
|
||||
print(html)
|
9
makefile
9
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
|
||||
|
|
|
@ -1,10 +1,12 @@
|
|||
+++
|
||||
description = computer whisperer
|
||||
keywords = Cat Flynn, Ktyl, About, Computer Whisperer
|
||||
+++
|
||||
|
||||
<!doctype html>
|
||||
<html>
|
||||
|
||||
#include header.html
|
||||
|
||||
<body>
|
||||
<title>ktyl ~ about</title>
|
||||
<title>ktyl ~ computer whisperer</title>
|
||||
|
||||
<div class="nav">
|
||||
#include titlestart.html
|
||||
|
|
|
@ -1,8 +1,11 @@
|
|||
+++
|
||||
description = blog
|
||||
keywords = Cat Flynn, Ktyl, Blog
|
||||
+++
|
||||
|
||||
<!doctype html>
|
||||
<html>
|
||||
|
||||
#include header.html
|
||||
|
||||
<body>
|
||||
<title>ktyl ~ blog</title>
|
||||
|
||||
|
|
|
@ -1,8 +1,11 @@
|
|||
+++
|
||||
description = gallery
|
||||
keywords = Cat Flynn, Ktyl, Portfolio, Gallery, Ray Tracing, Relativity
|
||||
+++
|
||||
|
||||
<!doctype html>
|
||||
<html>
|
||||
|
||||
#include header.html
|
||||
|
||||
<body>
|
||||
<title>ktyl ~ gallery</title>
|
||||
|
||||
|
|
|
@ -1,8 +1,11 @@
|
|||
+++
|
||||
description = home
|
||||
keywords = Cat Flynn, Ktyl, Portfolio, Showreel, Blog, Gallery
|
||||
+++
|
||||
|
||||
<!doctype html>
|
||||
<html>
|
||||
|
||||
#include header.html
|
||||
|
||||
<body>
|
||||
<title>ktyl ~ home</title>
|
||||
|
||||
|
|
Loading…
Reference in New Issue