Skip to content

Commit 8406da3

Browse files
committed
update(model): move serialization to RSS item as PageInformation method
1 parent 00a35c4 commit 8406da3

File tree

2 files changed

+28
-22
lines changed

2 files changed

+28
-22
lines changed

mkdocs_rss_plugin/models.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@
99
from collections.abc import MutableMapping
1010
from dataclasses import dataclass, field
1111
from datetime import datetime
12+
from email.utils import format_datetime
1213
from pathlib import Path
13-
from typing import Any, Optional
14+
from typing import Any, Literal, Optional
1415

1516
# 3rd party
1617
from mkdocs.structure.pages import Page
@@ -66,6 +67,22 @@ class PageInformation:
6667
default=None, repr=False, compare=False
6768
)
6869

70+
def as_rss_item(self, date_type: Literal["created", "updated"]) -> dict:
71+
"""Return the object as a dictionary formatted for RSS item."""
72+
pub_date: datetime = getattr(self, date_type)
73+
return {
74+
"authors": self.authors,
75+
"categories": self.categories,
76+
"comments_url": self.url_comments,
77+
"description": self.description,
78+
"guid": self.guid,
79+
"image": self.image,
80+
"link": self.url_full,
81+
"pubDate": format_datetime(dt=pub_date),
82+
"pubDate3339": pub_date.isoformat("T"),
83+
"title": self.title,
84+
}
85+
6986

7087
@dataclass
7188
class RssFeedBase:

mkdocs_rss_plugin/util.py

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,10 @@
88
import logging
99
from collections.abc import Iterable
1010
from datetime import date, datetime
11-
from email.utils import format_datetime
1211
from functools import lru_cache
1312
from mimetypes import guess_type
1413
from pathlib import Path
15-
from typing import Any, Union
14+
from typing import Any, Literal, Union
1615
from urllib.parse import urlencode, urlparse, urlunparse
1716

1817
# 3rd party
@@ -797,36 +796,26 @@ def guess_locale(self, mkdocs_config: MkDocsConfig) -> Optional[str]:
797796
return None
798797

799798
@staticmethod
800-
def filter_pages(pages: list[PageInformation], attribute: str, length: int) -> list:
799+
def filter_pages(
800+
pages: list[PageInformation],
801+
filter_attribute: Literal["created", "updated"],
802+
length: int,
803+
) -> list[dict]:
801804
"""Filter and return pages into a friendly RSS structure.
802805
803806
Args:
804807
pages (list): pages to filter
805-
attribute (str): page attribute as filter variable
808+
filter_attribute (str): page attribute to use as filter variable
806809
length (int): max number of pages to return
807810
808811
Returns:
809-
list: list of filtered pages
812+
list[dict]: list of filtered pages as RSS item dict
810813
"""
811814
filtered_pages = []
812815
for page in sorted(
813-
pages, key=lambda page: getattr(page, attribute), reverse=True
816+
pages, key=lambda page: getattr(page, filter_attribute), reverse=True
814817
)[:length]:
815-
pub_date: datetime = getattr(page, attribute)
816-
filtered_pages.append(
817-
{
818-
"authors": page.authors,
819-
"categories": page.categories,
820-
"comments_url": page.url_comments,
821-
"description": page.description,
822-
"guid": page.guid,
823-
"image": page.image,
824-
"link": page.url_full,
825-
"pubDate": format_datetime(dt=pub_date),
826-
"pubDate3339": pub_date.isoformat("T"),
827-
"title": page.title,
828-
}
829-
)
818+
filtered_pages.append(page.as_rss_item(date_type=filter_attribute))
830819

831820
return filtered_pages
832821

0 commit comments

Comments
 (0)