From 9887fb0a6f9f0e94d9d7faca57c62fb3549bdb83 Mon Sep 17 00:00:00 2001 From: welpo Date: Fri, 10 Mar 2023 23:25:54 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat:=20allow=20dual=20date-format?= =?UTF-8?q?=20(short/long)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config.toml | 9 +++++-- templates/base.html | 3 ++- templates/cards.html | 2 +- templates/macros/cards_pages.html | 43 +++++++++++++++++++++++++++++++ templates/macros/cards_posts.html | 43 ------------------------------- templates/macros/content.html | 4 +-- templates/macros/format_date.html | 33 ++++++++++++++++++++++++ templates/macros/list_posts.html | 2 +- templates/section.html | 4 ++- 9 files changed, 92 insertions(+), 51 deletions(-) create mode 100644 templates/macros/cards_pages.html delete mode 100644 templates/macros/cards_posts.html create mode 100644 templates/macros/format_date.html diff --git a/config.toml b/config.toml index cf5538474..f438d51cf 100644 --- a/config.toml +++ b/config.toml @@ -16,8 +16,13 @@ highlight_code = true highlight_theme = "css" [extra] -# If unset, uses format: "6 July 2049" ("%d %B %Y"). Don't leave it empty or dates won't appear. Either use a proper format or comment out the line. -# date_format = "%d %B %Y" +# Date format used when listing posts (main page, /blog section, tag posts list…) +# Default is "6th July 2049". +long_date_format = "" + +# Date format used for blog posts. +# Default is "31st Dec 2011". +short_date_format = "" # Custom separator used in title tag and posts metadata (between date, time to read, and tags). separator = "•" diff --git a/templates/base.html b/templates/base.html index 7cec6b3dc..cba0fe3bf 100644 --- a/templates/base.html +++ b/templates/base.html @@ -2,9 +2,10 @@ {% import "macros/page_header.html" as macros_page_header %} {% import "macros/page_desc.html" as macros_page_desc %} {% import "macros/content.html" as macros_content %} -{% import "macros/cards_posts.html" as macros_cards_posts %} +{% import "macros/cards_pages.html" as macros_cards_pages %} {% import "macros/set_title.html" as macros_set_title %} {% import "macros/paginate.html" as macros_paginate %} +{% import "macros/format_date.html" as macros_format_date %} diff --git a/templates/cards.html b/templates/cards.html index 4f02fa55b..feed44658 100644 --- a/templates/cards.html +++ b/templates/cards.html @@ -14,7 +14,7 @@ {%- set show_pages = section.pages -%} {% endif -%} - {{ macros_cards_posts::cards_posts(pages=show_pages) }} + {{ macros_cards_pages::cards_pages(pages=show_pages) }} {% if paginator %} diff --git a/templates/macros/cards_pages.html b/templates/macros/cards_pages.html new file mode 100644 index 000000000..40b1e5d00 --- /dev/null +++ b/templates/macros/cards_pages.html @@ -0,0 +1,43 @@ +{% macro cards_pages(pages) %} + +
+ {%- for page in pages %} +
+ {% if page.extra.local_image %} + {{ + {% elif page.extra.remote_image %} + {{ + {% else %} +
+ {% endif %} + +
+

+ {% if page.extra.link_to %} + {{page.title}} + {% else %} + {{page.title}} + {% endif %} +

+ +
+ {% if page.date %} + {{ macros_format_date::format_date(date=page.date, short=false) }} + {% endif %} + {% if page.draft %} + DRAFT + {% endif %} +
+ +
+ {% if page.description %} + {{ page.description }} + {% endif %} +
+
+
+ + {% endfor -%} +
+ +{% endmacro cards_pages %} diff --git a/templates/macros/cards_posts.html b/templates/macros/cards_posts.html deleted file mode 100644 index 4edb704ec..000000000 --- a/templates/macros/cards_posts.html +++ /dev/null @@ -1,43 +0,0 @@ -{% macro cards_posts(pages) %} - -
- {%- for page in pages %} -
- {% if page.extra.local_image %} - {{ - {% elif page.extra.remote_image %} - {{ - {% else %} -
- {% endif %} - -
-

- {% if page.extra.link_to %} - {{page.title}} - {% else %} - {{page.title}} - {% endif %} -

- -
- {%- if page.date %} - {{ page.date | date(format=config.extra.date_format | default (value="%d %B %Y")) }} - {% endif -%} - {% if page.draft %} - DRAFT - {% endif %} -
- -
- {% if page.description %} - {{ page.description }} - {% endif %} -
-
-
- - {% endfor -%} -
- -{% endmacro cards_posts %} diff --git a/templates/macros/content.html b/templates/macros/content.html index e19e6b8e4..fd11703ba 100644 --- a/templates/macros/content.html +++ b/templates/macros/content.html @@ -14,7 +14,7 @@ {% endif %} {% if page.date %} -
  • {{ page.date | date(format=config.extra.date_format | default (value="%d %B %Y")) }} {{ separator }}
  • +
  • {{ macros_format_date::format_date(date=page.date, short=true) }} {{ separator }}
  • {% endif %}
  •  {{ page.reading_time }} min read
  • @@ -31,7 +31,7 @@ {% endif %} {% if page.updated %} - diff --git a/templates/macros/format_date.html b/templates/macros/format_date.html new file mode 100644 index 000000000..a48748483 --- /dev/null +++ b/templates/macros/format_date.html @@ -0,0 +1,33 @@ +{% macro format_date(date, short) %} + +{% if config.extra.short_date_format %} + {{ date | date(format=config.extra.short_date_format) }} +{% elif config.extra.long_date_format %} + {{ date | date(format=config.extra.long_date_format) }} +{% else %} + {% set day = date | date(format='%-d') | int %} + + {% if day in [11, 12, 13] %} + {% set suffix = "th" %} + {% else %} + {% set last_digit = day % 10 %} + {% if last_digit == 1 %} + {% set suffix = "st" %} + {% elif last_digit == 2 %} + {% set suffix = "nd" %} + {% elif last_digit == 3 %} + {% set suffix = "rd" %} + {% else %} + {% set suffix = "th" %} + {% endif %} + {% endif %} + + {# Return the date. #} + {% if short == true %} + {{ date | date(format="%-d") }}{{ suffix }} {{ date | date(format="%b %Y") }} + {% else %} + {{ date | date(format="%-d") }}{{ suffix }} {{ date | date(format="%B %Y") }} + {% endif %} +{% endif %} + +{% endmacro %} diff --git a/templates/macros/list_posts.html b/templates/macros/list_posts.html index 3f6c2e3c8..29715211a 100644 --- a/templates/macros/list_posts.html +++ b/templates/macros/list_posts.html @@ -13,7 +13,7 @@ {% if post.date %}
    - {{ post.date | date(format=config.extra.date_format | default (value="%d %B %Y")) }} + {{ macros_format_date::format_date(date=post.date, short=false) }}
    {% endif %} diff --git a/templates/section.html b/templates/section.html index f3f95dd98..46c73d85f 100644 --- a/templates/section.html +++ b/templates/section.html @@ -2,6 +2,7 @@ {% block main_content %} +
    {% if section.extra.section_path -%} {% set extra_section = get_section(path=section.extra.section_path) %} {% endif -%} @@ -10,7 +11,7 @@ {{ macros_page_desc::page_desc(desc=section.extra.header, page=section) }} {% endif -%} -
    +
    {{ macros_page_header::page_header(title=section.title) }}
    @@ -23,6 +24,7 @@ {% set max = section.extra.max_posts | default(value=999999) %} {{ macros_list_posts::list_posts(posts=pages, max=max) }} +
    {% if paginator %}