Skip to content

Use locale during date formatting #22

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
Mar 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 58 additions & 18 deletions mkdocs_git_revision_date_localized_plugin/plugin.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# standard lib
import logging
import re

# 3rd party
Expand All @@ -7,18 +8,17 @@
from mkdocs.structure.nav import Page

# package modules
from .util import Util
from mkdocs_git_revision_date_localized_plugin.util import Util


class GitRevisionDateLocalizedPlugin(BasePlugin):
config_scheme = (
("fallback_to_build_date", config_options.Type(bool, default=False)),
("locale", config_options.Type(str, default="")),
("locale", config_options.Type(str, default=None)),
("type", config_options.Type(str, default="date")),
)

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

def on_config(self, config: config_options.Config) -> dict:
Expand All @@ -37,24 +37,56 @@ def on_config(self, config: config_options.Config) -> dict:
dict: global configuration object
"""

# 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 == "":
theme_locale = vars(config["theme"]).get("_vars", {}).get("language", "")
# Get locale settings - might be added in future mkdocs versions
# see: https://github.com/timvink/mkdocs-git-revision-date-localized-plugin/issues/24
mkdocs_locale = config.get("locale", None)

# Get locale from plugin configuration
plugin_locale = self.config.get("locale", None)

# theme locale
if "theme" in config and "locale" in config.get("theme"):
custom_theme = config.get("theme")
theme_locale = custom_theme._vars.get("locale")
logging.debug(
"Locale '%s' extracted from the custom theme: '%s'"
% (theme_locale, custom_theme.name)
)
elif "theme" in config and "language" in config.get("theme"):
custom_theme = config.get("theme")
theme_locale = custom_theme._vars.get("language")
logging.debug(
"Locale '%s' extracted from the custom theme: '%s'"
% (theme_locale, custom_theme.name)
)

else:
theme_locale = None
logging.debug(
"No locale found in theme configuration (or no custom theme set)"
)

# First prio: plugin locale
if plugin_locale != "":
self.locale = plugin_locale
if plugin_locale:
locale_set = plugin_locale
logging.debug("Using locale from plugin configuration: %s" % locale_set)
# Second prio: theme locale
elif theme_locale != "":
self.locale = theme_locale
elif theme_locale:
locale_set = theme_locale
logging.debug(
"Locale not set in plugin. Fallback to theme configuration: %s"
% locale_set
)
# Third prio is mkdocs locale (which might be added in the future)
elif mkdocs_locale != "":
self.locale = mkdocs_locale
elif mkdocs_locale:
locale_set = mkdocs_locale
logging.debug("Using locale from mkdocs configuration: %s" % locale_set)
else:
self.locale = "en"
locale_set = "en"
logging.debug("No locale set. Fallback to: %s" % locale_set)

# set locale also in plugin configuration
self.config["locale"] = locale_set

return config

Expand All @@ -78,7 +110,7 @@ def on_post_page(self, output_content: str, **kwargs) -> str:
str: output of rendered template as string
"""

if self.config["type"] != "timeago":
if self.config.get("type") != "timeago":
return output_content

extra_js = """
Expand Down Expand Up @@ -118,7 +150,7 @@ def on_page_markdown(

revision_dates = self.util.get_revision_date_for_file(
path=page.file.abs_src_path,
locale=self.locale,
locale=self.config.get("locale", "en"),
fallback_to_build_date=self.config.get("fallback_to_build_date"),
)
revision_date = revision_dates[self.config["type"]]
Expand All @@ -129,3 +161,11 @@ def on_page_markdown(
markdown,
flags=re.IGNORECASE,
)


# ##############################################################################
# ##### Stand alone program ########
# ##################################
if __name__ == "__main__":
"""Standalone execution and quick tests."""
pass
3 changes: 2 additions & 1 deletion mkdocs_git_revision_date_localized_plugin/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ def _date_formats(unix_timestamp: float, locale="en") -> dict:
timestamp_in_ms = unix_timestamp * 1000

revision_date = datetime.utcfromtimestamp(unix_timestamp)
logging.debug("Revision date: %s - Locale: %s" % (revision_date, locale))

return {
"date": format_date(revision_date, format="long", locale=locale),
Expand Down Expand Up @@ -118,7 +119,7 @@ def get_revision_date_for_file(
unix_timestamp = datetime.utcnow().timestamp()
logging.warning("%s has no git logs, using current timestamp" % path)

return self._date_formats(unix_timestamp)
return self._date_formats(unix_timestamp=unix_timestamp, locale=locale)


def is_shallow_clone(repo: Git) -> bool:
Expand Down
2 changes: 1 addition & 1 deletion tests/basic_setup/mkdocs_datetime.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ use_directory_urls: true
plugins:
- search
- git-revision-date-localized:
type: datetime
type: datetime
2 changes: 1 addition & 1 deletion tests/basic_setup/mkdocs_locale.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
site_name: test gitrevisiondatelocalized_plugin
use_directory_urls: true

locale: nl
locale: fr

plugins:
- search
Expand Down
2 changes: 1 addition & 1 deletion tests/basic_setup/mkdocs_plugin_locale.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ use_directory_urls: true
plugins:
- search
- git-revision-date-localized:
locale: nl
locale: nl
9 changes: 9 additions & 0 deletions tests/basic_setup/mkdocs_theme_no_locale.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
site_name: test gitrevisiondatelocalized_plugin
use_directory_urls: true

theme:
name: 'material'

plugins:
- search
- git-revision-date-localized
99 changes: 71 additions & 28 deletions tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,60 @@
>>> os.mkdir(tmp_path)
"""

# #############################################################################
# ########## Libraries #############
# ##################################

# standard lib
import logging
import os
import re
import shutil

# 3rd partu
# MkDocs
from mkdocs.__main__ import build_command
from mkdocs.config import load_config

# other 3rd party
import git
import pytest
import yaml
from click.testing import CliRunner
from mkdocs.__main__ import build_command

# package module
from mkdocs_git_revision_date_localized_plugin.util import Util

# #############################################################################
# ######## Globals #################
# ##################################

PLUGIN_NAME = "git-revision-date-localized"

def load_config(mkdocs_path):
return yaml.load(open(mkdocs_path, "rb"), Loader=yaml.Loader)
# custom log level to get plugin info messages
logging.basicConfig(level=logging.INFO)


# #############################################################################
# ########## Helpers ###############
# ##################################
def get_plugin_config_from_mkdocs(mkdocs_path) -> dict:
# instanciate plugin
cfg_mkdocs = load_config(mkdocs_path)

plugins = cfg_mkdocs.get("plugins")
plugin_loaded = plugins.get(PLUGIN_NAME)

cfg = plugin_loaded.on_config(cfg_mkdocs)
logging.info("Fixture configuration loaded: " + str(cfg))

assert (
plugin_loaded.config.get("locale") is not None
), "Locale should never be None after plugin is loaded"

logging.info(
"Locale '%s' determined from %s"
% (plugin_loaded.config.get("locale"), mkdocs_path)
)
return plugin_loaded.config


def setup_clean_mkdocs_folder(mkdocs_yml_path, output_path):
Expand Down Expand Up @@ -141,7 +176,7 @@ def build_docs_setup(testproject_path):
raise


def validate_build(testproject_path):
def validate_build(testproject_path, plugin_config: dict):
"""
Validates a build from a testproject

Expand All @@ -157,19 +192,21 @@ def validate_build(testproject_path):
# Make sure with markdown tag has valid
# git revision date tag
page_with_tag = testproject_path / "site/page_with_tag/index.html"
contents = page_with_tag.read_text()
contents = page_with_tag.read_text(encoding="utf8")
assert re.search(r"Markdown tag\:\s[<span>|\w].+", contents)

repo = Util(testproject_path)
date_formats = repo.get_revision_date_for_file(
testproject_path / "docs/page_with_tag.md"
path=testproject_path / "docs/page_with_tag.md",
locale=plugin_config.get("locale"),
fallback_to_build_date=plugin_config.get("fallback_to_build_date"),
)

searches = [re.search(x, contents) for x in date_formats.values()]
assert any(searches), "No correct date formats output was found"


def validate_mkdocs_file(temp_path, mkdocs_yml_file):
def validate_mkdocs_file(temp_path: str, mkdocs_yml_file: str):
"""
Creates a clean mkdocs project
for a mkdocs YML file, builds and validates it.
Expand All @@ -184,14 +221,18 @@ def validate_mkdocs_file(temp_path, mkdocs_yml_file):
setup_commit_history(testproject_path)
result = build_docs_setup(testproject_path)
assert result.exit_code == 0, "'mkdocs build' command failed"
validate_build(testproject_path)

return testproject_path

# validate build with locale retrieved from mkdocs config file
validate_build(
testproject_path, plugin_config=get_plugin_config_from_mkdocs(mkdocs_yml_file)
)

#### Tests ####
return testproject_path


# #############################################################################
# ########### Tests ################
# ##################################
def test_date_formats():
u = Util()
assert u._date_formats(1582397529) == {
Expand All @@ -217,19 +258,6 @@ def test_missing_git_repo(tmp_path):
), "'mkdocs build' command succeeded while there is no GIT repo"


def test_missing_git_repo_ignored(tmp_path):
"""
When there is no GIT repo, the git error should be ignored.
"""
testproject_path = setup_clean_mkdocs_folder(
mkdocs_yml_path="tests/basic_setup/mkdocs_fallback_to_build_date.yml",
output_path=tmp_path,
)

result = build_docs_setup(testproject_path)
assert result.exit_code == 0


def test_build_no_options(tmp_path):
# Enable plugin with no extra options set
validate_mkdocs_file(tmp_path, "tests/basic_setup/mkdocs.yml")
Expand All @@ -241,7 +269,7 @@ def test_build_locale_plugin(tmp_path):


def test_build_locale_mkdocs(tmp_path):
# Enable plugin with mkdocs locale set to 'nl'
# Enable plugin with mkdocs locale set to 'fr'
validate_mkdocs_file(tmp_path, "tests/basic_setup/mkdocs_locale.yml")


Expand All @@ -257,10 +285,25 @@ def test_material_theme(tmp_path):
# In mkdocs-material, a 'last update' should appear
# in German because locale is set to 'de'
index_file = testproject_path / "site/index.html"
contents = index_file.read_text()
contents = index_file.read_text(encoding="utf8")
assert re.search(r"Letztes Update\:\s[\w].+", contents)


def test_material_theme_no_locale(tmp_path):
"""
When using mkdocs-material theme, test correct working
"""
# theme set to 'material' with 'language' set to 'de'
testproject_path = validate_mkdocs_file(
tmp_path, "tests/basic_setup/mkdocs_theme_no_locale.yml"
)

# In mkdocs-material, a 'last update' should appear
# in German because locale is set to 'de'
index_file = testproject_path / "site/index.html"
contents = index_file.read_text(encoding="utf8")
assert re.search(r"Last update\:\s[\w].+", contents)

def test_type_timeago(tmp_path):
# type: 'timeago'
testproject_path = validate_mkdocs_file(
Expand Down
Loading