From 34ba74d2b0c755145e50dd39c43d2c422cba54ce Mon Sep 17 00:00:00 2001 From: Bastian Venthur Date: Sat, 6 Feb 2021 14:29:56 +0100 Subject: [PATCH] differentiate between pages and articles --- blag/blag.py | 34 +++++++++++++++++++++++++++++----- blag/templates/archive.html | 8 +++++--- blag/templates/article.html | 6 ++++++ blag/templates/base.html | 27 +++++++++++++++++++++++++++ blag/templates/page.html | 7 +++++++ 5 files changed, 74 insertions(+), 8 deletions(-) create mode 100644 blag/templates/base.html create mode 100644 blag/templates/page.html diff --git a/blag/blag.py b/blag/blag.py index 0e9686e..31d86fe 100644 --- a/blag/blag.py +++ b/blag/blag.py @@ -89,15 +89,35 @@ def build(args): path = os.path.relpath(f'{root}/{dirname}', start=args.input_dir) os.makedirs(f'{args.output_dir}/{path}', exist_ok=True) - convert_to_html( + process_markdown( convertibles, args.input_dir, args.output_dir, args.template_dir ) -def convert_to_html(convertibles, input_dir, output_dir, template_dir): +def process_markdown(convertibles, input_dir, output_dir, template_dir): + """Process markdown files. + This method processes the convertibles, converts them to html and + saves them to the respective destination paths. + + If a markdown file has a `date` metadata field it will be recognized + as article otherwise as page. + + Parameters + ---------- + convertibles : List[Tuple[str, str]] + relative paths to markdown- (src) html- (dest) files + input_dir : str + output_dir : str + template_dir : str + + Returns + ------- + articles, pages : List[Tuple[str, Dict]] + + """ env = Environment( loader=ChoiceLoader([ FileSystemLoader([template_dir]), @@ -108,7 +128,7 @@ def convert_to_html(convertibles, input_dir, output_dir, template_dir): md = markdown_factory() articles = [] - + pages = [] for src, dst in convertibles: logger.debug(f'Processing {src}') with open(f'{input_dir}/{src}', 'r') as fh: @@ -123,8 +143,10 @@ def convert_to_html(convertibles, input_dir, output_dir, template_dir): # everything else are just pages if meta and 'date' in meta: articles.append((dst, context)) - - template = env.get_template('article.html') + template = env.get_template('article.html') + else: + pages.append((dst, content)) + template = env.get_template('page.html') result = template.render(context) with open(f'{output_dir}/{dst}', 'w') as fh_dest: fh_dest.write(result) @@ -163,6 +185,8 @@ def convert_to_html(convertibles, input_dir, output_dir, template_dir): with open('build/index.html', 'w') as fh: fh.write(result) + return articles, pages + if __name__ == '__main__': main() diff --git a/blag/templates/archive.html b/blag/templates/archive.html index 06c177b..92adb21 100644 --- a/blag/templates/archive.html +++ b/blag/templates/archive.html @@ -1,13 +1,15 @@ -

Archive

+{% extends "base.html" %} +{% block title %}{{ title }}{% endblock %} + +{% block content %} {% for entry in archive %} {% if entry.title %}

{{entry.title}}

{% endif %} - {% if entry.date %}

Written on {{ entry.date.date() }}.

- {% endif %} {% endfor %} +{% endblock %} diff --git a/blag/templates/article.html b/blag/templates/article.html index cddd070..5507a42 100644 --- a/blag/templates/article.html +++ b/blag/templates/article.html @@ -1 +1,7 @@ +{% extends "base.html" %} + +{% block title %}{{ title }}{% endblock %} + +{% block content %} {{ content }} +{% endblock %} diff --git a/blag/templates/base.html b/blag/templates/base.html new file mode 100644 index 0000000..481c2be --- /dev/null +++ b/blag/templates/base.html @@ -0,0 +1,27 @@ + + + + + + + {% block title %}{% endblock %} + + + +
+

A Blog

+ +
+ +
+ {% block content %} + {% endblock %} +
+ + + + diff --git a/blag/templates/page.html b/blag/templates/page.html new file mode 100644 index 0000000..5507a42 --- /dev/null +++ b/blag/templates/page.html @@ -0,0 +1,7 @@ +{% extends "base.html" %} + +{% block title %}{{ title }}{% endblock %} + +{% block content %} +{{ content }} +{% endblock %}