Skip to content

Commit efda66d

Browse files
authored
fix #229 allow date.created to get creation date (#237)
2 parents d7b5c81 + 2ecbc87 commit efda66d

6 files changed

Lines changed: 199 additions & 32 deletions

File tree

docs/configuration.md

Lines changed: 57 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -309,8 +309,8 @@ Basically, the plugin aims to retrieve creation and update dates from git log. B
309309

310310
So, it's possible to use the dates manually specified into the [page metadata] through the [YAML frontmatter](https://www.mkdocs.org/user-guide/writing-your-docs/#meta-data).
311311

312-
- `as_creation`: meta tag name to use as creation date. Default to `False`.
313-
- `as_update`: meta tag name to use as update date. Default to `False`.
312+
- `as_creation`: meta tag name (or a dot-separated tag name) to use as creation date. Default to `False`.
313+
- `as_update`: meta tag name (or a dot-separated tag name) to use as update date. Default to `False`.
314314
- `datetime_format`: datetime format. Default to `"%Y-%m-%d %H:%M"`.
315315
- `default_timezone`: timezone to use by default to make aware datetimes. Default to `UTC`. Introduced in version 1.3.0 with [PR 142](https://github.com/Guts/mkdocs-rss-plugin/pull/142).
316316
- `default_time`: time to use if page contains only a date. Useful to avoid the 'midnight syndrome' or have to specify hour in every single page. Default to `None`. 24h-clock format is expected: `%H:%M`. Example: `"14:20"`. Introduced in version 1.4.0 with [PR 145](https://github.com/Guts/mkdocs-rss-plugin/pull/145).
@@ -319,30 +319,60 @@ So, it's possible to use the dates manually specified into the [page metadata] t
319319

320320
For example, in your `best_article.md` created in 2019, you can write the front-matter like this:
321321

322-
```markdown
323-
---
324-
title: "This page title is a perfect clickbait!"
325-
authors:
326-
- "Julien M."
327-
date: "2020-10-22 17:18"
328-
---
329-
330-
# This plugin will change your MkDocs life
331-
332-
Lorem ipsum [...]
333-
```
334-
335-
So in your `mkdocs.yml` you will have:
336-
337-
```yaml
338-
plugins:
339-
- rss:
340-
date_from_meta:
341-
as_creation: "date"
342-
as_update: false
343-
datetime_format: "%Y-%m-%d %H:%M"
344-
default_timezone: Europe/Paris
345-
```
322+
=== "tag name: `date`"
323+
324+
```markdown hl_lines="5"
325+
---
326+
title: "This page title is a perfect clickbait!"
327+
authors:
328+
- "Julien M."
329+
date: "2020-10-22 17:18"
330+
---
331+
332+
# This plugin will change your MkDocs life
333+
334+
Lorem ipsum [...]
335+
```
336+
337+
So in your `mkdocs.yml` you will have:
338+
339+
```yaml hl_lines="4-5"
340+
plugins:
341+
- rss:
342+
date_from_meta:
343+
as_creation: "date"
344+
as_update: false
345+
datetime_format: "%Y-%m-%d %H:%M"
346+
default_timezone: Europe/Paris
347+
```
348+
349+
=== "dot-separated tag name: `date.created`"
350+
351+
```markdown hl_lines="6"
352+
---
353+
title: "This page title is a perfect clickbait!"
354+
authors:
355+
- "Julien M."
356+
date:
357+
created: "2020-10-22 17:18"
358+
---
359+
360+
# This plugin will change your MkDocs life
361+
362+
Lorem ipsum [...]
363+
```
364+
365+
So in your `mkdocs.yml` you will have:
366+
367+
```yaml hl_lines="4-5"
368+
plugins:
369+
- rss:
370+
date_from_meta:
371+
as_creation: "date.created"
372+
as_update: false
373+
datetime_format: "%Y-%m-%d %H:%M"
374+
default_timezone: Europe/Paris
375+
```
346376

347377
At the end, into the RSS you will get:
348378

@@ -395,7 +425,7 @@ Default: `.*`.
395425

396426
### `url_parameters`: additional URL parameters
397427

398-
This option allows you to add parameters to the URLs of the RSS feed items. It works as a dictionary of keys/values that is passed to [Python *urllib.parse.urlencode*](https://docs.python.org/3/library/urllib.parse.html#urllib.parse.urlencode).
428+
This option allows you to add parameters to the URLs of the RSS feed items. It works as a dictionary of keys/values that is passed to [Python *urllib.parse.urlencode*](https://docs.python.org/3/library/urllib.parse.html#urllib.parse.urlencode).
399429
One possible use case is the addition of [Urchin Tracking Module (UTM) parameters](https://en.wikipedia.org/wiki/UTM_parameters):
400430

401431
```yaml

mkdocs_rss_plugin/util.py

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from email.utils import format_datetime
1313
from mimetypes import guess_type
1414
from pathlib import Path
15-
from typing import Iterable, Optional, Tuple
15+
from typing import Any, Iterable, Optional, Tuple, Union
1616
from urllib import request
1717
from urllib.error import HTTPError, URLError
1818
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:
139139
url_parts[4] = urlencode(args_dict)
140140
return urlunparse(url_parts)
141141

142+
def get_value_from_dot_key(self, data: dict, dot_key: Union[str, bool]) -> Any:
143+
"""
144+
Retrieves a value from a dictionary using a dot notation key.
145+
146+
:param data: The dictionary from which to retrieve the value.
147+
:type data: dict
148+
:param dot_key: The key in dot notation to specify the path in the dictionary.
149+
:type dot_key: Union[str, bool]
150+
151+
:return: The value retrieved from the dictionary, or None if the key
152+
does not exist.
153+
:rtype: Any
154+
"""
155+
if not isinstance(dot_key, str):
156+
return data.get(dot_key)
157+
for key in dot_key.split("."):
158+
if isinstance(data, dict) and key in data:
159+
data = data[key]
160+
else:
161+
return None
162+
return data
163+
142164
def get_file_dates(
143165
self,
144166
in_page: Page,
@@ -174,10 +196,13 @@ def get_file_dates(
174196

175197
# if enabled, try to retrieve dates from page metadata
176198
if not self.use_git or (
177-
source_date_creation != "git" and in_page.meta.get(source_date_creation)
199+
source_date_creation != "git"
200+
and self.get_value_from_dot_key(in_page.meta, source_date_creation)
178201
):
179202
dt_created = self.get_date_from_meta(
180-
date_metatag_value=in_page.meta.get(source_date_creation),
203+
date_metatag_value=self.get_value_from_dot_key(
204+
in_page.meta, source_date_creation
205+
),
181206
meta_datetime_format=meta_datetime_format,
182207
meta_datetime_timezone=meta_default_timezone,
183208
meta_default_time=meta_default_time,
@@ -195,10 +220,13 @@ def get_file_dates(
195220
)
196221

197222
if not self.use_git or (
198-
source_date_update != "git" and in_page.meta.get(source_date_update)
223+
source_date_update != "git"
224+
and self.get_value_from_dot_key(in_page.meta, source_date_update)
199225
):
200226
dt_updated = self.get_date_from_meta(
201-
date_metatag_value=in_page.meta.get(source_date_update),
227+
date_metatag_value=self.get_value_from_dot_key(
228+
in_page.meta, source_date_update
229+
),
202230
meta_datetime_format=meta_datetime_format,
203231
meta_datetime_timezone=meta_default_timezone,
204232
meta_default_time=meta_default_time,
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
---
2+
title: Page with meta date in dot key
3+
authors:
4+
- Guts
5+
- Tim Vink
6+
- liang2kl
7+
date:
8+
created: 2023-10-07 10:20
9+
update: 2023-10-08 10:20
10+
description: First test page of mkdocs-rss-plugin test suite
11+
image: "https://svgsilh.com/png-512/97849.png"
12+
tags:
13+
- test
14+
---
15+
16+
# Test page with meta
17+
18+
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Project information
2+
site_name: MkDocs RSS Plugin - TEST
3+
site_description: Basic setup to test against MkDocs RSS plugin
4+
site_author: Julien Moura (Guts)
5+
site_url: https://guts.github.io/mkdocs-rss-plugin
6+
copyright: 'Guts - In Geo Veritas'
7+
8+
# Repository
9+
repo_name: 'guts/mkdocs-rss-plugin'
10+
repo_url: 'https://github.com/guts/mkdocs-rss-plugin'
11+
12+
use_directory_urls: true
13+
14+
plugins:
15+
- rss:
16+
date_from_meta:
17+
# date location given as dot key
18+
as_creation: "date.created"
19+
as_update: "update"
20+
datetime_format: "%Y-%m-%d %H:%M"
21+
22+
theme:
23+
name: readthedocs
24+
25+
# Extensions to enhance markdown
26+
markdown_extensions:
27+
- meta

tests/test_build.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,35 @@ def test_bad_config(self):
510510
self.assertEqual(cli_result.exit_code, 1)
511511
self.assertIsNotNone(cli_result.exception)
512512

513+
def test_date(self):
514+
with tempfile.TemporaryDirectory() as tmpdirname:
515+
cli_result = self.build_docs_setup(
516+
testproject_path="docs",
517+
mkdocs_yml_filepath=Path(
518+
"tests/fixtures/mkdocs_dates_overridden_in_dot_key.yml"
519+
),
520+
output_path=tmpdirname,
521+
strict=True,
522+
)
523+
self.assertEqual(cli_result.exit_code, 0)
524+
self.assertIsNone(cli_result.exception)
525+
526+
feed_rss_created = feedparser.parse(
527+
Path(tmpdirname) / "feed_rss_created.xml"
528+
)
529+
for page in feed_rss_created.entries:
530+
if page.title == "Page with meta date in dot key":
531+
self.assertEqual(page.published, "Sat, 07 Oct 2023 10:20:00 +0000")
532+
break
533+
534+
feed_rss_updated = feedparser.parse(
535+
Path(tmpdirname) / "feed_rss_updated.xml"
536+
)
537+
for page in feed_rss_updated.entries:
538+
if page.title == "Page with meta date in dot key":
539+
self.assertEqual(page.published, "Sun, 08 Oct 2023 10:20:00 +0000")
540+
break
541+
513542
def test_bad_date_format(self):
514543
# add a new page without tracking it
515544
md_str = """---\ndate: 13 April 2022\n---\n\n# This page is dynamically created for test purposes\n\nHi!\n

tests/test_rss_util.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,41 @@ def test_remote_image_none(self):
9595
)
9696
self.assertIsNone(img_length)
9797

98+
def test_get_value_from_dot_key(self):
99+
param_list = [
100+
{
101+
"meta": {"date": "2021-09-01"},
102+
"value_location": "date",
103+
"value": "2021-09-01",
104+
},
105+
{
106+
"meta": {"date": {"created": "2021-09-01"}},
107+
"value_location": "date.created",
108+
"value": "2021-09-01",
109+
},
110+
{
111+
"meta": {"date": "2021-09-01"},
112+
"value_location": "date.created",
113+
"value": None,
114+
},
115+
{
116+
"meta": {True: "bool_as_key"},
117+
"value_location": True,
118+
"value": "bool_as_key",
119+
},
120+
{
121+
"meta": {"date": "2021-09-01"},
122+
"value_location": True,
123+
"value": None,
124+
},
125+
]
126+
for param in param_list:
127+
with self.subTest(param=param):
128+
result = self.plg_utils.get_value_from_dot_key(
129+
param["meta"], param["value_location"]
130+
)
131+
self.assertEqual(result, param["value"])
132+
98133

99134
# ##############################################################################
100135
# ##### Stand alone program ########

0 commit comments

Comments
 (0)