Unify do_index & do_blog, add syntax highlighting
This commit is contained in:
parent
5787cbb7e5
commit
0d06090fcb
5 changed files with 115 additions and 44 deletions
|
@ -5,6 +5,7 @@
|
|||
<meta name="viewport" content="width=device-width"/>
|
||||
<title>{title} – midgard</title>
|
||||
<link rel="stylesheet" href="../main.css"/>
|
||||
<link rel="stylesheet" href="../syntax.css"/>
|
||||
</head>
|
||||
<body class="blog">
|
||||
<header>
|
||||
|
|
104
do_blog.py
104
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 = """
|
||||
<li><a href="{path}">
|
||||
<div class="title">{title}</div>
|
||||
<div class="summary">{summary}</div>
|
||||
</a></li>"""
|
||||
|
||||
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("<p>", "").replace("</p>", "")
|
||||
)
|
||||
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)
|
||||
|
|
28
do_index.py
28
do_index.py
|
@ -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 = """
|
||||
<li><a href="{path}">
|
||||
<div class="title">{title}</div>
|
||||
<div class="summary">{summary}</div>
|
||||
</a></li>"""
|
||||
|
||||
|
||||
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)
|
11
makefile
11
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)"
|
||||
|
|
|
@ -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%;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue