From e0382bfc81c1e964f7ed013308a2de0f0354ce21 Mon Sep 17 00:00:00 2001 From: ktyl Date: Sat, 11 Mar 2023 13:29:39 +0000 Subject: [PATCH] make html --- build/index.py | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++ makefile | 25 ++++++++++++++++----- 2 files changed, 78 insertions(+), 6 deletions(-) create mode 100644 build/index.py diff --git a/build/index.py b/build/index.py new file mode 100644 index 0000000..febbc86 --- /dev/null +++ b/build/index.py @@ -0,0 +1,59 @@ +#!/usr/bin/env python3 + +import sys +import re + +# we expect the arguments to be filepaths to each blog post + +def print_usage(): + print("\nusage: python mkblogindex.py POSTS\n") + print("\n") + print("\t\tPOSTS\tfilepaths of blog posts") + +# check args for at least one file path +if len(sys.argv) < 2: + print_usage() + sys.exit(1) + +# posts are arguments from index 1 onwards +posts = sys.argv[1:] + +dir_pattern = re.compile("(.+)/(blog\/.+\.html)") +path_pattern = re.compile("(.+)\/(\d{4})\/(\d{1,2})\/(\d{1,2})\/(.+).html") +title_pattern = re.compile("

(.+)

") + +# filter posts to just those with a date in them +posts = [p for p in posts if path_pattern.match(p)] +posts.reverse() + +links = [] + +# for each file we want to output an tag with a relative href to the site root +for path in posts: + m = re.match(path_pattern, path) + year = m.group(2) + month = m.group(3).rjust(2, '0') + day = m.group(4).rjust(2, '0') + + date = f'' + + title = "" + with open(path) as f: + for line in f: + if title_pattern.match(line): + title = re.sub(title_pattern, r'\1', line).strip() + break + + # clean leading directories to get the relative path we'll use for the link + url = re.sub(dir_pattern, r"\2", path) + + item = (date, f'
  • {date}{title}
  • ') + links.append(item) + +# make sure we're properly ordered in reverse date order lol +links = sorted(links, key=lambda x: x[0]) +links.reverse() + +for l in links: + print(l[1]) + diff --git a/makefile b/makefile index 9abe944..306d8f4 100644 --- a/makefile +++ b/makefile @@ -3,26 +3,39 @@ OUT_DIR = out/ HTML_DIR = $(OUT_DIR)html GEMINI_DIR = $(OUT_DIR)gemini -MAKE_GEMINI = build/markdown2gemini.py -MAKE_HTML = build/markdown2html.py -MAKE_RSS = build/rss.py +MAKE_GEMINI = build/markdown2gemini.py +MAKE_HTML = build/markdown2html.py +MAKE_RSS = build/rss.py +MAKE_HTML_INDEX = build/index.py PAGES = $(shell find $(SRC_DIR) -wholename "$(BLOG_SRC_DIR)*.md") HTML_TARGETS = $(PAGES:$(SRC_DIR)/%.md=$(HTML_DIR)/%.html) HTML_RSS = $(HTML_DIR)/index.xml +HTML_INDEX = $(HTML_DIR)/index.html + GEMINI_TARGETS = $(PAGES:$(SRC_DIR)/%.md=$(GEMINI_DIR)/%.gmi) _dummy := $(shell mkdir -p $(HTML_DIR) $(GEMINI_DIR)) $(HTML_DIR)/%.html: $(SRC_DIR)/%.md - python $(MAKE_HTML) $< $@ + pipenv run python $(MAKE_HTML) $< $@ $(HTML_RSS): $(PAGES) pipenv run python $(MAKE_RSS) $(PAGES) > $@ -html: $(HTML_TARGETS) $(HTML_RSS) - echo $(HTML_TARGETS) +$(HTML_INDEX): $(HTML_TARGETS) + pipenv run python $(MAKE_HTML_INDEX) $(HTML_TARGETS) > $@ + +$(HTML_DIR)/%.png: $(SRC_DIR)/%.png + mkdir -p $(shell dirname $@) + cp $< $@ + +$(HTML_DIR)/%.jpg: $(SRC_DIR)/%.jpg + mkdir -p $(shell dirname $@) + cp $< $@ + +html: $(HTML_TARGETS) $(HTML_RSS) $(HTML_INDEX) $(IMAGE_TARGETS) gemini: