Commit 303d1192 authored by Marien Fressinaud's avatar Marien Fressinaud

tec: Refactor articles gathering

parents c37fe01d 207381f1
......@@ -29,6 +29,35 @@ class ProgramError(Exception):
pass
class Article:
def __init__(self, slug, article_content):
self.slug = slug
# 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 +70,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,34 +100,25 @@ 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
# List files from ./articles
articles = []
for filename in dir_tree(articles_path):
base, ext = os.path.splitext(filename)
slug, ext = os.path.splitext(filename)
if ext != ".md":
# Don't consider files which are not Markdown
next
# We read the article file and initialize an Article from it
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
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(slug, article_file.read())
articles.append(article)
# And we write the content in the output file
# And write them in the ./site folder
for article in articles:
output_filepath = os.path.join(output_path, f"{article.slug}.html")
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