aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoruser <user@node5.net>2024-01-07 13:58:10 +0100
committeruser <user@node5.net>2024-01-07 13:58:10 +0100
commit7c911e8f5b09dee35ea7137c9ca915f2f34f9ae6 (patch)
tree976bb2415249a2173d141782b825bebeeded5efe
parentb8c5550480acccfabbcc8211c3f1cc37dbc24e70 (diff)
parse article metadata
-rw-r--r--README.md14
-rw-r--r--blog_generator.py23
2 files changed, 33 insertions, 4 deletions
diff --git a/README.md b/README.md
index fc632da..3785100 100644
--- a/README.md
+++ b/README.md
@@ -1 +1,13 @@
-KISS static blog site generator
+# KISS static blog site generator
+## For blog.node5.net
+
+Made for my speceific use case, but otherwise generalized.
+Each directory is one article, it contains the markdown, and all the needed media for this article.
+The name of the article is derived from the folder name.
+Meta data is defined at the top of the article in YAML format enclosed in `---`
+Additional meta data may be added, and referenced in the templates.
+```yaml
+---
+Title: Article 1
+Catagory: Hardware
+---
diff --git a/blog_generator.py b/blog_generator.py
index 3c5a4de..981b2b9 100644
--- a/blog_generator.py
+++ b/blog_generator.py
@@ -34,13 +34,17 @@ class Article:
output_dir_path: str
output_path: str
source_file_name: str
-
+
+ # meta data, can't be required, due to Article.__dict__.keys()
title: str = None
description: str = None
- thumbnail_path: str = None
created: datetime.date = None
modified: datetime.date = None
+ thumbnail_path: str = None
+
+meta_data_keys = [key for key in Article.__dict__.keys() if not key.startswith('_')]
+
with open("config.yml", "r") as file:
config = yaml.safe_load(file)
logger.debug(f'Config loaded: {config.__repr__()}')
@@ -69,6 +73,19 @@ for article_name in articles_name:
with open(article_source_path, 'r') as file:
source = file.read()
+
+ # Check for article META data
+ if source.startswith('---'):
+ meta_data_yml_end_char_index = source.find('---', 3)
+ meta_data_yml = source[3:meta_data_yml_end_char_index]
+ # Strip meta data text from source, before feeding it to the markdown reader
+ source = source[meta_data_yml_end_char_index+3:]
+ meta_data = yaml.safe_load(meta_data_yml)
+ # Gets additional meta data may be added, and referenced in the templates.
+ known_meta_data = {key: value for key, value in meta_data.items() if key in meta_data_keys}
+ else:
+ logger.warn(f'No metadata found in: {article_name}')
+
try:
html = markdown.markdown(source)
except Exception as ex:
@@ -83,7 +100,7 @@ for article_name in articles_name:
article = Article(name=article_name, dir_path=article_dir_path, source_path=article_source_path, source=source,
html=html, output_dir_path=article_output_dir_path, output_path=article_output_path,
- source_file_name=article_source_file_name)
+ source_file_name=article_source_file_name, **known_meta_data)
articles.append(article)
with open(article.output_path, "a") as file: