Skip to content

Commit

Permalink
set date format through type parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
timvink committed Dec 9, 2019
1 parent 03016f2 commit 141f8c1
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 32 deletions.
43 changes: 23 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# mkdocs-git-revision-date-localized-plugin

[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).
[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.

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

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

Expand Down Expand Up @@ -41,30 +41,33 @@ In your markdown files you can use `{{ git_revision_date_localized }}`:
Last update: {{ git_revision_date_localized }}
```

## Localizated variants

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:
## Options

```django hljs
{{ git_revision_date_localized }}
{{ git_revision_date_localized_time }}
{{ git_revision_date_localized_iso }}
{{ git_revision_date_localized_iso_time }}
{{ git_revision_date_localized_timeago }}
```
### `type`

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

```bash
28 November, 2019 # type: date
28 November, 2019 13:57:28 # type: datetime
2019-11-28 # type: iso_date
2019-11-28 13:57:26 # type: iso_datetime
20 hours ago # type: timeago
```
28 November, 2019
28 November, 2019 13:57:28
2019-11-28
2019-11-28 13:57:26
20 hours ago
```

## Options

### `locale`

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`).

### Example

Setting a different configuration:

```yaml
# mkdocs.yml
plugins:
- git-revision-date-localized:
type: timeago
locale: nl
```
12 changes: 5 additions & 7 deletions mkdocs_git_revision_date_localized_plugin/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
class GitRevisionDateLocalizedPlugin(BasePlugin):
config_scheme = (
('locale', config_options.Type(string_types, default='')),
('modify_md', config_options.Type(bool, default=True))
('type', config_options.Type(string_types, default='date'))
)

def __init__(self):
Expand Down Expand Up @@ -51,20 +51,18 @@ def on_page_markdown(self, markdown, page, config, files):
path = page.file.abs_src_path,
locale = self.locale
)
revision_date = revision_dates[self.config['type']]

for variable, date in revision_dates.items():
page.meta[variable] = date
page.meta['git_revision_date_localized'] = revision_date

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

10 changes: 5 additions & 5 deletions mkdocs_git_revision_date_localized_plugin/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ def get_revision_date_for_file(self, path: str, locale: str = 'en'):

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

return revision_dates

0 comments on commit 141f8c1

Please sign in to comment.