From 0d06090fcb79748e69c3af135a987801a7b42250 Mon Sep 17 00:00:00 2001
From: Midgard <2885-Midgard@users.noreply.framagit.org>
Date: Mon, 8 Jun 2020 02:55:50 +0200
Subject: [PATCH] Unify do_index & do_blog, add syntax highlighting
---
blog.html | 1 +
do_blog.py | 104 ++++++++++++++++++++++++++++++++++++++++++------
do_index.py | 28 -------------
makefile | 11 +++--
public/main.css | 15 +++++++
5 files changed, 115 insertions(+), 44 deletions(-)
delete mode 100755 do_index.py
diff --git a/blog.html b/blog.html
index 0d0011c..5d0e25c 100644
--- a/blog.html
+++ b/blog.html
@@ -5,6 +5,7 @@
{title} – midgard
+
diff --git a/do_blog.py b/do_blog.py
index 020e228..888b4d3 100755
--- a/do_blog.py
+++ b/do_blog.py
@@ -1,11 +1,16 @@
#!/usr/bin/env python3
import sys
-from strictyaml import load as load_yaml
-from ipo import read, write, map, starstarmap, join, sort, ipo
-from glob import glob
-from markdown import markdown
import re
+from glob import glob
+from ipo import read, write, map, dictmap, starstarmap, join, sort, ipo
+from functools import partial
+from markdown import markdown
+import strictyaml
+
+
+md_to_html = join("\n") | ipo(markdown)
+yaml_to_dict = join("\n") | ipo(strictyaml.load) | ipo(lambda x: x.data)
@ipo
@@ -30,13 +35,88 @@ def parted(data, regex):
return (first, it)
-with open("blog.html") as blog_fh:
- TEMPLATE = blog_fh.read()
+LIST_ITEM_TEMPLATE = """
+
+ {title}
+ {summary}
+ """
-with open(sys.argv[1]) as file_in:
- metadata_yaml, content = read(file_in) | parted("---+")
- metadata = load_yaml(metadata_yaml | join("\n")).data
- body = markdown(content | join("\n"), extensions=["abbr", "toc"])
-with open(sys.argv[2], "w") as file_out:
- TEMPLATE.format(**metadata, body=body) | write(file=file_out)
+def blog_page_template():
+ with open("blog.html") as fh:
+ return fh.read()
+
+
+# FIXME what a mess
+def blog_page(file, read_body=True):
+ """
+ Keep the file open if you want to be able to read the body.
+ Good:
+ >>> with open(…) as file:
+ >>> metadata, body = blog_page(file)
+ >>> [smthng(line) for line in body]
+ >>> print("\n".join(hello))
+
+ Bad:
+ >>> with open(…) as file:
+ >>> metadata, body = blog_page(file)
+ >>> hello = (smthng(line) for line in body) # Lazy generator, the body wasn't read from file yet
+ >>> print("\n".join(hello))
+ """
+ if isinstance(file, str):
+ assert not read_body, "Can't read body when giving a filename, need a file for that."
+ filename = file
+ file = open(filename)
+ else:
+ filename = None
+
+ try:
+ metadata_yaml, body_md = read(file) | parted("---+")
+
+ metadata = {
+ **(metadata_yaml | yaml_to_dict),
+ }
+ if not read_body:
+ metadata["path"] = re.sub(r".md$", "", filename)
+
+ body = (
+ body_md | md_to_html(extensions=["abbr", "toc", "smarty", "fenced_code", "codehilite"])
+ if read_body else None
+ )
+
+ return (metadata, body)
+
+ finally:
+ if filename:
+ file.close()
+
+
+def safe_metadata(metadata):
+ return {
+ k: (
+ v
+ if k in ("published", "path")
+ else markdown(v, extensions=["smarty"]).replace("", "").replace("
", "")
+ )
+ for k, v in metadata.items()
+ }
+
+
+if sys.argv[1] == "--index":
+ blog_post_list = (
+ glob("blog/*.md") |
+ map(lambda filename: {
+ **blog_page(filename, read_body=False)[0],
+ }) |
+ sort(key=lambda x: x["published"]) |
+ map(safe_metadata) |
+ map(lambda metadata: LIST_ITEM_TEMPLATE.format(**metadata)) |
+ join("")
+ )
+
+ with open(sys.argv[2]) as file_in, open(sys.argv[3], "w") as file_out:
+ file_in.read().format(blog_posts=blog_post_list) | write(file=file_out)
+else:
+ with open(sys.argv[1]) as file_in, open(sys.argv[2], "w") as file_out:
+ metadata, body = blog_page(file_in)
+ blog_page_template().format(**safe_metadata(metadata), body=body) | write(file=file_out)
diff --git a/do_index.py b/do_index.py
deleted file mode 100755
index 8497727..0000000
--- a/do_index.py
+++ /dev/null
@@ -1,28 +0,0 @@
-#!/usr/bin/env python3
-
-import sys
-from glob import glob
-import re
-from strictyaml import load as load_yaml
-from ipo import list, str, read, write, map, starstarmap, takewhile, join, sort
-
-
-TEMPLATE = """
-
- {title}
- {summary}
- """
-
-
-def metadata(filename):
- with open(filename) as fh:
- metadata_yaml = read(fh) | takewhile(lambda line: line != "---") | join("\n")
- return {
- **load_yaml(metadata_yaml).data,
- "path": re.sub(r".md$", "", filename)
- }
-
-blog_post_list = glob("blog/*.md") | map(metadata) | sort(key=lambda x: x["published"]) | starstarmap(TEMPLATE.format) | join("")
-
-with open(sys.argv[1]) as file_in, open(sys.argv[2], "w") as file_out:
- file_in.read().format(blog_posts=blog_post_list) | write(file=file_out)
diff --git a/makefile b/makefile
index 30d173b..cbba03f 100644
--- a/makefile
+++ b/makefile
@@ -7,10 +7,10 @@ PYTHON ::= venv/bin/python
.PHONY: clean public
-all: $(BUILD_DIR)/index.html public $(BLOG_HTML)
+all: $(BUILD_DIR)/index.html $(BUILD_DIR)/syntax.css public $(BLOG_HTML)
-$(BUILD_DIR)/index.html: index.html $(BLOG) do_index.py $(BUILD_DIR)
- $(PYTHON) do_index.py "$<" "$@"
+$(BUILD_DIR)/index.html: index.html $(BLOG) do_blog.py $(BUILD_DIR)
+ $(PYTHON) do_blog.py --index "$<" "$@"
$(BUILD_DIR)/blog/%.html: blog/%.md blog.html do_blog.py $(BUILD_DIR)/blog
$(PYTHON) do_blog.py "$<" "$@"
@@ -21,8 +21,11 @@ $(BUILD_DIR):
$(BUILD_DIR)/blog:
mkdir -p "$@"
+$(BUILD_DIR)/syntax.css: makefile
+ pygmentize -S rainbow_dash -f html > "$@"
+
public:
cp -rt "$(BUILD_DIR)" public/.
clean:
- rm -rf "$(OUTPUT)" "$(BUILD_DIR)"
+ rm -rf "$(BUILD_DIR)"
diff --git a/public/main.css b/public/main.css
index 63eb234..c84c2fd 100644
--- a/public/main.css
+++ b/public/main.css
@@ -207,9 +207,13 @@ header img, .sitename {
+body.blog {
+ background-color: #efe5e0;
+}
.blog main {
line-height: 1.5;
max-width: 50em;
+ background: #fff5f0;
}
@media (min-width:900px) or ((min-aspect-ratio:1/1) and (min-width:300px)) {
.blog main {
@@ -219,6 +223,17 @@ header img, .sitename {
+.codehilite {
+ background-color: #111;
+ color: #eee;
+ padding: 0.5em;
+}
+.codehilite pre {
+ margin: 0;
+}
+
+
+
#social {
font-size: 75%;
}