From f456510d32bfb4014d75241e42b7d16c599697de Mon Sep 17 00:00:00 2001 From: Starfall Date: Thu, 28 Oct 2021 22:33:37 -0500 Subject: strike: implement basic key=value front matter --- README | 5 ++--- sample/blog/with-frontmatter.html | 6 ++++++ sample/html/with-frontmatter.html | 17 +++++++++++++++++ strike.py | 18 ++++++++++++++++-- 4 files changed, 41 insertions(+), 5 deletions(-) create mode 100644 sample/blog/with-frontmatter.html create mode 100644 sample/html/with-frontmatter.html diff --git a/README b/README index 4a8b50a..441fefd 100644 --- a/README +++ b/README @@ -50,8 +50,7 @@ Right now, templates are just HTML in a Python f-string. You can use these varia Front matter ------------ -Current plan: -* Front matter is indicated by starting a file with a line that contains exactly three dashes ( --- ) and nothing else. -* Front matter begins on the line after that and will be in Python configparser format. +* Front matter is indicated by starting a file with a line that contains exactly three dashes and nothing else ("---\n"). * Front matter is closed by another line that containts exactly three dashes and nothing else. +* Every line between the opening and closing line should be key=value pairs. * Front matter is stored into the 'meta' dict, so you can overwrite meta[title] or meta[date] if you want to. diff --git a/sample/blog/with-frontmatter.html b/sample/blog/with-frontmatter.html new file mode 100644 index 0000000..5195292 --- /dev/null +++ b/sample/blog/with-frontmatter.html @@ -0,0 +1,6 @@ +--- +title=Custom Title +--- +Things before this line should not appear in the output. +--- +Including the delimiter in body content shouldn't be an issue. diff --git a/sample/html/with-frontmatter.html b/sample/html/with-frontmatter.html new file mode 100644 index 0000000..681c23d --- /dev/null +++ b/sample/html/with-frontmatter.html @@ -0,0 +1,17 @@ + + +Custom Title + + + + + +
+ Things before this line should not appear in the output. +--- +Including the delimiter in body content shouldn't be an issue. + +
+ diff --git a/strike.py b/strike.py index b417bf1..0679bd1 100755 --- a/strike.py +++ b/strike.py @@ -25,13 +25,27 @@ def read_config(location): return basedir, parser def handle_file(location): - metadata = dict( + meta_dict = dict( title = '.'.join(path.basename(location).split('.')[:-1]), date = strftime('%a, %d %b %Y %H:%M:%S GMT', gmtime(path.getmtime(location))) ) with open(location, 'r') as fin: file_contents = fin.read() - return file_contents, metadata + contents = handle_frontmatter(file_contents, meta_dict) + return contents, meta_dict + +def handle_frontmatter(file_contents, meta_dict): + delim = '---\n' + if not file_contents.startswith(delim): + return file_contents + parts = file_contents.split(delim, maxsplit=2) + meta_lines = parts[1].splitlines() + meta = dict( + (x.strip(), y.strip()) + for x, y in (kvp.split('=') for kvp in meta_lines) + ) + meta_dict.update(meta) + return parts[2] def apply_template(content, metadata, template="{content}"): return template.format( -- cgit