Skip to content

Commit 141f8c1

Browse files
committed
set date format through type parameter
1 parent 03016f2 commit 141f8c1

File tree

3 files changed

+33
-32
lines changed

3 files changed

+33
-32
lines changed

README.md

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# mkdocs-git-revision-date-localized-plugin
22

3-
[MkDocs](https://www.mkdocs.org/) plugin that enables displaying the localized date of the last git modification of a markdown file. Forked from [mkdocs-git-revision-date-plugin](https://github.com/zhaoterryy/mkdocs-git-revision-date-plugin).
3+
[MkDocs](https://www.mkdocs.org/) plugin that enables displaying the localized date of the last git modification of a markdown file. Forked from [mkdocs-git-revision-date-plugin](https://github.com/zhaoterryy/mkdocs-git-revision-date-plugin). The plugin uses [babel](https://github.com/python-babel/babel/tree/master/babel) and [timeago](https://github.com/hustcc/timeago) to provide different localized date formats.
44

5-
![example](example.png)
5+
![example](https://github.com/timvink/mkdocs-git-revision-date-localized-plugin/raw/master/example.png)
66

77
(*Example when used together with [mkdocs-material](https://github.com/squidfunk/mkdocs-material) theme*)
88

@@ -41,30 +41,33 @@ In your markdown files you can use `{{ git_revision_date_localized }}`:
4141
Last update: {{ git_revision_date_localized }}
4242
```
4343

44-
## Localizated variants
4544

46-
The plugin uses [babel](https://github.com/python-babel/babel/tree/master/babel) and [timeago](https://github.com/hustcc/timeago) to provide different date formats:
45+
## Options
4746

48-
```django hljs
49-
{{ git_revision_date_localized }}
50-
{{ git_revision_date_localized_time }}
51-
{{ git_revision_date_localized_iso }}
52-
{{ git_revision_date_localized_iso_time }}
53-
{{ git_revision_date_localized_timeago }}
54-
```
47+
### `type`
5548

56-
Output:
49+
Set this option to one of `date`, `datetime`, `iso_date`, `iso_datetime` or `timeago`. Default is `date`. Example outputs:
5750

51+
```bash
52+
28 November, 2019 # type: date
53+
28 November, 2019 13:57:28 # type: datetime
54+
2019-11-28 # type: iso_date
55+
2019-11-28 13:57:26 # type: iso_datetime
56+
20 hours ago # type: timeago
5857
```
59-
28 November, 2019
60-
28 November, 2019 13:57:28
61-
2019-11-28
62-
2019-11-28 13:57:26
63-
20 hours ago
64-
```
65-
66-
## Options
6758

6859
### `locale`
6960

7061
Set this option to a two letter [ISO639](https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes) language code to use a another language. This will overwrite any locale setting in `mkdocs` or your theme. If no locale is set fallback is English (`en`).
62+
63+
### Example
64+
65+
Setting a different configuration:
66+
67+
```yaml
68+
# mkdocs.yml
69+
plugins:
70+
- git-revision-date-localized:
71+
type: timeago
72+
locale: nl
73+
```

mkdocs_git_revision_date_localized_plugin/plugin.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
class GitRevisionDateLocalizedPlugin(BasePlugin):
1212
config_scheme = (
1313
('locale', config_options.Type(string_types, default='')),
14-
('modify_md', config_options.Type(bool, default=True))
14+
('type', config_options.Type(string_types, default='date'))
1515
)
1616

1717
def __init__(self):
@@ -51,20 +51,18 @@ def on_page_markdown(self, markdown, page, config, files):
5151
path = page.file.abs_src_path,
5252
locale = self.locale
5353
)
54+
revision_date = revision_dates[self.config['type']]
5455

55-
for variable, date in revision_dates.items():
56-
page.meta[variable] = date
56+
page.meta['git_revision_date_localized'] = revision_date
5757

5858
if 'macros' in config['plugins']:
5959
keys = list(config['plugins'].keys())
6060
vals = list(config['plugins'].values())
6161
if keys.index('macros') > vals.index(self):
62-
for variable, date in revision_dates.items():
63-
markdown = '{{% set' + variable + f" = '{date}' " + ' %}}' + markdown
64-
return markdown
62+
return '{{% set git_revision_date = \'{}\' %}}\n'.format(revision_date) + markdown
6563
else:
6664
print('WARNING - macros plugin must be placed AFTER the git-revision-date-localized plugin. Skipping markdown modifications')
6765
return markdown
6866
else:
69-
return Template(markdown).render(revision_dates)
67+
return Template(markdown).render({'git_revision_date_localized': revision_date})
7068

mkdocs_git_revision_date_localized_plugin/util.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ def get_revision_date_for_file(self, path: str, locale: str = 'en'):
1919

2020
# Localized versions
2121
revision_dates = {
22-
'git_revision_date_localized' : format_date(revision_date, format="long", locale=locale),
23-
'git_revision_date_localized_time' : format_date(revision_date, format="long", locale=locale) + ' ' +revision_date.strftime("%H:%M:%S"),
24-
'git_revision_date_localized_iso' : revision_date.strftime("%Y-%m-%d"),
25-
'git_revision_date_localized_iso_time' : revision_date.strftime("%Y-%m-%d %H:%M:%S"),
26-
'git_revision_date_localized_timeago' : timeago.format(revision_date, locale = locale)
22+
'date' : format_date(revision_date, format="long", locale=locale),
23+
'datetime' : format_date(revision_date, format="long", locale=locale) + ' ' +revision_date.strftime("%H:%M:%S"),
24+
'iso_date' : revision_date.strftime("%Y-%m-%d"),
25+
'iso_datetime' : revision_date.strftime("%Y-%m-%d %H:%M:%S"),
26+
'timeago' : timeago.format(revision_date, locale = locale)
2727
}
2828

2929
return revision_dates

0 commit comments

Comments
 (0)