Change sorting on the blog tags page

This commit is contained in:
akp 2025-04-30 18:29:29 +01:00
parent 7aed2f1517
commit 2b69b61641
2 changed files with 16 additions and 8 deletions

View file

@ -281,7 +281,7 @@ def blog(base_dir: Path, output_dir: Path, jinja_env: jinja2.Environment, site_c
# generate listing
post_list = []
tags = []
tags = {}
for slug in posts:
post = posts[slug]
@ -306,15 +306,24 @@ def blog(base_dir: Path, output_dir: Path, jinja_env: jinja2.Environment, site_c
if "tags" in post:
for tag in post["tags"]:
if tag not in tags:
tags.append(tag)
tags[tag] = tags.get(tag, 0) + 1
try:
tag_descriptions = load_yaml(open(base_dir / "blogTags.yml").read())
except FileNotFoundError:
tag_descriptions = {}
tags = list(map(lambda x: AbbreviatedTag(x, tag_descriptions.get(x)), tags))
tag_list = [AbbreviatedTag(x, tag_descriptions.get(x), tags[x]) for x in tags]
# Here, we sort twice - once to get the tag list sorted alphabetically going in ascending order
# and a second time to get the tag list sorted by presence of description and article count in
# descending order.
tag_list = sorted(tag_list, key=lambda x: x.slug)
tag_list = sorted(
tag_list,
key=lambda x: (x.description is not None, x.articleCount),
reverse=True,
)
post_list = list(
sorted(
@ -353,15 +362,14 @@ def blog(base_dir: Path, output_dir: Path, jinja_env: jinja2.Environment, site_c
"canonicalURL": _make_canonical_url(site_config, "/blog/tags/"),
"showAside": True,
},
"tags": list(sorted(filter(lambda x: x.description is not None, tags)))
+ list(sorted(filter(lambda x: x.description is None, tags))),
"tags": tag_list,
}
)
f.write(r)
# generate tag-specific index pages
tpl = jinja_env.get_template("_layouts/blog/postsFilteredByTag.html")
for tag in tags:
for tag in tag_list:
d = tags_output_dir / tag.slug
os.makedirs(d, exist_ok=True)
with open(d / "index.html", "w") as f:

View file

@ -13,4 +13,4 @@ AbbreviatedPost = namedtuple(
],
)
AbbreviatedTag = namedtuple("AbbreviatedTag", ["slug", "description"])
AbbreviatedTag = namedtuple("AbbreviatedTag", ["slug", "description", "articleCount"])