Commit 96cd2873 authored by berumuron's avatar berumuron
Browse files

Change how article attributes are accessed

parent 9be19d59
...@@ -30,16 +30,16 @@ class ProgramError(Exception): ...@@ -30,16 +30,16 @@ class ProgramError(Exception):
class Article: class Article:
def __init__(self, slug, article_content): def __init__(self, slug, article_content, configuration={}):
self.slug = slug
# We convert the file's content into HTML # We convert the file's content into HTML
md = markdown.Markdown(extensions=["meta", "fenced_code", "attr_list"]) 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) # We get the local variables from the Markdown file (metadata)
# which will be accessible in the article template # which will be accessible in the article template
self.meta = {} self.meta = {}
self.meta.update(configuration)
self.meta["ARTICLE_SLUG"] = slug
for key, values in md.Meta.items(): for key, values in md.Meta.items():
article_key = f"ARTICLE_{key.upper()}" article_key = f"ARTICLE_{key.upper()}"
# The Markdown's meta extension extract all the values in arrays, even # The Markdown's meta extension extract all the values in arrays, even
...@@ -47,15 +47,23 @@ class Article: ...@@ -47,15 +47,23 @@ class Article:
# the template system, for the moment we just get the first value of # the template system, for the moment we just get the first value of
# the array. # the array.
self.meta[article_key] = values[0] self.meta[article_key] = values[0]
self.meta["ARTICLE_CONTENT"] = content
# And we initialize a template for the article # And we initialize a template for the article
article_template_filepath = os.path.join("theme", "article.html") article_template_filepath = os.path.join("theme", "article.html")
self.template = boopsy.Template(article_template_filepath) self.template = boopsy.Template(article_template_filepath)
def render(self): def render(self):
variables = self.meta.copy() return self.template.render(self.meta)
variables["ARTICLE_CONTENT"] = self.content
return self.template.render(variables) 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): def dir_tree(path):
...@@ -116,7 +124,7 @@ def main(): ...@@ -116,7 +124,7 @@ def main():
# And write them in the ./site folder # And write them in the ./site folder
for article in articles: 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: with open(output_filepath, "w") as output_file:
output_file.write(article.render()) 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