Skip to content

Commit 8cca1db

Browse files
committed
Add exclude pages option, see #55
1 parent 3531480 commit 8cca1db

File tree

6 files changed

+105
-2
lines changed

6 files changed

+105
-2
lines changed

docs/options.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ plugins:
99
show_line_count: true
1010
count_empty_lines: true
1111
fallback_to_empty: false
12+
exclude:
13+
- index.md
1214
```
1315
1416
## `show_contribution`
@@ -33,3 +35,18 @@ If this option is set to `true` (default: `false`) empty lines will count toward
3335
## `fallback_to_empty`
3436

3537
If this option is set to `true` (default: `false`) the plugin will work even outside of a proper Git environment, prompting a warning when it's the case, and resulting in empty author list.
38+
39+
## `exclude`
40+
41+
Default is empty. Specify a list of page source paths (one per line) that should not have author(s) included (excluded from processing by this plugin). This can be useful for example to remove the authors 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:
42+
43+
```yaml
44+
# mkdocs.yml
45+
plugins:
46+
- git-authors:
47+
exclude:
48+
- index.md
49+
- subfolder/page.md
50+
- another_page.md
51+
- folder/*
52+
```
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
"""
2+
Module to assist exclude certain files being processed by plugin.
3+
Inspired by https://github.com/apenwarr/mkdocs-exclude
4+
"""
5+
import os
6+
import fnmatch
7+
from typing import List
8+
9+
10+
def exclude(src_path: str, globs: List[str]) -> bool:
11+
"""
12+
Determine if a src_path should be excluded.
13+
Supports globs (e.g. folder/* or *.md).
14+
Credits: code inspired by / adapted from
15+
https://github.com/apenwarr/mkdocs-exclude/blob/master/mkdocs_exclude/plugin.py
16+
Args:
17+
src_path (src): Path of file
18+
globs (list): list of globs
19+
Returns:
20+
(bool): whether src_path should be excluded
21+
"""
22+
assert isinstance(src_path, str)
23+
assert isinstance(globs, list)
24+
25+
for g in globs:
26+
if fnmatch.fnmatchcase(src_path, g):
27+
return True
28+
29+
# Windows reports filenames as eg. a\\b\\c instead of a/b/c.
30+
# To make the same globs/regexes match filenames on Windows and
31+
# other OSes, let's try matching against converted filenames.
32+
# On the other hand, Unix actually allows filenames to contain
33+
# literal \\ characters (although it is rare), so we won't
34+
# always convert them. We only convert if os.sep reports
35+
# something unusual. Conversely, some future mkdocs might
36+
# report Windows filenames using / separators regardless of
37+
# os.sep, so we *always* test with / above.
38+
if os.sep != "/":
39+
src_path_fix = src_path.replace(os.sep, "/")
40+
if fnmatch.fnmatchcase(src_path_fix, g):
41+
return True
42+
43+
return False

mkdocs_git_authors_plugin/plugin.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from . import util
88
from .git.repo import Repo
99
from mkdocs_git_authors_plugin.ci import raise_ci_warnings
10+
from mkdocs_git_authors_plugin.exclude import exclude
1011

1112
logger = logging.getLogger("mkdocs.plugins")
1213

@@ -15,7 +16,8 @@ class GitAuthorsPlugin(BasePlugin):
1516
("show_contribution", config_options.Type(bool, default=False)),
1617
("show_line_count", config_options.Type(bool, default=False)),
1718
("count_empty_lines", config_options.Type(bool, default=True)),
18-
("fallback_to_empty", config_options.Type(bool, default=False))
19+
("fallback_to_empty", config_options.Type(bool, default=False)),
20+
("exclude", config_options.Type(list, default=[])),
1921
# ('sort_authors_by_name', config_options.Type(bool, default=True)),
2022
# ('sort_reverse', config_options.Type(bool, default=False))
2123
)
@@ -115,6 +117,11 @@ def on_page_content(self, html, page, config, files, **kwargs):
115117
Returns:
116118
str: HTML text of page as string
117119
"""
120+
# Exclude pages specified in config
121+
excluded_pages = self.config.get("exclude", [])
122+
if exclude(page.file.src_path, excluded_pages):
123+
logging.debug("on_page_html, Excluding page " + page.file.src_path)
124+
return html
118125

119126
list_pattern = re.compile(
120127
r"\{\{\s*git_site_authors\s*\}\}", flags=re.IGNORECASE
@@ -146,6 +153,13 @@ def on_page_markdown(self, markdown, page, config, files, **kwargs):
146153
Returns:
147154
str: Markdown source text of page as string
148155
"""
156+
157+
# Exclude pages specified in config
158+
excluded_pages = self.config.get("exclude", [])
159+
if exclude(page.file.src_path, excluded_pages):
160+
logging.debug("on_page_markdown, Excluding page " + page.file.src_path)
161+
return markdown
162+
149163
pattern_authors_summary = re.compile(
150164
r"\{\{\s*git_authors_summary\s*\}\}", flags=re.IGNORECASE
151165
)
@@ -195,6 +209,12 @@ def on_page_context(self, context, page, config, nav, **kwargs):
195209
if self._fallback:
196210
return context
197211

212+
# Exclude pages specified in config
213+
excluded_pages = self.config.get("exclude", [])
214+
if exclude(page.file.src_path, excluded_pages):
215+
logging.debug("on_page_context, Excluding page " + page.file.src_path)
216+
return context
217+
198218
path = page.file.abs_src_path
199219
page_obj = self.repo().page(path)
200220
authors = page_obj.get_authors()

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
setup(
77
name="mkdocs-git-authors-plugin",
8-
version="0.4.1",
8+
version="0.5",
99
description="Mkdocs plugin to display git authors of a page",
1010
long_description=long_description,
1111
long_description_content_type="text/markdown",
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
site_name: test gitauthors_plugin
2+
use_directory_urls: true
3+
4+
plugins:
5+
- search
6+
- git-authors:
7+
exclude:
8+
- page_with_tag.md

tests/test_basic.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,21 @@ def test_basic_working(tmp_path):
6868
assert re.search("<span class='git-page-authors", contents)
6969

7070

71+
def test_exclude_working(tmp_path):
72+
73+
result = build_docs_setup("tests/basic_setup/mkdocs_exclude.yml", tmp_path)
74+
assert result.exit_code == 0, (
75+
"'mkdocs build' command failed. Error: %s" % result.stdout
76+
)
77+
78+
page_file = tmp_path / "page_with_tag/index.html"
79+
assert page_file.exists(), "%s does not exist" % page_file
80+
81+
contents = page_file.read_text()
82+
assert not re.search("<span class='git-page-authors", contents)
83+
84+
85+
7186
def test_project_with_no_commits(tmp_path):
7287
"""
7388
Structure:

0 commit comments

Comments
 (0)