From f25bfb1ef6d52110f4eb9a8e489fe2f080c8920e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20Mart=C3=ADnez=20Portela?= Date: Sun, 17 Sep 2023 23:30:24 +0200 Subject: [PATCH] Add category pages. --- scripts/blog.py | 38 ++++++++++++++++++++++++ static/article.tmpl.html | 2 +- static/blog_index.tmpl.html | 2 +- static/category_list.tmpl.html | 54 ++++++++++++++++++++++++++++++++++ static/style.css | 10 +++++++ 5 files changed, 104 insertions(+), 2 deletions(-) create mode 100644 static/category_list.tmpl.html diff --git a/scripts/blog.py b/scripts/blog.py index df2b009..3a5a11b 100644 --- a/scripts/blog.py +++ b/scripts/blog.py @@ -46,6 +46,7 @@ ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) STATIC_PATH = os.path.join(ROOT_DIR, 'static') ARTICLE_TEMPLATE_NAME = 'article.tmpl.html' BLOG_INDEX_TEMPLATE_NAME = 'blog_index.tmpl.html' +CATEGORY_LIST_TEMPLATE_NAME = 'category_list.tmpl.html' BLOG_INDEX_PAGE_SIZE = 10 STATIC_RESOURCES = ( @@ -64,6 +65,8 @@ def update_statics(): ARTICLE_TEMPLATE = JINJA_ENV.get_template(ARTICLE_TEMPLATE_NAME) global BLOG_INDEX_TEMPLATE BLOG_INDEX_TEMPLATE = JINJA_ENV.get_template(BLOG_INDEX_TEMPLATE_NAME) + global CATEGORY_LIST_TEMPLATE + CATEGORY_LIST_TEMPLATE = JINJA_ENV.get_template(CATEGORY_LIST_TEMPLATE_NAME) update_statics() @@ -319,6 +322,38 @@ def render_index(docs, dest_top): with open(os.path.join(dest_top, fname), 'wt') as f: f.write(result) +def render_categories(docs, dest_top): + categories = {} + for (doc, front_matter, out_path) in docs.values(): + for tag in split_tags(front_matter['tags']): + if tag not in categories: + categories[tag] = [] + categories[tag].append((doc, front_matter, out_path)) + + print("Found {} tags".format(len(categories), categories)) + for tag, docs in categories.items(): + docs = sorted(docs, key=lambda x: x[1]['date'], reverse=True) + + posts = [ + { + # "doc": doc, + "title": front_matter['title'], + "post_publication_date": front_matter['date'], + "post_tags": split_tags(front_matter['tags']), + # "summary": summarize(doc), + "link": out_path.rstrip('/') + '/', + } + for (doc, front_matter, out_path) in docs + ] + + result = CATEGORY_LIST_TEMPLATE.render( + posts=posts, + ) + path = os.path.join(dest_top, "tags", tag, "index.html") + os.makedirs(os.path.dirname(path), exist_ok=True) + with open(path, 'wt') as f: + f.write(result) + def regen_all(source_top, dest_top, docs=None): if docs is None: @@ -359,6 +394,9 @@ def regen_all(source_top, dest_top, docs=None): # Render index render_index(docs, dest_top) + # Render categories + render_categories(docs, dest_top) + return docs diff --git a/static/article.tmpl.html b/static/article.tmpl.html index 72e07a0..7b4ba16 100644 --- a/static/article.tmpl.html +++ b/static/article.tmpl.html @@ -35,7 +35,7 @@ diff --git a/static/blog_index.tmpl.html b/static/blog_index.tmpl.html index c34c0ec..cf97293 100644 --- a/static/blog_index.tmpl.html +++ b/static/blog_index.tmpl.html @@ -37,7 +37,7 @@ diff --git a/static/category_list.tmpl.html b/static/category_list.tmpl.html new file mode 100644 index 0000000..893b5a3 --- /dev/null +++ b/static/category_list.tmpl.html @@ -0,0 +1,54 @@ + + + + + Código para llevar + + + + + + + +
+ +
+ + diff --git a/static/style.css b/static/style.css index e79db77..fbb2613 100644 --- a/static/style.css +++ b/static/style.css @@ -486,6 +486,16 @@ article.post { content: '< '; } +/* Categories and archive */ +.post-list .post .post-metadata, +.post-list .post h4 { + display: inline; +} + +.post-list .post { + margin-top: 1rem; +} + /* Tables. */ table, th, td, tr { border: 1px solid black;