Skip to content

Commit 6150fad

Browse files
authored
Merge pull request #110 from timvink/108-strict-mode
Add new 'strict' option, closes #108
2 parents 7ddc74c + 7d89e01 commit 6150fad

File tree

5 files changed

+33
-7
lines changed

5 files changed

+33
-7
lines changed

docs/options.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ You can customize the plugin by setting options in `mkdocs.yml`. For example:
1616
exclude:
1717
- index.md
1818
enabled: true
19+
strict: true
1920
```
2021
2122
## `type`
@@ -131,3 +132,18 @@ Which enables you to disable the plugin locally using:
131132
export ENABLED_GIT_REVISION_DATE=false
132133
mkdocs serve
133134
```
135+
136+
## `strict`
137+
138+
Default is `true`. When enabled, the logs will show warnings when something is wrong but a fallback has been used. When disabled, the logger will use the INFO level instead.
139+
140+
- If you want to raise an error when a warning is logged, use [mkdocs strict mode](https://www.mkdocs.org/user-guide/configuration/#strict) (with `mkdocs build --strict`).
141+
- If you are already using [mkdocs strict mode](https://www.mkdocs.org/user-guide/configuration/#strict), but do not care about these warnings, you can set `strict: false` to ensure no errors are raised.
142+
143+
=== ":octicons-file-code-16: mkdocs.yml"
144+
145+
```yaml
146+
plugins:
147+
- git-revision-date-localized:
148+
strict: true
149+
```

mkdocs_git_revision_date_localized_plugin/plugin.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class GitRevisionDateLocalizedPlugin(BasePlugin):
4343
("exclude", config_options.Type(list, default=[])),
4444
("enable_creation_date", config_options.Type(bool, default=False)),
4545
("enabled", config_options.Type(bool, default=True)),
46+
("strict", config_options.Type(bool, default=True)),
4647
)
4748

4849
def on_config(self, config: config_options.Config, **kwargs) -> Dict[str, Any]:

mkdocs_git_revision_date_localized_plugin/util.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,14 @@ def get_git_commit_timestamp(
101101
"""
102102
commit_timestamp = ""
103103

104+
# Determine the logging level
105+
# Only log warnings when plugin is set to strict.
106+
# That way, users turn those into errors using mkdocs build --strict
107+
if self.config.get('strict'):
108+
log = logger.warning
109+
else:
110+
log = logger.info
111+
104112
# perform git log operation
105113
try:
106114
# Retrieve author date in UNIX format (%at)
@@ -126,20 +134,20 @@ def get_git_commit_timestamp(
126134
)
127135
except (InvalidGitRepositoryError, NoSuchPathError) as err:
128136
if self.config.get('fallback_to_build_date'):
129-
logger.warning(
137+
log(
130138
"[git-revision-date-localized-plugin] Unable to find a git directory and/or git is not installed."
131139
" Option 'fallback_to_build_date' set to 'true': Falling back to build date"
132140
)
133141
commit_timestamp = time.time()
134142
else:
135-
logger.error(
143+
log(
136144
"[git-revision-date-localized-plugin] Unable to find a git directory and/or git is not installed."
137145
" To ignore this error, set option 'fallback_to_build_date: true'"
138146
)
139147
raise err
140148
except GitCommandError as err:
141149
if self.config.get('fallback_to_build_date'):
142-
logger.warning(
150+
log(
143151
"[git-revision-date-localized-plugin] Unable to read git logs of '%s'. Is git log readable?"
144152
" Option 'fallback_to_build_date' set to 'true': Falling back to build date"
145153
% path
@@ -154,13 +162,13 @@ def get_git_commit_timestamp(
154162
raise err
155163
except GitCommandNotFound as err:
156164
if self.config.get('fallback_to_build_date'):
157-
logger.warning(
165+
log(
158166
"[git-revision-date-localized-plugin] Unable to perform command: 'git log'. Is git installed?"
159167
" Option 'fallback_to_build_date' set to 'true': Falling back to build date"
160168
)
161169
commit_timestamp = time.time()
162170
else:
163-
logger.error(
171+
log(
164172
"[git-revision-date-localized-plugin] Unable to perform command 'git log'. Is git installed?"
165173
" To ignore this error, set option 'fallback_to_build_date: true'"
166174
)
@@ -169,7 +177,7 @@ def get_git_commit_timestamp(
169177
# create timestamp
170178
if commit_timestamp == "":
171179
commit_timestamp = time.time()
172-
logger.warning(
180+
log(
173181
"[git-revision-date-localized-plugin] '%s' has no git logs, using current timestamp"
174182
% path
175183
)

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
mkdocs>=1.0
22
GitPython
33
babel>=2.7.0
4+
pytz

setup.py

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

1313
setup(
1414
name="mkdocs-git-revision-date-localized-plugin",
15-
version="1.1.0",
15+
version="1.2.0",
1616
description="Mkdocs plugin that enables displaying the localized date of the last git modification of a markdown file.",
1717
long_description=LONG_DESCRIPTION,
1818
long_description_content_type="text/markdown",

0 commit comments

Comments
 (0)