Skip to content

Commit 22c575e

Browse files
authored
Merge pull request #129 from timvink/fix_exclude
Fix exclude() function to deal with folders
2 parents 4b17176 + 1deff72 commit 22c575e

File tree

7 files changed

+52
-34
lines changed

7 files changed

+52
-34
lines changed

docs/options.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,4 @@ plugins:
9595
```
9696

9797
`exclude`
98-
: Default is empty. Allows to specify a list of page source paths that should not be included in the print page. See [Do Not Print](how-to/do_not_print.md#ignoring-an-entire-page) for more info.
98+
: Default is empty. Allows to specify a list of page source paths that should not be included in the print page. Supports [glob](https://docs.python.org/3/library/glob.html)-like syntax such as `folder/` or `folder/*`. See [Do Not Print](how-to/do_not_print.md#ignoring-an-entire-page) for more info on excluding pages.
+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "2.7.0"
1+
__version__ = "2.7.1"

src/mkdocs_print_site_plugin/exclude.py

+39-31
Original file line numberDiff line numberDiff line change
@@ -8,40 +8,48 @@
88
import fnmatch
99
from typing import List
1010

11+
12+
import fnmatch
13+
import os
14+
from typing import List
1115

12-
def exclude(src_path: str, globs: List[str]) -> bool:
16+
def exclude(path: str, exclude_patterns: List[str]) -> bool:
1317
"""
14-
Determine if a src_path should be excluded.
15-
16-
Supports globs (e.g. folder/* or *.md).
17-
Credits: code inspired by / adapted from
18-
https://github.com/apenwarr/mkdocs-exclude/blob/master/mkdocs_exclude/plugin.py
19-
18+
Check if a path should be excluded based on a list of patterns.
19+
2020
Args:
21-
src_path (src): Path of file
22-
globs (list): list of globs
21+
path: The path to check
22+
exclude_patterns: List of glob patterns to exclude
23+
2324
Returns:
24-
(bool): whether src_path should be excluded
25+
True if the path should be excluded, False otherwise
2526
"""
26-
assert isinstance(src_path, str)
27-
assert isinstance(globs, list)
28-
29-
for g in globs:
30-
if fnmatch.fnmatchcase(src_path, g):
31-
return True
32-
33-
# Windows reports filenames as eg. a\\b\\c instead of a/b/c.
34-
# To make the same globs/regexes match filenames on Windows and
35-
# other OSes, let's try matching against converted filenames.
36-
# On the other hand, Unix actually allows filenames to contain
37-
# literal \\ characters (although it is rare), so we won't
38-
# always convert them. We only convert if os.sep reports
39-
# something unusual. Conversely, some future mkdocs might
40-
# report Windows filenames using / separators regardless of
41-
# os.sep, so we *always* test with / above.
42-
if os.sep != "/":
43-
src_path_fix = src_path.replace(os.sep, "/")
44-
if fnmatch.fnmatchcase(src_path_fix, g):
27+
assert isinstance(path, str)
28+
assert isinstance(exclude_patterns, list)
29+
30+
if not exclude_patterns:
31+
return False
32+
33+
# Normalize path separators to handle both Windows and Unix paths
34+
path = path.replace('\\', '/')
35+
36+
for pattern in exclude_patterns:
37+
# Normalize pattern separators
38+
pattern = pattern.replace('\\', '/')
39+
40+
# Check for directory patterns (ending with /)
41+
if pattern.endswith('/'):
42+
if path.startswith(pattern) or path.startswith(pattern[:-1] + '/'):
4543
return True
46-
47-
return False
44+
# Regular glob pattern matching
45+
elif fnmatch.fnmatch(path, pattern):
46+
return True
47+
# Check if path is in a directory that matches the pattern
48+
elif '/' in path:
49+
path_parts = path.split('/')
50+
for i in range(1, len(path_parts)):
51+
partial_path = '/'.join(path_parts[:i])
52+
if fnmatch.fnmatch(partial_path, pattern) or partial_path == pattern:
53+
return True
54+
55+
return False
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Page in subfolder
2+
3+
unique code we can search for during unit tests: d1231dct9dkqwn2

tests/fixtures/projects/basic/mkdocs_do_not_print.yml

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ plugins:
44
- print-site:
55
exclude:
66
- z.md
7-
- subsection1.md
7+
- subsection1.md
8+
- subfolder/
89

910
markdown_extensions:
1011
- attr_list

tests/test_building.py

+4
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,10 @@ def test_exclude_page(tmp_path):
308308
'<p class="print-site-plugin-ignore">This paragraph is ignored, this unique code should not be found: V5lI1bUdnUI9</p>', # noqa
309309
)
310310

311+
# Test that globs also work for entire directories
312+
# subfolder/another_page.md has a code 'd1231dct9dkqwn2' that should not be present because we excluded the subfolder/ directory
313+
assert not text_in_page(prj_path, "print_page/index.html", "d1231dct9dkqwn2")
314+
311315

312316
def test_basic_build99(tmp_path):
313317
"""

tests/test_exclude.py

+2
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,6 @@ def test_exclude():
1717

1818
globs = ["folder/*"]
1919
assert exclude("folder/index.md", globs)
20+
assert exclude("folder/index.md", ["folder"])
2021
assert not exclude("subfolder/index.md", globs)
22+
assert not exclude("subfolder", globs)

0 commit comments

Comments
 (0)