From 555c2e767a4fdb9d9e2ea343ddae61979b72ac67 Mon Sep 17 00:00:00 2001 From: ktyl Date: Sun, 12 Mar 2023 23:19:55 +0000 Subject: [PATCH] make images --- build/page.py | 88 +++++++++++++++++++++++++++++++++++++++++++++++++++ makefile | 7 +++- 2 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 build/page.py diff --git a/build/page.py b/build/page.py new file mode 100644 index 0000000..2a6af6d --- /dev/null +++ b/build/page.py @@ -0,0 +1,88 @@ +#!/usr/bin/env python + +import os +import sys +import markdown +import re + +# SRC +# +-2022/ +# | +-10/ +# | +-12/ +# | +-25/ +# +-2023/ +# | +-1/ +# | +-26/ +# | +-3/ +# ... + +def print_usage(): + print("\nusage: python mkblog.py SRC DEST\n") + print("\n") + print("\t\tSRC\tinput markdown file") + print("\t\tDEST\tdestination html file") + +# check args +if len(sys.argv) != 3: + print_usage() + sys.exit(1) + +src_file = sys.argv[1] +dest_file = sys.argv[2] + +# check blog root exists +if not os.path.isfile(src_file): + print("{blog_root} doesn't exist") + sys.exit(1) + +# make dest dir if it doesnt exist + +dest_dir = os.path.dirname(dest_file) +print(dest_dir) +if not os.path.isdir(dest_dir): + os.makedirs(dest_dir) + +# write markdown into a dummy file first so that we can add lines before it in the final output +dummy_file = f"{dest_file}.bak" +open(dummy_file, 'w').close() + +print(f"{src_file} -> {dummy_file}") +markdown.markdownFromFile(input=src_file, output=dummy_file, extensions=["fenced_code"]) + +# TODO: a lot of this templating work is specific to the ktyl.dev blog - ideally, that stuff should +# be in *that* repo, not this one +print(f"{dummy_file} -> {dest_file}") +with open(dummy_file, 'r') as read_file, open(dest_file, 'w') as write_file: + write_file.write("#include blogstart.html\n") + + # modify the basic html to make it nicer for styling later + html = read_file.read() + + # extract images from their enclosing

tags and put them in img panels + html = re.sub('(

()

)', r'
\2
', html) + + # insert text-panel start between non-

and

elements + html = re.sub('((?)\n)(

)', r'\1

\n\2', html) + # insert para-block end between

and non-

elements + html = re.sub('(

\n)((?!

))', r'\1

\n\2', html) + + # insert code-panel start before
 elements
+    html = re.sub('(
)', r'
\n\1', html) + # insert code-panel end after
elements + html = re.sub('(
)', r'\1\n', html) + + # replace horizontal rules with nice separator dot + html = re.sub('
', r'
', html) + + lines = html.split("\n") + + # tack on a closing div because we will have opened one without closing it on the final

+ lines.append("") + + for line in lines: + write_file.write(line + "\n") + + write_file.write("\n#include blogend.html\n") + +os.remove(dummy_file) + diff --git a/makefile b/makefile index 306d8f4..0764d9c 100644 --- a/makefile +++ b/makefile @@ -4,7 +4,7 @@ HTML_DIR = $(OUT_DIR)html GEMINI_DIR = $(OUT_DIR)gemini MAKE_GEMINI = build/markdown2gemini.py -MAKE_HTML = build/markdown2html.py +MAKE_HTML = build/page.py MAKE_RSS = build/rss.py MAKE_HTML_INDEX = build/index.py @@ -16,6 +16,11 @@ HTML_INDEX = $(HTML_DIR)/index.html GEMINI_TARGETS = $(PAGES:$(SRC_DIR)/%.md=$(GEMINI_DIR)/%.gmi) +IMAGES = $(shell find $(SRC_DIR) -wholename "$(SRC_DIR)*.png" -o -wholename "$(SRC_DIR)*.jpg") +PNG_TARGETS = $(IMAGES:$(SRC_DIR)/%.png=$(HTML_DIR)/%.png) +JPG_TARGETS = $(IMAGES:$(SRC_DIR)/%.jpg=$(HTML_DIR)/%.jpg) +IMAGE_TARGETS = $(PNG_TARGETS) $(JPG_TARGETS) + _dummy := $(shell mkdir -p $(HTML_DIR) $(GEMINI_DIR)) $(HTML_DIR)/%.html: $(SRC_DIR)/%.md