Skip to content

Commit

Permalink
fix #229 allow date.created to get creation date
Browse files Browse the repository at this point in the history
  • Loading branch information
copdips committed Jan 11, 2024
1 parent 5256a50 commit 9e09ef3
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 5 deletions.
38 changes: 33 additions & 5 deletions mkdocs_rss_plugin/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down
35 changes: 35 additions & 0 deletions tests/test_rss_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 ########
Expand Down

0 comments on commit 9e09ef3

Please sign in to comment.