Skip to content

Commit

Permalink
Pass a dict of tags to the all_tags template
Browse files Browse the repository at this point in the history
  • Loading branch information
Siecje committed Jan 23, 2024
1 parent b3e3cf7 commit 0d9ddd0
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 27 deletions.
8 changes: 4 additions & 4 deletions htmd/example_site/templates/all_tags.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
<article>
<h1>All Tags</h1>

{% for tag in tags %}
{% for tag, count in tags.items() %}
<div class="post-preview">
<a href="{{ url_for('tag', tag=tag.tag) }}"
<h2 class="post-title" style="font-size: {{1+0.1*tag.count}}em">
{{ tag.tag }}
<a href="{{ url_for('tag', tag=tag) }}">
<h2 class="post-title" style="font-size: {{1+0.1*count}}em">
{{ tag }}
</h2>
</a>
</div>
Expand Down
28 changes: 5 additions & 23 deletions htmd/site.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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/<string:tag>/')
Expand Down

0 comments on commit 0d9ddd0

Please sign in to comment.