|
8 | 8 | import logging |
9 | 9 | from collections.abc import Iterable |
10 | 10 | from datetime import date, datetime |
11 | | -from email.utils import format_datetime |
12 | 11 | from functools import lru_cache |
13 | 12 | from mimetypes import guess_type |
14 | 13 | from pathlib import Path |
15 | | -from typing import Any, Union |
| 14 | +from typing import Any, Literal, Union |
16 | 15 | from urllib.parse import urlencode, urlparse, urlunparse |
17 | 16 |
|
18 | 17 | # 3rd party |
@@ -797,36 +796,26 @@ def guess_locale(self, mkdocs_config: MkDocsConfig) -> Optional[str]: |
797 | 796 | return None |
798 | 797 |
|
799 | 798 | @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]: |
801 | 804 | """Filter and return pages into a friendly RSS structure. |
802 | 805 |
|
803 | 806 | Args: |
804 | 807 | pages (list): pages to filter |
805 | | - attribute (str): page attribute as filter variable |
| 808 | + filter_attribute (str): page attribute to use as filter variable |
806 | 809 | length (int): max number of pages to return |
807 | 810 |
|
808 | 811 | Returns: |
809 | | - list: list of filtered pages |
| 812 | + list[dict]: list of filtered pages as RSS item dict |
810 | 813 | """ |
811 | 814 | filtered_pages = [] |
812 | 815 | 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 |
814 | 817 | )[: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)) |
830 | 819 |
|
831 | 820 | return filtered_pages |
832 | 821 |
|
|
0 commit comments