-
-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
644 additions
and
90 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
] | ||
} | ||
) |