Skip to content

Commit ff2b48c

Browse files
committed
Add new option 'enabled', see #57
1 parent 8cca1db commit ff2b48c

File tree

5 files changed

+64
-2
lines changed

5 files changed

+64
-2
lines changed

docs/options.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ plugins:
1111
fallback_to_empty: false
1212
exclude:
1313
- index.md
14+
enabled: true
1415
```
1516
1617
## `show_contribution`
@@ -50,3 +51,21 @@ plugins:
5051
- another_page.md
5152
- folder/*
5253
```
54+
55+
## `enabled`
56+
57+
Default is `true`. Enables you to deactivate this plugin. A possible use case is local development where you might want faster build times and/or do not have git available. It's recommended to use this option with an environment variable together with a default fallback (introduced in `mkdocs` v1.2.1, see [docs](https://www.mkdocs.org/user-guide/configuration/#environment-variables)). Example:
58+
59+
```yaml
60+
# mkdocs.yml
61+
plugins:
62+
- git-authors:
63+
enabled: !ENV [ENABLED_GIT_AUTHORS, True]
64+
```
65+
66+
Which enables you do disable the plugin locally using:
67+
68+
```bash
69+
export ENABLED_GIT_AUTHORS=false
70+
mkdocs serve
71+
```

mkdocs_git_authors_plugin/plugin.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class GitAuthorsPlugin(BasePlugin):
1818
("count_empty_lines", config_options.Type(bool, default=True)),
1919
("fallback_to_empty", config_options.Type(bool, default=False)),
2020
("exclude", config_options.Type(list, default=[])),
21+
("enabled", config_options.Type(bool, default=True)),
2122
# ('sort_authors_by_name', config_options.Type(bool, default=True)),
2223
# ('sort_reverse', config_options.Type(bool, default=False))
2324
)
@@ -45,6 +46,9 @@ def on_config(self, config, **kwargs):
4546
Returns:
4647
(updated) configuration object
4748
"""
49+
if not self.config.get('enabled'):
50+
return config
51+
4852
try:
4953
self._repo = Repo()
5054
self._fallback = False
@@ -87,8 +91,11 @@ def on_files(self, files, config, **kwargs):
8791
Returns:
8892
global files collection
8993
"""
94+
if not self.config.get('enabled'):
95+
return
9096
if self._fallback:
9197
return
98+
9299
for file in files:
93100
path = file.abs_src_path
94101
if path.endswith(".md"):
@@ -117,6 +124,9 @@ def on_page_content(self, html, page, config, files, **kwargs):
117124
Returns:
118125
str: HTML text of page as string
119126
"""
127+
if not self.config.get('enabled'):
128+
return html
129+
120130
# Exclude pages specified in config
121131
excluded_pages = self.config.get("exclude", [])
122132
if exclude(page.file.src_path, excluded_pages):
@@ -153,7 +163,9 @@ def on_page_markdown(self, markdown, page, config, files, **kwargs):
153163
Returns:
154164
str: Markdown source text of page as string
155165
"""
156-
166+
if not self.config.get('enabled'):
167+
return markdown
168+
157169
# Exclude pages specified in config
158170
excluded_pages = self.config.get("exclude", [])
159171
if exclude(page.file.src_path, excluded_pages):
@@ -206,6 +218,8 @@ def on_page_context(self, context, page, config, nav, **kwargs):
206218
Returns:
207219
dict: template context variables
208220
"""
221+
if not self.config.get('enabled'):
222+
return context
209223
if self._fallback:
210224
return context
211225

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.5",
8+
version="0.6",
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: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
site_name: test gitauthors_plugin with material theme and git-revision-date
2+
use_directory_urls: true
3+
4+
theme:
5+
name: 'material'
6+
custom_dir: 'docs/theme'
7+
language: nl
8+
9+
plugins:
10+
- search
11+
- git-authors:
12+
count_empty_lines: false
13+
show_contribution: true
14+
show_line_count: true
15+
enabled: false
16+
- git-revision-date-localized

tests/test_basic.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,19 @@ def test_exclude_working(tmp_path):
8181
contents = page_file.read_text()
8282
assert not re.search("<span class='git-page-authors", contents)
8383

84+
def test_enabled_working(tmp_path):
85+
86+
result = build_docs_setup("tests/basic_setup/mkdocs_complete_material_disabled.yml", tmp_path)
87+
assert result.exit_code == 0, (
88+
"'mkdocs build' command failed. Error: %s" % result.stdout
89+
)
90+
91+
page_file = tmp_path / "page_with_tag/index.html"
92+
assert page_file.exists(), "%s does not exist" % page_file
93+
94+
contents = page_file.read_text()
95+
assert not re.search("<span class='git-page-authors", contents)
96+
8497

8598

8699
def test_project_with_no_commits(tmp_path):

0 commit comments

Comments
 (0)