about summary refs log tree commit diff
diff options
context:
space:
mode:
authorStarfall <us@starfall.systems>2021-10-28 22:33:37 -0500
committerStarfall <us@starfall.systems>2023-12-05 10:00:41 -0600
commitf456510d32bfb4014d75241e42b7d16c599697de (patch)
treeddbf78f0cd548577d391033197028c505439212d
parent7806ca6c9c72657dcb6f316656a7e4bd4abff629 (diff)
strike: implement basic key=value front matter
-rw-r--r--README5
-rw-r--r--sample/blog/with-frontmatter.html6
-rw-r--r--sample/html/with-frontmatter.html17
-rwxr-xr-xstrike.py18
4 files changed, 41 insertions, 5 deletions
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 @@
+<!DOCTYPE html>
+<html lang=en-US dir=ltr>
+<title>Custom Title</title>
+<meta charset=UTF-8>
+<meta name=robots content="noindex, nofollow">
+<meta name=viewport content="width=device-width, initial-scale=1">
+<meta http-equiv=last-modified content="Fri, 29 Oct 2021 03:25:04 GMT">
+
+<section>
+	Things before this line should not appear in the output.
+---
+Including the delimiter in body content shouldn't be an issue.
+
+</section>
+<footer>
+	<p>This page was generated by <a href=https://git.starfall.systems/strike>Strike</a>.  
+</footer>
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(