Skip to content

Commit

Permalink
add localization
Browse files Browse the repository at this point in the history
  • Loading branch information
timvink committed Dec 9, 2019
1 parent 342150e commit 525c58c
Show file tree
Hide file tree
Showing 9 changed files with 644 additions and 90 deletions.
516 changes: 515 additions & 1 deletion .gitignore

Large diffs are not rendered by default.

35 changes: 19 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
# mkdocs-git-revision-date-localized-plugin

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

![example](example.png)

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

## Setup

Install the plugin using pip:

```bash
# FIRST VERSION NOT YET PUBLISHED
pip install mkdocs-git-revision-date-localized-plugin
```

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

```django hljs
{% if page.meta.git_revision_date_localized %}
<small><br><i>Updated {{ page.meta.git_revision_date_localized }}</i></small>
Last update: {{ page.meta.git_revision_date_localized }}
{% endif %}
```

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

```django hljs
Updated {{ git_revision_date_localized_iso }}
Last update: {{ git_revision_date_localized }}
```

## Localization updates

There are three date formats:
## Localizated variants

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

```django hljs
<i>Updated {{ git_revision_date_localized }}</i>
<i>Updated {{ git_revision_date_localized_iso }}</i>
<i>Updated {{ git_revision_date_localized_timeago }}</i>
{{ 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 }}
```

Output:

```
Updated 28 November, 2019
Updated 2019-11-28
Updated 20 hours agon
28 November, 2019
28 November, 2019 13:57:28
2019-11-28
2019-11-28 13:57:26
20 hours ago
```

## Options
Expand Down
Binary file added example.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
70 changes: 70 additions & 0 deletions mkdocs_git_revision_date_localized_plugin/plugin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
from os import environ

from mkdocs.config import config_options
from mkdocs.plugins import BasePlugin
from mkdocs.utils import string_types
from jinja2 import Template
from .util import Util
from datetime import datetime


class GitRevisionDateLocalizedPlugin(BasePlugin):
config_scheme = (
('locale', config_options.Type(string_types, default='')),
('modify_md', config_options.Type(bool, default=True))
)

def __init__(self):
self.locale = 'en'
self.util = Util()

def on_config(self, config):

# Get locale settings
mkdocs_locale = config.get('locale')
plugin_locale = self.config['locale']
theme_locale = vars(config['theme']).get('_vars', {}).get('locale')
if theme_locale is None:
theme_locale = vars(config['theme']).get('_vars', {}).get('language')

# First prio: plugin locale
if plugin_locale != '':
if plugin_locale != mkdocs_locale:
print(f"WARNING - plugin locale setting '{plugin_locale}' will overwrite mkdocs locale '{mkdocs_locale}'")
self.locale = mkdocs_locale
return

# Second prio: theme
if theme_locale:
self.locale = theme_locale
# Third is mkdocs locale setting (might be add in the future)
if mkdocs_locale:
self.locale = mkdocs_locale

# Final fallback is english
return


def on_page_markdown(self, markdown, page, config, files):

revision_dates = self.util.get_revision_date_for_file(
path = page.file.abs_src_path,
locale = self.locale
)

for variable, date in revision_dates.items():
page.meta[variable] = 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
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)

29 changes: 29 additions & 0 deletions mkdocs_git_revision_date_localized_plugin/util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from git import Git
from datetime import datetime
import timeago
from babel.dates import format_date

class Util:

def __init__(self):
self.g = Git()

def get_revision_date_for_file(self, path: str, locale: str = 'en'):

unix_timestamp = self.g.log(path, n=1, date='short', format='%at')
if not unix_timestamp:
revision_date = datetime.now()
print('WARNING - %s has no git logs, revision date defaulting to today\'s date' % path)
else:
revision_date = datetime.utcfromtimestamp(int(unix_timestamp))

# 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)
}

return revision_dates
Empty file.
55 changes: 0 additions & 55 deletions mkdocs_git_revision_date_plugin/plugin.py

This file was deleted.

9 changes: 0 additions & 9 deletions mkdocs_git_revision_date_plugin/util.py

This file was deleted.

20 changes: 11 additions & 9 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
from setuptools import setup, find_packages

setup(
name='mkdocs-git-revision-date-plugin',
version='0.1.5',
description='MkDocs plugin for setting revision date from git per markdown file.',
keywords='mkdocs git meta yaml frontmatter',
url='https://github.com/zhaoterryy/mkdocs-git-revision-date-plugin/',
author='Terry Zhao',
author_email='zhao.terryy@gmail.com',
name='mkdocs-git-revision-date-localized-plugin',
version='0.2',
description='Mkdocs plugin that enables displaying the localized date of the last git modification of a markdown file.',
keywords='mkdocs git date timeago babel plugin',
url='https://github.com/timvink/mkdocs-git-revision-date-localized-plugin/',
author='Tim Vink',
author_email='vinktim@gmail.com',
license='MIT',
python_requires='>=3.4',
install_requires=[
'mkdocs>=0.17',
'GitPython',
'jinja2'
'jinja2',
'babel>=2.7.0',
'timeago>=1.0.10'
],
packages=find_packages(),
entry_points={
'mkdocs.plugins': [
'git-revision-date = mkdocs_git_revision_date_plugin.plugin:GitRevisionDatePlugin'
'git-revision-date-localized = mkdocs_git_revision_date_localized_plugin.plugin:GitRevisionDateLocalizedPlugin'
]
}
)

0 comments on commit 525c58c

Please sign in to comment.