From 9e09ef3ad5c51f2940b4062e349b70596406774e Mon Sep 17 00:00:00 2001 From: Xiang ZHU Date: Fri, 5 Jan 2024 16:32:19 +0100 Subject: [PATCH] fix #229 allow date.created to get creation date --- mkdocs_rss_plugin/util.py | 38 +++++++++++++++++++++++++++++++++----- tests/test_rss_util.py | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 5 deletions(-) diff --git a/mkdocs_rss_plugin/util.py b/mkdocs_rss_plugin/util.py index 4a8a446e..611157c6 100644 --- a/mkdocs_rss_plugin/util.py +++ b/mkdocs_rss_plugin/util.py @@ -12,7 +12,7 @@ from email.utils import format_datetime from mimetypes import guess_type from pathlib import Path -from typing import Iterable, Optional, Tuple +from typing import Any, Iterable, Optional, Tuple from urllib import request from urllib.error import HTTPError, URLError from urllib.parse import urlencode, urlparse, urlunparse @@ -139,6 +139,28 @@ def build_url(self, base_url: str, path: str, args_dict: dict = None) -> str: url_parts[4] = urlencode(args_dict) return urlunparse(url_parts) + def get_value_from_dot_key(self, data: dict, dot_key: Any) -> Any: + """ + Retrieves a value from a dictionary using a dot notation key. + + :param data: The dictionary from which to retrieve the value. + :type data: dict + :param dot_key: The key in dot notation to specify the path in the dictionary. + :type dot_key: Any + + :return: The value retrieved from the dictionary, or None if the key + does not exist. + :rtype: Any + """ + if not isinstance(dot_key, str): + return data.get(dot_key) + for key in dot_key.split("."): + if isinstance(data, dict) and key in data: + data = data[key] + else: + return None + return data + def get_file_dates( self, in_page: Page, @@ -174,10 +196,13 @@ def get_file_dates( # if enabled, try to retrieve dates from page metadata if not self.use_git or ( - source_date_creation != "git" and in_page.meta.get(source_date_creation) + source_date_creation != "git" + and self.get_value_from_dot_key(in_page.meta, source_date_creation) ): dt_created = self.get_date_from_meta( - date_metatag_value=in_page.meta.get(source_date_creation), + date_metatag_value=self.get_value_from_dot_key( + in_page.meta, source_date_creation + ), meta_datetime_format=meta_datetime_format, meta_datetime_timezone=meta_default_timezone, meta_default_time=meta_default_time, @@ -195,10 +220,13 @@ def get_file_dates( ) if not self.use_git or ( - source_date_update != "git" and in_page.meta.get(source_date_update) + source_date_update != "git" + and self.get_value_from_dot_key(in_page.meta, source_date_update) ): dt_updated = self.get_date_from_meta( - date_metatag_value=in_page.meta.get(source_date_update), + date_metatag_value=self.get_value_from_dot_key( + in_page.meta, source_date_update + ), meta_datetime_format=meta_datetime_format, meta_datetime_timezone=meta_default_timezone, meta_default_time=meta_default_time, diff --git a/tests/test_rss_util.py b/tests/test_rss_util.py index 5c3a0329..a452148f 100644 --- a/tests/test_rss_util.py +++ b/tests/test_rss_util.py @@ -95,6 +95,41 @@ def test_remote_image_none(self): ) self.assertIsNone(img_length) + def test_get_value_from_dot_key(self): + param_list = [ + { + "meta": {"date": "2021-09-01"}, + "value_location": "date", + "value": "2021-09-01", + }, + { + "meta": {"date": {"created": "2021-09-01"}}, + "value_location": "date.created", + "value": "2021-09-01", + }, + { + "meta": {"date": "2021-09-01"}, + "value_location": "date.created", + "value": None, + }, + { + "meta": {True: "bool_as_key"}, + "value_location": True, + "value": "bool_as_key", + }, + { + "meta": {"date": "2021-09-01"}, + "value_location": True, + "value": None, + }, + ] + for param in param_list: + with self.subTest(param=param): + result = self.plg_utils.get_value_from_dot_key( + param["meta"], param["value_location"] + ) + self.assertEqual(result, param["value"]) + # ############################################################################## # ##### Stand alone program ########