Skip to content

Commit aee67a6

Browse files
committed
Add windows support and glob patterns in exclude parameter
1 parent fae06c6 commit aee67a6

File tree

4 files changed

+69
-2
lines changed

4 files changed

+69
-2
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ Default is `false`. If set to `true` the plugin will use the time at `mkdocs bui
154154

155155
### `exclude`
156156

157-
Default is empty. Specify a list of page source paths (one per line) that should not have a revision date included. This can be useful for example to remove the revision date from the front page. The source page is relative to your `docs/` folder. To exclude `docs/subfolder/page.md` specify in your `mkdocs.yml` a line under `exclude:` with `- subfolder/page.md`. Example:
157+
Default is empty. Specify a list of page source paths (one per line) that should not have a revision date included (excluded from processing by this plugin). This can be useful for example to remove the revision date from the front page. The source path of a page is relative to your `docs/` folder. You can also use [globs](https://docs.python.org/3/library/glob.html) instead of full source paths. To exclude `docs/subfolder/page.md` specify in your `mkdocs.yml` a line under `exclude:` with `- subfolder/page.md`. Some examples:
158158

159159
```yaml
160160
# mkdocs.yml
@@ -164,6 +164,7 @@ plugins:
164164
- index.md
165165
- subfolder/page.md
166166
- another_page.md
167+
- folder/*
167168
```
168169

169170
## Contributing
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
"""
2+
Module to assist exclude certain files being processed by plugin.
3+
4+
Inspired by https://github.com/apenwarr/mkdocs-exclude
5+
"""
6+
import os
7+
import fnmatch
8+
from typing import List
9+
10+
11+
def exclude(src_path: str, globs: List[str]) -> bool:
12+
"""
13+
Determine if a src_path should be excluded.
14+
15+
Supports globs (e.g. folder/* or *.md).
16+
Credits: code inspired by / adapted from
17+
https://github.com/apenwarr/mkdocs-exclude/blob/master/mkdocs_exclude/plugin.py
18+
19+
Args:
20+
src_path (src): Path of file
21+
globs (list): list of globs
22+
Returns:
23+
(bool): whether src_path should be excluded
24+
"""
25+
assert isinstance(src_path, str)
26+
assert isinstance(globs, list)
27+
28+
for g in globs:
29+
if fnmatch.fnmatchcase(src_path, g):
30+
return True
31+
32+
# Windows reports filenames as eg. a\\b\\c instead of a/b/c.
33+
# To make the same globs/regexes match filenames on Windows and
34+
# other OSes, let's try matching against converted filenames.
35+
# On the other hand, Unix actually allows filenames to contain
36+
# literal \\ characters (although it is rare), so we won't
37+
# always convert them. We only convert if os.sep reports
38+
# something unusual. Conversely, some future mkdocs might
39+
# report Windows filenames using / separators regardless of
40+
# os.sep, so we *always* test with / above.
41+
if os.sep != "/":
42+
src_path_fix = src_path.replace(os.sep, "/")
43+
if fnmatch.fnmatchcase(src_path_fix, g):
44+
return True
45+
46+
return False

mkdocs_git_revision_date_localized_plugin/plugin.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
# package modules
1919
from mkdocs_git_revision_date_localized_plugin.util import Util
20+
from mkdocs_git_revision_date_localized_plugin.exclude import exclude
2021

2122
from typing import Any, Dict
2223

@@ -142,7 +143,7 @@ def on_page_markdown(
142143
"""
143144
# Exclude pages specified in config
144145
excluded_pages = self.config.get("exclude", [])
145-
if page.file.src_path in excluded_pages:
146+
if exclude(page.file.src_path, excluded_pages):
146147
logging.debug("Excluding page " + page.file.src_path)
147148
return markdown
148149

tests/test_exclude.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from mkdocs_git_revision_date_localized_plugin.exclude import exclude
2+
import pytest
3+
4+
def test_exclude():
5+
6+
with pytest.raises(AssertionError):
7+
exclude("fsdfs", "not a list")
8+
9+
globs = ["index.md"]
10+
assert exclude("index.md", globs)
11+
12+
globs = ["*.md"]
13+
assert exclude("index.md", globs)
14+
assert exclude("folder/index.md", globs)
15+
assert exclude("folder\\index.md", globs)
16+
17+
globs = ["folder/*"]
18+
assert exclude("folder/index.md", globs)
19+
assert not exclude("subfolder/index.md", globs)

0 commit comments

Comments
 (0)