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

add match_path option to filter files #43

Merged
merged 6 commits into from
Feb 22, 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
6 changes: 1 addition & 5 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -201,11 +201,7 @@ dmypy.json
# ####################################################################################
# ## VisualStudioCode ######################
# ##########################################
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
.vscode
*.code-workspace

### VisualStudioCode Patch ###
Expand Down
7 changes: 0 additions & 7 deletions .vscode/extensions.json

This file was deleted.

25 changes: 0 additions & 25 deletions .vscode/settings.json

This file was deleted.

6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Removed -->

## 0.13.0

### Added

- add `match_path` option which should be a regex pattern matching the path to your files within the docs_dir. See [issue #34](https://github.com/Guts/mkdocs-rss-plugin/issues/34) and the related [PR #43](https://github.com/Guts/mkdocs-rss-plugin/pull/43). Contributed by [Ryan Morshead](https://github.com/rmorshea/).

----

## 0.12.0
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ plugins:
image: https://upload.wikimedia.org/wikipedia/commons/thumb/4/43/Feed-icon.svg/128px-Feed-icon.svg.png
length: 20
pretty_print: false
match_path: ".*"
```

For further information, [see the user documentation](https://guts.github.io/mkdocs-rss-plugin/).
Expand Down
12 changes: 12 additions & 0 deletions docs/usage/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,18 @@ plugins:

Default: `False`.

### Filter pages

This adds a `match_path` option which should be a regex pattern matching the path to your files within the `docs_dir`. For example if you had a blog under `docs/blog` where `docs_dir` is `docs` you might use:

```yaml
plugins:
- rss:
match_path: "blog/.*"
```

Default: `None`.

----

## Integration
Expand Down
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ plugins:
as_update: false
datetime_format: "%Y-%m-%d %H:%M"
image: https://upload.wikimedia.org/wikipedia/commons/thumb/4/43/Feed-icon.svg/128px-Feed-icon.svg.png
match_path: ".*"
pretty_print: false
- search

Expand Down
1 change: 1 addition & 0 deletions mkdocs_rss_plugin/customtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ class PageInformation(NamedTuple):
description: str = None
image: str = None
url_full: str = None
src_path: str = None
4 changes: 4 additions & 0 deletions mkdocs_rss_plugin/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,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)),
)

def __init__(self):
Expand Down Expand Up @@ -188,6 +189,7 @@ 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 @@ -215,6 +217,7 @@ 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 @@ -224,6 +227,7 @@ 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
8 changes: 6 additions & 2 deletions mkdocs_rss_plugin/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

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

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

:param pages: pages to filter
:type pages: dict
:type pages: list
:param attribute: page attribute as filter variable
:type attribute: str
:param length: max number of pages to return
Expand All @@ -471,9 +472,12 @@ def filter_pages(pages: dict, attribute: str, length: int) -> list:
: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
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Page to test feature

See: <https://github.com/Guts/mkdocs-rss-plugin/pull/43/>
5 changes: 2 additions & 3 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@
import unittest
from pathlib import Path

# mkdocs
from mkdocs.structure.pages import Page

# plugin target
from mkdocs_rss_plugin.plugin import GitRssPlugin

Expand Down Expand Up @@ -61,6 +58,7 @@ def test_plugin_config_defaults(self):
"image": None,
"length": 20,
"pretty_print": False,
"match_path": None,
}

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

# custom config
Expand Down