Skip to content

Commit

Permalink
Add new option 'enabled', see #58
Browse files Browse the repository at this point in the history
  • Loading branch information
timvink committed Sep 11, 2021
1 parent e0d80ac commit 1037a8d
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 9 deletions.
19 changes: 19 additions & 0 deletions docs/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ plugins:
enable_creation_date: true
exclude:
- index.md
enabled: true
```
## `type`
Expand Down Expand Up @@ -71,3 +72,21 @@ plugins:
- another_page.md
- folder/*
```
## `enabled`

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:

```yaml
# mkdocs.yml
plugins:
- git-revision-date-localized:
enabled: !ENV [ENABLED_GIT_REVISION_DATE, True]
```

Which enables you do disable the plugin locally using:

```bash
export ENABLED_GIT_REVISION_DATE=false
mkdocs serve
```
9 changes: 8 additions & 1 deletion mkdocs_git_revision_date_localized_plugin/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class GitRevisionDateLocalizedPlugin(BasePlugin):
("timezone", config_options.Type(str, default="UTC")),
("exclude", config_options.Type(list, default=[])),
("enable_creation_date", config_options.Type(bool, default=False)),
("enabled", config_options.Type(bool, default=True)),
)

def on_config(self, config: config_options.Config, **kwargs) -> Dict[str, Any]:
Expand All @@ -55,6 +56,9 @@ def on_config(self, config: config_options.Config, **kwargs) -> Dict[str, Any]:
Returns:
dict: global configuration object
"""
if not self.config.get('enabled'):
return config

self.util = Util(config=self.config)

# Get locale settings - might be added in future mkdocs versions
Expand Down Expand Up @@ -142,6 +146,9 @@ def on_page_markdown(
Returns:
str: Markdown source text of page as string
"""
if not self.config.get('enabled'):
return markdown

# Exclude pages specified in config
excluded_pages = self.config.get("exclude", [])
if exclude(page.file.src_path, excluded_pages):
Expand Down Expand Up @@ -206,7 +213,7 @@ def on_post_build(self, config: Dict[str, Any], **kwargs) -> None:
Adds the timeago assets to the build.
"""
# Add timeago files:
if self.config.get("type") == "timeago":
if self.config.get("type") == "timeago" and self.config.get('enabled'):
files = [
"js/timeago.min.js",
"js/timeago_mkdocs_material.js",
Expand Down
11 changes: 11 additions & 0 deletions tests/fixtures/basic_project/mkdocs_theme_locale_disabled.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
site_name: test gitrevisiondatelocalized_plugin
use_directory_urls: true

theme:
name: 'material'
locale: nl

plugins:
- search
- git-revision-date-localized:
enabled: !ENV [ENABLED_GIT_REVISION_DATE, False]
37 changes: 29 additions & 8 deletions tests/test_builds.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,15 @@ def get_plugin_config_from_mkdocs(mkdocs_path) -> dict:
cfg = plugin_loaded.on_config(cfg_mkdocs)
logging.info("Fixture configuration loaded: " + str(cfg))

assert (
plugin_loaded.config.get("locale") is not None
), "Locale should never be None after plugin is loaded"

logging.info(
"Locale '%s' determined from %s"
% (plugin_loaded.config.get("locale"), mkdocs_path)
)
if plugin_loaded.config.get("enabled"):
assert (
plugin_loaded.config.get("locale") is not None
), "Locale should never be None after plugin is loaded"

logging.info(
"Locale '%s' determined from %s"
% (plugin_loaded.config.get("locale"), mkdocs_path)
)
return plugin_loaded.config


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

# Make sure with markdown tag has valid
# git revision date tag
if not plugin_config.get('enabled'):
return

page_with_tag = testproject_path / "site/page_with_tag/index.html"
contents = page_with_tag.read_text(encoding="utf8")
assert re.search(r"renders as\:\s[<span>|\w].+", contents)
Expand Down Expand Up @@ -346,6 +350,23 @@ def test_material_theme_locale(tmp_path):
contents = index_file.read_text(encoding="utf8")
assert re.search(r"Last update\:\s[<span class].+", contents)

def test_material_theme_locale_disabled(tmp_path):
"""
When using mkdocs-material theme, test correct working
"""
# theme set to 'material' with 'locale' set to 'de'
testproject_path = validate_mkdocs_file(
tmp_path, "tests/fixtures/basic_project/mkdocs_theme_locale_disabled.yml"
)

# In mkdocs-material, a 'last update' should appear
# in english instead of German because you should use 'language' and not locale.
# The date will be in german though
index_file = testproject_path / "site/index.html"
contents = index_file.read_text(encoding="utf8")
assert re.search(r"Last update\:\s[<span class].+", contents) is None



def test_material_theme_no_locale(tmp_path):
"""
Expand Down

0 comments on commit 1037a8d

Please sign in to comment.