Skip to content
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

Precompute all last commit timestamps in on_files #116

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
46 changes: 36 additions & 10 deletions mkdocs_git_revision_date_localized_plugin/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@
"""
# standard lib
import logging
import multiprocessing
import re
import os
import time

# 3rd party
from mkdocs.config import config_options
from mkdocs.config.defaults import MkDocsConfig
from mkdocs.plugins import BasePlugin
from mkdocs.structure.files import Files
from mkdocs.structure.nav import Page
from mkdocs.utils import copy_file
from mkdocs.exceptions import ConfigurationError
Expand All @@ -34,6 +37,10 @@ class GitRevisionDateLocalizedPlugin(BasePlugin):
See https://www.mkdocs.org/user-guide/plugins
"""

def __init__(self):
super().__init__()
self.commit_timestamps = {}

config_scheme = (
("fallback_to_build_date", config_options.Type(bool, default=False)),
("locale", config_options.Type(str, default=None)),
Expand Down Expand Up @@ -63,7 +70,7 @@ def on_config(self, config: config_options.Config, **kwargs) -> Dict[str, Any]:
"""
if not self.config.get('enabled'):
return config

assert self.config['type'] in ["date","datetime","iso_date","iso_datetime","timeago","custom"]

self.util = Util(config=self.config)
Expand Down Expand Up @@ -138,9 +145,32 @@ def on_config(self, config: config_options.Config, **kwargs) -> Dict[str, Any]:
msg = "[git-revision-date-localized] should be defined after the i18n plugin in your mkdocs.yml file. "
msg += "This is because i18n adds a 'locale' variable to markdown pages that this plugin supports."
raise ConfigurationError(msg)

return config

def parallel_compute_commit_timestamps(self, files, docs_dir):
pool = multiprocessing.Pool(processes=min(10, multiprocessing.cpu_count()))
results = []
for file in files:
if file.is_documentation_page():
abs_src_path = os.path.join(docs_dir, file.src_uri)
result = pool.apply_async(
self.util.get_git_commit_timestamp, args=(abs_src_path, False)
)
results.append((abs_src_path, result))
pool.close()
pool.join()
for src_uri, result in results:
self.commit_timestamps[src_uri] = result.get()

def on_files(self, files: Files, config: MkDocsConfig):
# TechDocs copies docs_dir to a tmp dir and we need the real git path.
if not self.commit_timestamps:
real_docs_dir = os.path.join(
os.path.dirname(config["config_file_path"]), "docs"
)
self.parallel_compute_commit_timestamps(files, real_docs_dir)

def on_page_markdown(
self, markdown: str, page: Page, config: config_options.Config, files, **kwargs
) -> str:
Expand Down Expand Up @@ -189,23 +219,20 @@ def on_page_markdown(
# Finally, if no page locale set, we take the locale determined on_config()
if not locale:
locale = self.config.get("locale")

# MkDocs supports 2-letter and 5-letter locales
# https://www.mkdocs.org/user-guide/localizing-your-theme/#supported-locales
# We need the 2 letter variant
if len(locale) == 5:
locale = locale[:2]
assert len(locale) == 2, "locale must be a 2 letter code, see https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes"

# Retrieve git commit timestamp
# Except for generated pages (f.e. by mkdocs-gen-files plugin)
if hasattr(page.file, "generated_by"):
last_revision_timestamp = int(time.time())
else:
last_revision_timestamp = self.util.get_git_commit_timestamp(
path=page.file.abs_src_path,
is_first_commit=False,
)
last_revision_timestamp = self.commit_timestamps[page.file.abs_src_path]

# Last revision date
revision_dates = self.util.get_date_formats_for_timestamp(last_revision_timestamp, locale=locale, add_spans=True)
Expand Down Expand Up @@ -250,12 +277,11 @@ def on_page_markdown(
flags=re.IGNORECASE,
)


# If creation date not enabled, return markdown
# This is for speed: prevents another `git log` operation each file
if not self.config.get("enable_creation_date"):
return markdown

# Retrieve git commit timestamp
# Except for generated pages (f.e. by mkdocs-gen-files plugin)
if hasattr(page.file, "generated_by"):
Expand Down