Skip to content

Commit 1037a8d

Browse files
committed
Add new option 'enabled', see #58
1 parent e0d80ac commit 1037a8d

File tree

4 files changed

+67
-9
lines changed

4 files changed

+67
-9
lines changed

docs/options.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ plugins:
1212
enable_creation_date: true
1313
exclude:
1414
- index.md
15+
enabled: true
1516
```
1617
1718
## `type`
@@ -71,3 +72,21 @@ plugins:
7172
- another_page.md
7273
- folder/*
7374
```
75+
76+
## `enabled`
77+
78+
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:
79+
80+
```yaml
81+
# mkdocs.yml
82+
plugins:
83+
- git-revision-date-localized:
84+
enabled: !ENV [ENABLED_GIT_REVISION_DATE, True]
85+
```
86+
87+
Which enables you do disable the plugin locally using:
88+
89+
```bash
90+
export ENABLED_GIT_REVISION_DATE=false
91+
mkdocs serve
92+
```

mkdocs_git_revision_date_localized_plugin/plugin.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class GitRevisionDateLocalizedPlugin(BasePlugin):
3838
("timezone", config_options.Type(str, default="UTC")),
3939
("exclude", config_options.Type(list, default=[])),
4040
("enable_creation_date", config_options.Type(bool, default=False)),
41+
("enabled", config_options.Type(bool, default=True)),
4142
)
4243

4344
def on_config(self, config: config_options.Config, **kwargs) -> Dict[str, Any]:
@@ -55,6 +56,9 @@ def on_config(self, config: config_options.Config, **kwargs) -> Dict[str, Any]:
5556
Returns:
5657
dict: global configuration object
5758
"""
59+
if not self.config.get('enabled'):
60+
return config
61+
5862
self.util = Util(config=self.config)
5963

6064
# Get locale settings - might be added in future mkdocs versions
@@ -142,6 +146,9 @@ def on_page_markdown(
142146
Returns:
143147
str: Markdown source text of page as string
144148
"""
149+
if not self.config.get('enabled'):
150+
return markdown
151+
145152
# Exclude pages specified in config
146153
excluded_pages = self.config.get("exclude", [])
147154
if exclude(page.file.src_path, excluded_pages):
@@ -206,7 +213,7 @@ def on_post_build(self, config: Dict[str, Any], **kwargs) -> None:
206213
Adds the timeago assets to the build.
207214
"""
208215
# Add timeago files:
209-
if self.config.get("type") == "timeago":
216+
if self.config.get("type") == "timeago" and self.config.get('enabled'):
210217
files = [
211218
"js/timeago.min.js",
212219
"js/timeago_mkdocs_material.js",
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
site_name: test gitrevisiondatelocalized_plugin
2+
use_directory_urls: true
3+
4+
theme:
5+
name: 'material'
6+
locale: nl
7+
8+
plugins:
9+
- search
10+
- git-revision-date-localized:
11+
enabled: !ENV [ENABLED_GIT_REVISION_DATE, False]

tests/test_builds.py

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,15 @@ def get_plugin_config_from_mkdocs(mkdocs_path) -> dict:
5151
cfg = plugin_loaded.on_config(cfg_mkdocs)
5252
logging.info("Fixture configuration loaded: " + str(cfg))
5353

54-
assert (
55-
plugin_loaded.config.get("locale") is not None
56-
), "Locale should never be None after plugin is loaded"
57-
58-
logging.info(
59-
"Locale '%s' determined from %s"
60-
% (plugin_loaded.config.get("locale"), mkdocs_path)
61-
)
54+
if plugin_loaded.config.get("enabled"):
55+
assert (
56+
plugin_loaded.config.get("locale") is not None
57+
), "Locale should never be None after plugin is loaded"
58+
59+
logging.info(
60+
"Locale '%s' determined from %s"
61+
% (plugin_loaded.config.get("locale"), mkdocs_path)
62+
)
6263
return plugin_loaded.config
6364

6465

@@ -196,6 +197,9 @@ def validate_build(testproject_path, plugin_config: dict = {}):
196197

197198
# Make sure with markdown tag has valid
198199
# git revision date tag
200+
if not plugin_config.get('enabled'):
201+
return
202+
199203
page_with_tag = testproject_path / "site/page_with_tag/index.html"
200204
contents = page_with_tag.read_text(encoding="utf8")
201205
assert re.search(r"renders as\:\s[<span>|\w].+", contents)
@@ -346,6 +350,23 @@ def test_material_theme_locale(tmp_path):
346350
contents = index_file.read_text(encoding="utf8")
347351
assert re.search(r"Last update\:\s[<span class].+", contents)
348352

353+
def test_material_theme_locale_disabled(tmp_path):
354+
"""
355+
When using mkdocs-material theme, test correct working
356+
"""
357+
# theme set to 'material' with 'locale' set to 'de'
358+
testproject_path = validate_mkdocs_file(
359+
tmp_path, "tests/fixtures/basic_project/mkdocs_theme_locale_disabled.yml"
360+
)
361+
362+
# In mkdocs-material, a 'last update' should appear
363+
# in english instead of German because you should use 'language' and not locale.
364+
# The date will be in german though
365+
index_file = testproject_path / "site/index.html"
366+
contents = index_file.read_text(encoding="utf8")
367+
assert re.search(r"Last update\:\s[<span class].+", contents) is None
368+
369+
349370

350371
def test_material_theme_no_locale(tmp_path):
351372
"""

0 commit comments

Comments
 (0)