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

Only accept Markdown files under articles folder

parent 513c781e
......@@ -82,6 +82,9 @@ $ echo site/my-article.html
<html><body><h1>My first article</h1></body></html>
```
Please note that `./articles` folder only accepts Markdown files (i.e.
extension is `.md`).
You can define meta variables in the Markdown file which will be accessible
then in the template. Variables are uppercased and prepended by `ARTICLE_`. For
example, for the following Markdown file:
......@@ -150,8 +153,6 @@ $ tree
└── index.html
```
Other files under `./articles` are also copied to the `./site` folder.
## Tests
There are some tests (i.e. doctests) that can be run to check that everything
......
......@@ -41,13 +41,6 @@ def dir_tree(path):
yield filename
def mkdirs_for_file(filepath):
"""Create parent directories for a given file.
"""
path = os.path.dirname(filepath)
os.makedirs(path, exist_ok=True)
def convert_meta_to_article_vars(meta):
"""Convert Markdown Meta to vars for article layout.
"""
......@@ -95,36 +88,31 @@ def main():
md = markdown.Markdown(extensions=["meta", "fenced_code", "attr_list"])
# And copy the files from ./articles to ./site
for filepath in dir_tree(articles_path):
base, ext = os.path.splitext(filepath)
content_filepath = os.path.join(articles_path, filepath)
output_filepath = os.path.join(output_path, filepath)
if ext == ".md":
output_filepath = os.path.join(output_path, f"{base}.html")
# First we need to create all the parent directories for our file
mkdirs_for_file(output_filepath)
if ext == ".md":
# Then, if it is a Markdown file, we convert it
with open(content_filepath, "r") as content_file:
html = md.convert(content_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
# And we write the content in the output file
with open(output_filepath, "w") as output_file:
output_file.write(template.render(article_vars))
else:
# ... else, we simply copy the file to the output directory
shutil.copyfile(content_filepath, output_filepath)
for filename in dir_tree(articles_path):
base, ext = os.path.splitext(filename)
if ext != ".md":
# Don't consider files which are not Markdown
next
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
# And we write the content in the output file
with open(output_filepath, "w") as output_file:
output_file.write(template.render(article_vars))
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