Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Completely skip pages excluded by match_path #49

Merged
merged 4 commits into from
Mar 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,9 @@ venv.bak/
# Rope project settings
.ropeproject

# JetBrains project settings
.idea

# mkdocs documentation
/site

Expand Down
2 changes: 1 addition & 1 deletion docs/usage/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ plugins:
match_path: "blog/.*"
```

Default: `None`.
Default: `.*`.

----

Expand Down
1 change: 0 additions & 1 deletion mkdocs_rss_plugin/customtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,3 @@ class PageInformation(NamedTuple):
description: str = None
image: str = None
url_full: str = None
src_path: str = None
13 changes: 9 additions & 4 deletions mkdocs_rss_plugin/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from copy import deepcopy
from email.utils import formatdate
from pathlib import Path
from re import compile

# 3rd party
from jinja2 import Environment, FileSystemLoader, select_autoescape
Expand Down Expand Up @@ -49,7 +50,7 @@ class GitRssPlugin(BasePlugin):
("image", config_options.Type(str, default=None)),
("length", config_options.Type(int, default=20)),
("pretty_print", config_options.Type(bool, default=False)),
("match_path", config_options.Type(str, default=None)),
("match_path", config_options.Type(str, default=".*")),
)

def __init__(self):
Expand Down Expand Up @@ -105,6 +106,9 @@ def on_config(self, config: config_options.Config) -> dict:
if self.config.get("image"):
base_feed["logo_url"] = self.config.get("image")

# pattern to match pages included in output
self.match_path_pattern = compile(self.config.get("match_path"))

# date handling
if self.config.get("date_from_meta") is not None:
self.src_date_created = self.config.get("date_from_meta").get(
Expand Down Expand Up @@ -166,6 +170,10 @@ def on_page_markdown(
Returns:
str: Markdown source text of page as string
"""
# skip pages that don't match the config var match_path
if not self.match_path_pattern.match(page.file.src_path):
return

# retrieve dates from git log
page_dates = self.util.get_file_dates(
in_page=page,
Expand All @@ -189,7 +197,6 @@ def on_page_markdown(
in_page=page, base_url=config.get("site_url", __uri__)
),
url_full=page.canonical_url,
src_path=page.file.src_path,
)
)

Expand Down Expand Up @@ -217,7 +224,6 @@ def on_post_build(self, config: config_options.Config) -> dict:
pages=self.pages_to_filter,
attribute="created",
length=self.config.get("length", 20),
match_path=self.config.get("match_path"),
)
)

Expand All @@ -227,7 +233,6 @@ def on_post_build(self, config: config_options.Config) -> dict:
pages=self.pages_to_filter,
attribute="updated",
length=self.config.get("length", 20),
match_path=self.config.get("match_path"),
)
)

Expand Down
6 changes: 1 addition & 5 deletions mkdocs_rss_plugin/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

# standard library
import logging
import re
import ssl
from datetime import date, datetime
from email.utils import formatdate
Expand Down Expand Up @@ -458,7 +457,7 @@ def guess_locale(mkdocs_config: Config) -> str or None:
return None

@staticmethod
def filter_pages(pages: list, attribute: str, length: int, match_path: str) -> list:
def filter_pages(pages: list, attribute: str, length: int) -> list:
"""Filter and return pages into a friendly RSS structure.

:param pages: pages to filter
Expand All @@ -472,12 +471,9 @@ def filter_pages(pages: list, attribute: str, length: int, match_path: str) -> l
:rtype: list
"""
filtered_pages = []
path_pattern = re.compile(match_path) if match_path else None
for page in sorted(
pages, key=lambda page: getattr(page, attribute), reverse=True
)[:length]:
if path_pattern and not path_pattern.match(page.src_path):
continue
filtered_pages.append(
{
"authors": page.authors,
Expand Down
4 changes: 2 additions & 2 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def test_plugin_config_defaults(self):
"image": None,
"length": 20,
"pretty_print": False,
"match_path": None,
"match_path": ".*",
}

# load
Expand All @@ -78,7 +78,7 @@ def test_plugin_config_image(self):
"image": self.feed_image,
"length": 20,
"pretty_print": False,
"match_path": None,
"match_path": ".*",
}

# custom config
Expand Down