Commit 96cd2873 authored by Marien Fressinaud's avatar Marien Fressinaud

Change how article attributes are accessed

parent 9be19d59
......@@ -30,16 +30,16 @@ class ProgramError(Exception):
class Article:
def __init__(self, slug, article_content):
self.slug = slug
def __init__(self, slug, article_content, configuration={}):
# We convert the file's content into HTML
md = markdown.Markdown(extensions=["meta", "fenced_code", "attr_list"])
self.content = md.convert(article_content)
content = md.convert(article_content)
# We get the local variables from the Markdown file (metadata)
# which will be accessible in the article template
self.meta = {}
self.meta.update(configuration)
self.meta["ARTICLE_SLUG"] = slug
for key, values in md.Meta.items():
article_key = f"ARTICLE_{key.upper()}"
# The Markdown's meta extension extract all the values in arrays, even
......@@ -47,15 +47,23 @@ class Article:
# the template system, for the moment we just get the first value of
# the array.
self.meta[article_key] = values[0]
self.meta["ARTICLE_CONTENT"] = content
# And we initialize a template for the article
article_template_filepath = os.path.join("theme", "article.html")
self.template = boopsy.Template(article_template_filepath)
def render(self):
variables = self.meta.copy()
variables["ARTICLE_CONTENT"] = self.content
return self.template.render(variables)
return self.template.render(self.meta)
def slug(self):
return self.meta["ARTICLE_SLUG"]
def title(self):
return self.meta.get("ARTICLE_TITLE", "An article")
def content(self):
return self.meta["ARTICLE_CONTENT"]
def dir_tree(path):
......@@ -116,7 +124,7 @@ def main():
# And write them in the ./site folder
for article in articles:
output_filepath = os.path.join(output_path, f"{article.slug}.html")
output_filepath = os.path.join(output_path, f"{article.slug()}.html")
with open(output_filepath, "w") as output_file:
output_file.write(article.render())
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment