Skip to content

Commit 525c58c

Browse files
committed
add localization
1 parent 342150e commit 525c58c

File tree

9 files changed

+644
-90
lines changed

9 files changed

+644
-90
lines changed

.gitignore

Lines changed: 515 additions & 1 deletion
Large diffs are not rendered by default.

README.md

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

3-
[MkDocs](https://www.mkdocs.org/) plugin that displays the localized date of the last 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).
4+
5+
![example](example.png)
6+
7+
(*Example when used together with [mkdocs-material](https://github.com/squidfunk/mkdocs-material) theme*)
48

59
## Setup
610

711
Install the plugin using pip:
812

913
```bash
10-
# FIRST VERSION NOT YET PUBLISHED
1114
pip install mkdocs-git-revision-date-localized-plugin
1215
```
1316

@@ -26,7 +29,7 @@ In templates you can use `page.meta.git_revision_date_localized`:
2629

2730
```django hljs
2831
{% if page.meta.git_revision_date_localized %}
29-
<small><br><i>Updated {{ page.meta.git_revision_date_localized }}</i></small>
32+
Last update: {{ page.meta.git_revision_date_localized }}
3033
{% endif %}
3134
```
3235

@@ -35,29 +38,29 @@ In templates you can use `page.meta.git_revision_date_localized`:
3538
In your markdown files you can use `{{ git_revision_date_localized }}`:
3639

3740
```django hljs
38-
Updated {{ git_revision_date_localized_iso }}
41+
Last update: {{ git_revision_date_localized }}
3942
```
4043

41-
## Localization updates
42-
43-
There are three date formats:
44+
## Localizated variants
4445

45-
- A date string format (using [babel](https://github.com/python-babel/babel/tree/master/babel)
46-
- A ISO format *(YYYY-mm-dd)*
47-
- A time ago format (using [timeago](https://github.com/hustcc/timeago)
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:
4847

4948
```django hljs
50-
<i>Updated {{ git_revision_date_localized }}</i>
51-
<i>Updated {{ git_revision_date_localized_iso }}</i>
52-
<i>Updated {{ git_revision_date_localized_timeago }}</i>
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 }}
5354
```
5455

5556
Output:
5657

5758
```
58-
Updated 28 November, 2019
59-
Updated 2019-11-28
60-
Updated 20 hours agon
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
6164
```
6265

6366
## Options

example.png

61.8 KB
Loading
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
from os import environ
2+
3+
from mkdocs.config import config_options
4+
from mkdocs.plugins import BasePlugin
5+
from mkdocs.utils import string_types
6+
from jinja2 import Template
7+
from .util import Util
8+
from datetime import datetime
9+
10+
11+
class GitRevisionDateLocalizedPlugin(BasePlugin):
12+
config_scheme = (
13+
('locale', config_options.Type(string_types, default='')),
14+
('modify_md', config_options.Type(bool, default=True))
15+
)
16+
17+
def __init__(self):
18+
self.locale = 'en'
19+
self.util = Util()
20+
21+
def on_config(self, config):
22+
23+
# Get locale settings
24+
mkdocs_locale = config.get('locale')
25+
plugin_locale = self.config['locale']
26+
theme_locale = vars(config['theme']).get('_vars', {}).get('locale')
27+
if theme_locale is None:
28+
theme_locale = vars(config['theme']).get('_vars', {}).get('language')
29+
30+
# First prio: plugin locale
31+
if plugin_locale != '':
32+
if plugin_locale != mkdocs_locale:
33+
print(f"WARNING - plugin locale setting '{plugin_locale}' will overwrite mkdocs locale '{mkdocs_locale}'")
34+
self.locale = mkdocs_locale
35+
return
36+
37+
# Second prio: theme
38+
if theme_locale:
39+
self.locale = theme_locale
40+
# Third is mkdocs locale setting (might be add in the future)
41+
if mkdocs_locale:
42+
self.locale = mkdocs_locale
43+
44+
# Final fallback is english
45+
return
46+
47+
48+
def on_page_markdown(self, markdown, page, config, files):
49+
50+
revision_dates = self.util.get_revision_date_for_file(
51+
path = page.file.abs_src_path,
52+
locale = self.locale
53+
)
54+
55+
for variable, date in revision_dates.items():
56+
page.meta[variable] = date
57+
58+
if 'macros' in config['plugins']:
59+
keys = list(config['plugins'].keys())
60+
vals = list(config['plugins'].values())
61+
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
65+
else:
66+
print('WARNING - macros plugin must be placed AFTER the git-revision-date-localized plugin. Skipping markdown modifications')
67+
return markdown
68+
else:
69+
return Template(markdown).render(revision_dates)
70+
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from git import Git
2+
from datetime import datetime
3+
import timeago
4+
from babel.dates import format_date
5+
6+
class Util:
7+
8+
def __init__(self):
9+
self.g = Git()
10+
11+
def get_revision_date_for_file(self, path: str, locale: str = 'en'):
12+
13+
unix_timestamp = self.g.log(path, n=1, date='short', format='%at')
14+
if not unix_timestamp:
15+
revision_date = datetime.now()
16+
print('WARNING - %s has no git logs, revision date defaulting to today\'s date' % path)
17+
else:
18+
revision_date = datetime.utcfromtimestamp(int(unix_timestamp))
19+
20+
# Localized versions
21+
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)
27+
}
28+
29+
return revision_dates

mkdocs_git_revision_date_plugin/__init__.py

Whitespace-only changes.

mkdocs_git_revision_date_plugin/plugin.py

Lines changed: 0 additions & 55 deletions
This file was deleted.

mkdocs_git_revision_date_plugin/util.py

Lines changed: 0 additions & 9 deletions
This file was deleted.

setup.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,26 @@
11
from setuptools import setup, find_packages
22

33
setup(
4-
name='mkdocs-git-revision-date-plugin',
5-
version='0.1.5',
6-
description='MkDocs plugin for setting revision date from git per markdown file.',
7-
keywords='mkdocs git meta yaml frontmatter',
8-
url='https://github.com/zhaoterryy/mkdocs-git-revision-date-plugin/',
9-
author='Terry Zhao',
10-
author_email='zhao.terryy@gmail.com',
4+
name='mkdocs-git-revision-date-localized-plugin',
5+
version='0.2',
6+
description='Mkdocs plugin that enables displaying the localized date of the last git modification of a markdown file.',
7+
keywords='mkdocs git date timeago babel plugin',
8+
url='https://github.com/timvink/mkdocs-git-revision-date-localized-plugin/',
9+
author='Tim Vink',
10+
author_email='vinktim@gmail.com',
1111
license='MIT',
1212
python_requires='>=3.4',
1313
install_requires=[
1414
'mkdocs>=0.17',
1515
'GitPython',
16-
'jinja2'
16+
'jinja2',
17+
'babel>=2.7.0',
18+
'timeago>=1.0.10'
1719
],
1820
packages=find_packages(),
1921
entry_points={
2022
'mkdocs.plugins': [
21-
'git-revision-date = mkdocs_git_revision_date_plugin.plugin:GitRevisionDatePlugin'
23+
'git-revision-date-localized = mkdocs_git_revision_date_localized_plugin.plugin:GitRevisionDateLocalizedPlugin'
2224
]
2325
}
2426
)

0 commit comments

Comments
 (0)