Commit 9b4f622f authored by Marien Fressinaud's avatar Marien Fressinaud

Abstract articles in their own class

parent c37fe01d
......@@ -29,6 +29,33 @@ class ProgramError(Exception):
pass
class Article:
def __init__(self, article_content):
# We convert the file's content into HTML
md = markdown.Markdown(extensions=["meta", "fenced_code", "attr_list"])
self.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 = {}
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
# if there is only one value. Because there is no iteration system in
# the template system, for the moment we just get the first value of
# the array.
self.meta[article_key] = values[0]
# 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)
def dir_tree(path):
"""List all the tree directory under the given path.
"""
......@@ -41,20 +68,6 @@ def dir_tree(path):
yield filename
def convert_meta_to_article_vars(meta):
"""Convert Markdown Meta to vars for article layout.
"""
article_vars = {}
for key, values in meta.items():
article_key = f"ARTICLE_{key.upper()}"
# The Markdown's meta extension extract all the values in arrays, even
# if there is only one value. Because there is no iteration system in
# the template system, for the moment we just get the first value of
# the array.
article_vars[article_key] = values[0]
return article_vars
def main():
# Make sure ./site folder is empty
output_path = os.path.join(os.curdir, "site")
......@@ -85,8 +98,6 @@ def main():
# If not, just stop here
return
md = markdown.Markdown(extensions=["meta", "fenced_code", "attr_list"])
# And copy the files from ./articles to ./site
for filename in dir_tree(articles_path):
base, ext = os.path.splitext(filename)
......@@ -97,22 +108,13 @@ def main():
article_filepath = os.path.join(articles_path, filename)
output_filepath = os.path.join(output_path, f"{base}.html")
# We convert the file's content into HTML
# We read the article file and initialize an Article from it
with open(article_filepath, "r") as article_file:
html = md.convert(article_file.read())
# We initialize the template
article_template_filepath = os.path.join("theme", "article.html")
template = boopsy.Template(article_template_filepath)
# We get the local variables from the Markdown file (metadata)
# which will be accessible in the article template
article_vars = convert_meta_to_article_vars(md.Meta)
article_vars["ARTICLE_CONTENT"] = html
article = Article(article_file.read())
# And we write the content in the output file
with open(output_filepath, "w") as output_file:
output_file.write(template.render(article_vars))
output_file.write(article.render())
if __name__ == "__main__":
......
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