Skip to content

Commit 20d2435

Browse files
authored
Merge pull request #85 from timvink/82-custom-date-format
Add new 'custom' type, closes #82
2 parents 1efdd24 + df6ab10 commit 20d2435

File tree

6 files changed

+38
-10
lines changed

6 files changed

+38
-10
lines changed

docs/available-variables.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,13 @@ To allow for more flexibility when overriding a theme there are also variables f
3232
- `page.meta.git_revision_date_localized_raw_iso_date`
3333
- `page.meta.git_revision_date_localized_raw_iso_datetime`
3434
- `page.meta.git_revision_date_localized_raw_timeago`
35+
- `page.meta.git_revision_date_localized_raw_custom`
3536
- `page.meta.git_site_revision_date_localized_raw_datetime`
3637
- `page.meta.git_site_revision_date_localized_raw_iso_date`
3738
- `page.meta.git_site_revision_date_localized_raw_date`
3839
- `page.meta.git_site_revision_date_localized_raw_iso_datetime`
3940
- `page.meta.git_site_revision_date_localized_raw_timeago`
41+
- `page.meta.git_site_revision_date_localized_raw_custom`
4042

4143
And if you've enabled creation date in the config:
4244

@@ -45,6 +47,7 @@ And if you've enabled creation date in the config:
4547
- `page.meta.git_creation_date_localized_raw_iso_date`
4648
- `page.meta.git_creation_date_localized_raw_iso_datetime`
4749
- `page.meta.git_creation_date_localized_raw_timeago`
50+
- `page.meta.git_creation_date_localized_raw_custom`
4851

4952
!!! warning "timeago.js dependency"
5053

docs/options.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ You can customize the plugin by setting options in `mkdocs.yml`. For example:
88
plugins:
99
- git-revision-date-localized:
1010
type: timeago
11+
custom_format: "%d. %B %Y"
1112
timezone: Europe/Amsterdam
1213
locale: en
1314
fallback_to_build_date: false
@@ -19,16 +20,21 @@ You can customize the plugin by setting options in `mkdocs.yml`. For example:
1920
2021
## `type`
2122

22-
Default is `date`. The format of the date to be displayed. Valid values are `date`, `datetime`, `iso_date`, `iso_datetime` and `timeago`. Example outputs:
23+
Default is `date`. The format of the date to be displayed. Valid values are `date`, `datetime`, `iso_date`, `iso_datetime`, `timeago` and `custom`. Example outputs:
2324

2425
```yaml
2526
November 28, 2019 # type: date (default)
2627
November 28, 2019 13:57:28 # type: datetime
2728
2019-11-28 # type: iso_date
2829
2019-11-28 13:57:26 # type: iso_datetime
2930
20 hours ago # type: timeago
31+
28. November 2019 # type: custom
3032
```
3133

34+
## `custom_format`
35+
36+
Default is `%d. %B %Y`. The date format used when `type: custom`. Passed to python's `strftime`, see the [cheatsheat](https://strftime.org/) for details.
37+
3238
## `timezone`
3339

3440
Default is `UTC`. Specify a time zone database name ([reference](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones)). This option is especially relevant when using `type: datetime` and `type: iso_datetime`. Note that when using [timeago](http://timeago.yarp.com/) (with `type: timeago`) any difference in time zones between server and client will be handled automatically.

mkdocs_git_revision_date_localized_plugin/plugin.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class GitRevisionDateLocalizedPlugin(BasePlugin):
3737
("fallback_to_build_date", config_options.Type(bool, default=False)),
3838
("locale", config_options.Type(str, default=None)),
3939
("type", config_options.Type(str, default="date")),
40+
("custom_format", config_options.Type(str, default="%d. %B %Y")),
4041
("timezone", config_options.Type(str, default="UTC")),
4142
("exclude", config_options.Type(list, default=[])),
4243
("enable_creation_date", config_options.Type(bool, default=False)),
@@ -61,7 +62,7 @@ def on_config(self, config: config_options.Config, **kwargs) -> Dict[str, Any]:
6162
if not self.config.get('enabled'):
6263
return config
6364

64-
assert self.config['type'] in ["date","datetime","iso_date","iso_datetime","timeago"]
65+
assert self.config['type'] in ["date","datetime","iso_date","iso_datetime","timeago","custom"]
6566

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

@@ -195,7 +196,6 @@ def on_page_markdown(
195196
assert len(locale) == 2, "locale must be a 2 letter code, see https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes"
196197

197198

198-
199199
# Retrieve git commit timestamp
200200
last_revision_timestamp = self.util.get_git_commit_timestamp(
201201
path=page.file.abs_src_path,

mkdocs_git_revision_date_localized_plugin/util.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def _get_repo(self, path: str) -> Git:
4646

4747
@staticmethod
4848
def _date_formats(
49-
unix_timestamp: float, locale: str = "en", time_zone: str = "UTC"
49+
unix_timestamp: float, locale: str = "en", time_zone: str = "UTC", custom_format: str = "%d. %B %Y"
5050
) -> Dict[str, Any]:
5151
"""
5252
Calculate different date formats / types.
@@ -55,6 +55,7 @@ def _date_formats(
5555
unix_timestamp (float): A timestamp in seconds since 1970.
5656
locale (str): Locale code of language to use. Defaults to 'en'.
5757
time_zone (str): Timezone database name (https://en.wikipedia.org/wiki/List_of_tz_database_time_zones).
58+
custom_format (str): strftime format specifier for the 'custom' type
5859
5960
Returns:
6061
dict: Different date formats.
@@ -77,8 +78,8 @@ def _date_formats(
7778
),
7879
"iso_date": loc_revision_date.strftime("%Y-%m-%d"),
7980
"iso_datetime": loc_revision_date.strftime("%Y-%m-%d %H:%M:%S"),
80-
"timeago": '<span class="timeago" datetime="%s" locale="%s"></span>'
81-
% (loc_revision_date.isoformat(), locale),
81+
"timeago": '<span class="timeago" datetime="%s" locale="%s"></span>' % (loc_revision_date.isoformat(), locale),
82+
"custom": loc_revision_date.strftime(custom_format),
8283
}
8384

8485
def get_git_commit_timestamp(
@@ -195,7 +196,8 @@ def get_date_formats_for_timestamp(
195196
date_formats = self._date_formats(
196197
unix_timestamp=commit_timestamp,
197198
time_zone=self.config.get("timezone"),
198-
locale=locale
199+
locale=locale,
200+
custom_format=self.config.get('custom_format')
199201
)
200202
if add_spans:
201203
date_formats = self.add_spans(date_formats)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
site_name: test gitrevisiondatelocalized_plugin
2+
use_directory_urls: true
3+
4+
plugins:
5+
- search
6+
- git-revision-date-localized:
7+
type: custom
8+
custom_format: "%Y"

tests/test_builds.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,7 @@ def validate_mkdocs_file(temp_path: str, mkdocs_yml_file: str):
300300
'basic_project/mkdocs_theme_timeago_instant.yml',
301301
'basic_project/mkdocs_exclude.yml',
302302
'basic_project/mkdocs_meta.yml',
303+
'basic_project/mkdocs_custom_type.yml',
303304
# 'i18n/mkdocs.yml'
304305
]
305306

@@ -322,6 +323,7 @@ def test_date_formats():
322323
"iso_date": "2020-02-22",
323324
"iso_datetime": "2020-02-22 18:52:09",
324325
"timeago": '<span class="timeago" datetime="2020-02-22T18:52:09+00:00" locale="en"></span>',
326+
"custom": '22. February 2020',
325327
}
326328

327329

@@ -363,17 +365,22 @@ def test_tags_are_replaced(tmp_path, mkdocs_file):
363365
# Assert {{ git_revision_date_localized }} is replaced
364366
date_formats_revision_date = Util()._date_formats(1642911026,
365367
locale=plugin_config.get("locale"),
366-
time_zone=plugin_config.get("timezone"))
368+
time_zone=plugin_config.get("timezone"),
369+
custom_format=plugin_config.get("custom_format")
370+
)
367371
for k, v in date_formats_revision_date.items():
368372
assert v is not None
373+
369374
date = date_formats_revision_date.get(plugin_config.get('type'))
370375
assert re.search(rf"{date}\<\/span.+", contents)
371376

372377
# The last site revision was set in setup_commit_history to 1643911026 (Thu Feb 03 2022 17:57:06 GMT+0000)
373378
# Assert {{ git_site_revision_date_localized }} is replaced
374379
date_formats_revision_date = Util()._date_formats(1643911026,
375380
locale=plugin_config.get("locale"),
376-
time_zone=plugin_config.get("timezone"))
381+
time_zone=plugin_config.get("timezone"),
382+
custom_format=plugin_config.get("custom_format")
383+
)
377384
for k, v in date_formats_revision_date.items():
378385
assert v is not None
379386
date = date_formats_revision_date.get(plugin_config.get('type'))
@@ -384,7 +391,9 @@ def test_tags_are_replaced(tmp_path, mkdocs_file):
384391
# The creation of page_with_tag.md was set in setup_commit_history to 1500854705 ( Mon Jul 24 2017 00:05:05 GMT+0000 )
385392
date_formats_revision_date = Util()._date_formats(1500854705,
386393
locale=plugin_config.get("locale"),
387-
time_zone=plugin_config.get("timezone"))
394+
time_zone=plugin_config.get("timezone"),
395+
custom_format=plugin_config.get("custom_format")
396+
)
388397
for k, v in date_formats_revision_date.items():
389398
assert v is not None
390399
date = date_formats_revision_date.get(plugin_config.get('type'))

0 commit comments

Comments
 (0)