From 0d9ddd06288761529bc89f97654e7fccdc7e0d11 Mon Sep 17 00:00:00 2001 From: Cody Scott Date: Tue, 23 Jan 2024 09:32:09 -0500 Subject: [PATCH] Pass a dict of tags to the all_tags template --- htmd/example_site/templates/all_tags.html | 8 +++---- htmd/site.py | 28 ++++------------------- 2 files changed, 9 insertions(+), 27 deletions(-) diff --git a/htmd/example_site/templates/all_tags.html b/htmd/example_site/templates/all_tags.html index 74f2499..6d3ae65 100644 --- a/htmd/example_site/templates/all_tags.html +++ b/htmd/example_site/templates/all_tags.html @@ -6,11 +6,11 @@

All Tags

- {% for tag in tags %} + {% for tag, count in tags.items() %}
- - {{ tag.tag }} + +

+ {{ tag }}

diff --git a/htmd/site.py b/htmd/site.py index 2634bfa..81a1a3a 100644 --- a/htmd/site.py +++ b/htmd/site.py @@ -3,7 +3,6 @@ from pathlib import Path import sys import tomllib -from typing import TypedDict from bs4 import BeautifulSoup from feedwerk.atom import AtomFeed @@ -227,32 +226,15 @@ def post(year: str, month: str, day:str, path: str) -> Response: return render_template('post.html', post=post) -class TagDict(TypedDict): - tag: str - count: int - - -def tag_in_list(list_of_tags: [TagDict], tag: str) -> bool: - return any(i['tag'] == tag for i in list_of_tags) - - -def increment_tag_count(list_of_tags: [TagDict], tag: str) -> [TagDict]: - for i in list_of_tags: - if i['tag'] == tag: - i['count'] += 1 - return list_of_tags - - @app.route('/tags/') def all_tags() -> Response: - tags = [] + tag_counts: {str: int} = {} for post in posts: for tag in post.meta.get('tags', []): - if tag_in_list(tags, tag) is False: - tags.append({'tag': tag, 'count': 1}) - else: - increment_tag_count(tags, tag) - return render_template('all_tags.html', active='tags', tags=tags) + if tag not in tag_counts: + tag_counts[tag] = 0 + tag_counts[tag] += 1 + return render_template('all_tags.html', active='tags', tags=tag_counts) @app.route('/tags//')