From 03c7dc4687cda9c5324471abe49f4e21dab1be32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20Krzy=C5=9Bk=C3=B3w?= Date: Mon, 28 Oct 2024 14:07:46 +0100 Subject: [PATCH] refactor: blog card post placeholder handling --- mkdocs_nype/plugins/nype_tweaks/plugin.py | 5 +- mkdocs_nype/templates/partials/post-card.html | 55 ++++++++++--------- pyproject.toml | 2 +- 3 files changed, 33 insertions(+), 29 deletions(-) diff --git a/mkdocs_nype/plugins/nype_tweaks/plugin.py b/mkdocs_nype/plugins/nype_tweaks/plugin.py index d872b4f..ee0eb76 100644 --- a/mkdocs_nype/plugins/nype_tweaks/plugin.py +++ b/mkdocs_nype/plugins/nype_tweaks/plugin.py @@ -30,7 +30,10 @@ 6. Create nype_config for page and sync with global tweak: This was previously done at render time in nype-base.html, but this is too late for blog cards meta placeholders. To use the global value for the placeholder -better set it in the event. +better set it in the event. Errata: Turned out this train of thought was wrong, +as the posts' on_page events run after the blog index with the cards, so the +tweak doesn't make things easier. Global placeholder loading was moved to the +post-card.html template. MIT License 2024 Kamil Krzyśków (HRY) for Nype (npe.cm) """ diff --git a/mkdocs_nype/templates/partials/post-card.html b/mkdocs_nype/templates/partials/post-card.html index b78e753..99bf6df 100644 --- a/mkdocs_nype/templates/partials/post-card.html +++ b/mkdocs_nype/templates/partials/post-card.html @@ -35,33 +35,42 @@ {#- nype_config of the Excerpt can contain overrides to some values -#} {% set post_nype_config = post.meta.nype_config or {} -%} -{# store category separately for easier access later #} +{# Store category separately for easier access later #} {% if blog_card_category_view -%} {% set blog_card_category = blog_card_category_view -%} {% elif post.meta.categories -%} {% set blog_card_category = post.meta.categories[0].strip() -%} {% endif -%} -{# resolve category icon for card #} -{% if post_nype_config.blog_card_icon -%} - {% set blog_card_icon = post_nype_config.blog_card_icon -%} -{% elif blog_card_icons -%} - {% set blog_card_icon = blog_card_icons.get(blog_card_category, blog_card_icons._default) -%} -{% endif -%} +{# Resolve category icon for card #} +{% set blog_card_icon = post_nype_config.blog_card_icon or blog_card_icons.get(blog_card_category, blog_card_icons._default) or "material/file-document" -%} + +{#- Resolve page title -#} +{% set blog_card_title = post_nype_config.blog_card_title or theme_nype_config.blog_card_title -%} -{% if not blog_card_icon -%} - {% set blog_card_icon = "material/file-document" -%} +{% if blog_card_title -%} + {% set blog_card_title = blog_card_title.format(post_title=post.post.title) -%} +{% else -%} + {% set blog_card_title = post | post_card_title -%} {% endif -%} -{# resolve page read more prompt #} -{% if post_nype_config.blog_card_continue -%} - {% set blog_card_continue = post_nype_config.blog_card_continue -%} -{% elif blog_card_continues %} - {% set blog_card_continue = blog_card_continues.get(blog_card_category, blog_card_continues._default) -%} +{#- Resolve page description -#} +{% set blog_card_description = post_nype_config.blog_card_description or theme_nype_config.blog_card_description -%} + +{% if blog_card_description -%} + {% set blog_card_description = '

' ~ blog_card_description.format(post_description=post.meta.description) ~ '

' -%} +{% else -%} + {% set blog_card_description = post | post_card_description -%} + +{# Resolve page read more prompt #} +{% set blog_card_continue = post_nype_config.blog_card_continue or blog_card_continues.get(blog_card_category, blog_card_continues._default)-%} + +{% if blog_card_continue -%} + {% set blog_card_continue = blog_card_continue.format(category=blog_card_category) -%} +{% else -%} + {% set blog_card_continue = lang.t("blog.continue") -%} {% endif -%} -{% if not blog_card_continue -%} - {% set blog_card_continue = lang.t("blog.continue") -%} {% endif -%} @@ -75,28 +84,20 @@ {% include ".icons/" ~ blog_card_icon ~ ".svg" -%} - {% if post_nype_config.blog_card_title -%} - {{ post_nype_config.blog_card_title.format(post_title=post.post.title) -}} - {% else -%} - {{ post | post_card_title -}} - {% endif -%} + {{- blog_card_title -}}


- {% if post_nype_config.blog_card_description -%} -

{{ post_nype_config.blog_card_description.format(post_description=post.meta.description) }}

- {% else %} - {{ post | post_card_description -}} - {% endif %} + {{ blog_card_description -}} {% if post.more and (blog_cards != "off" or not hide_read_more) %} {% endif %} diff --git a/pyproject.toml b/pyproject.toml index 3cb39f1..119442a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -7,7 +7,7 @@ build-backend = "hatchling.build" [project] name = "mkdocs-nype" -version = "0.18.2" +version = "0.19.0" description = "MkDocs theme for Nype MkDocs projects, extends the Material for MkDocs theme" authors = [ { name = "Kamil Krzyśków", email = "kk@npe.cm" }