Update page.py with new ipo, use date from filename

This commit is contained in:
Midgard 2020-06-16 01:05:15 +02:00
parent 5b014b97da
commit bc6c3aa16e
Signed by untrusted user who does not match committer: midgard
GPG key ID: 511C112F1331BBB4
4 changed files with 53 additions and 85 deletions

View file

@ -1,6 +1,5 @@
title: SVG + makefile = slide deck title: SVG + makefile = slide deck
summary: "You don't need LibreOffice to prepare your presentation -- or: an introduction to makefiles" summary: "You don't need LibreOffice to prepare your presentation -- or: an introduction to makefiles"
published: 2020-06-11
--- ---

View file

@ -1,6 +1,5 @@
title: Get started using feeds title: Get started using feeds
summary: Don't subscribe to our email newsletter summary: Don't subscribe to our email newsletter
published: 2020-06-02
--- ---

View file

@ -1,6 +1,5 @@
title: Get started with IRC title: Get started with IRC
summary: Pick a client, join some channels, maybe register with NickServ summary: Pick a client, join some channels, maybe register with NickServ
published: 2020-06-02
--- ---

127
page.py
View file

@ -3,34 +3,10 @@
import sys import sys
import re import re
from markdown import markdown from markdown import markdown
import strictyaml from strictyaml import load as load_yaml
from ipo import read, write, map, dictmap, starstarmap, join, sort, ipo from pymaybe import maybe as maybe
from functools import partial as p
from ipo import read, write, dictmap, starstarmap, ipo, all_before, all_after
md_to_html = join("\n") | ipo(markdown)
yaml_to_dict = join("\n") | ipo(strictyaml.load) | ipo(lambda x: x.data)
@ipo
def parted(data, regex):
"""
Two parts of data, delimited by the first element on which regex fullmatches.
If a delimiter is found, the second part is an iterator over the remaining elements. Else it is None.
"""
first = []
it = iter(data)
try:
while True:
item = next(it)
if re.fullmatch(regex, item):
break
first.append(item)
except StopIteration:
return (first, None)
return (first, it)
LIST_ITEM_TEMPLATE = """ LIST_ITEM_TEMPLATE = """
@ -45,48 +21,32 @@ def blog_page_template():
return fh.read() return fh.read()
# FIXME what a mess def date_from_filename(filename):
def blog_page(file, read_body=True): return maybe(
""" re.search(r"(?:^|/)([0-9]{4}-[0-9]{2}-[0-9]{2})[^/]+$", filename)
Keep the file open if you want to be able to read the body. ).group(1).or_none()
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: def blog_page_metadata(filename):
metadata_yaml, body_md = read(file) | parted("---+") with open(filename) as file:
metadata_from_file = (
read(file=file) | p(all_before, p(re.fullmatch, "---+")) | "\n".join |
load_yaml | (lambda x: x.data)
).data
metadata = { return {
**(metadata_yaml | yaml_to_dict), **metadata_from_file,
"path": re.sub(r".md$", "", filename),
"published": date_from_filename(filename)
} }
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) def blog_page_body(filename):
with open(filename) as file:
finally: return (
if filename: read(file=file) | p(all_after, p(re.fullmatch, "---+")) | "\n".join |
file.close() p(markdown, extensions=["abbr", "toc", "smarty", "fenced_code", "codehilite"])
).data
def safe_metadata(metadata): def safe_metadata(metadata):
@ -100,21 +60,32 @@ def safe_metadata(metadata):
} }
if sys.argv[1] == "--index": def main():
if sys.argv[1] == "--index":
blog_post_list = ( blog_post_list = (
sys.argv[4:] | ipo(sys.argv[4:]) |
map(lambda filename: { p(map, blog_page_metadata) |
**blog_page(filename, read_body=False)[0], p(sorted, key=lambda x: x["published"]) |
}) | p(map, safe_metadata) |
sort(key=lambda x: x["published"]) | p(map, lambda metadata: LIST_ITEM_TEMPLATE.format(**metadata)) |
map(safe_metadata) | "".join
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: 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) print(
else: file_in.read().format(blog_posts=blog_post_list),
with open(sys.argv[1]) as file_in, open(sys.argv[2], "w") as file_out: end="", file=file_out
metadata, body = blog_page(file_in) )
blog_page_template().format(**safe_metadata(metadata), body=body) | write(file=file_out)
else:
metadata = safe_metadata(blog_page_metadata(sys.argv[1]))
body = blog_page_body(sys.argv[1])
with open(sys.argv[2], "w") as file_out:
print(
blog_page_template().format(**metadata, body=body),
end="", file=file_out
)
if __name__ == '__main__':
main()