Skip to content
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
70 changes: 35 additions & 35 deletions src/mkdocs_git_revision_date_localized_plugin/dates.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,49 +43,49 @@ def get_date_formats(
def strftime_to_babel_format(fmt: str) -> str:
"""
Convert strftime format string to Babel format pattern.

Args:
fmt (str): strftime format string

Returns:
str: Babel format pattern
"""
# Dictionary mapping strftime directives to Babel format patterns
mapping = {
'%a': 'EEE', # Weekday abbreviated
'%A': 'EEEE', # Weekday full
'%b': 'MMM', # Month abbreviated
'%B': 'MMMM', # Month full
'%c': '', # Locale's date and time (not directly mappable)
'%d': 'dd', # Day of month zero-padded
'%-d': 'd', # Day of month
'%e': 'd', # Day of month space-padded
'%f': 'SSSSSS', # Microsecond
'%H': 'HH', # Hour 24h zero-padded
'%-H': 'H', # Hour 24h
'%I': 'hh', # Hour 12h zero-padded
'%-I': 'h', # Hour 12h
'%j': 'DDD', # Day of year
'%m': 'MM', # Month zero-padded
'%-m': 'M', # Month
'%M': 'mm', # Minute zero-padded
'%-M': 'm', # Minute
'%p': 'a', # AM/PM
'%S': 'ss', # Second zero-padded
'%-S': 's', # Second
'%w': 'e', # Weekday as number
'%W': 'w', # Week of year
'%x': '', # Locale's date (not directly mappable)
'%X': '', # Locale's time (not directly mappable)
'%y': 'yy', # Year without century
'%Y': 'yyyy', # Year with century
'%z': 'Z', # UTC offset
'%Z': 'z', # Timezone name
'%%': '%' # Literal %
"%a": "EEE", # Weekday abbreviated
"%A": "EEEE", # Weekday full
"%b": "MMM", # Month abbreviated
"%B": "MMMM", # Month full
"%c": "", # Locale's date and time (not directly mappable)
"%d": "dd", # Day of month zero-padded
"%-d": "d", # Day of month
"%e": "d", # Day of month space-padded
"%f": "SSSSSS", # Microsecond
"%H": "HH", # Hour 24h zero-padded
"%-H": "H", # Hour 24h
"%I": "hh", # Hour 12h zero-padded
"%-I": "h", # Hour 12h
"%j": "DDD", # Day of year
"%m": "MM", # Month zero-padded
"%-m": "M", # Month
"%M": "mm", # Minute zero-padded
"%-M": "m", # Minute
"%p": "a", # AM/PM
"%S": "ss", # Second zero-padded
"%-S": "s", # Second
"%w": "e", # Weekday as number
"%W": "w", # Week of year
"%x": "", # Locale's date (not directly mappable)
"%X": "", # Locale's time (not directly mappable)
"%y": "yy", # Year without century
"%Y": "yyyy", # Year with century
"%z": "Z", # UTC offset
"%Z": "z", # Timezone name
"%%": "%", # Literal %
}

result = fmt
for strftime_code, babel_code in mapping.items():
result = result.replace(strftime_code, babel_code)
return result

return result
55 changes: 31 additions & 24 deletions src/mkdocs_git_revision_date_localized_plugin/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import os
import time
import multiprocessing
from pathlib import Path

from mkdocs import __version__ as mkdocs_version
from mkdocs.config import config_options
Expand All @@ -28,7 +29,7 @@

from packaging.version import Version

HERE = os.path.dirname(os.path.abspath(__file__))
HERE = Path(__file__).parent.absolute()


class GitRevisionDateLocalizedPlugin(BasePlugin):
Expand Down Expand Up @@ -144,19 +145,18 @@ def on_config(self, config: config_options.Config, **kwargs) -> Dict[str, Any]:

return config


def parallel_compute_commit_timestamps(self, files, original_source: Optional[Dict] = None, is_first_commit=False):
pool = multiprocessing.Pool(processes=min(10, multiprocessing.cpu_count()))
results = []
for file in files:
if file.is_documentation_page():
abs_src_path = file.abs_src_path
for f in files:
if f.is_documentation_page():
abs_src_path = f.abs_src_path
# Support plugins like monorep that might have moved the files from the original source that is under git
if original_source and abs_src_path in original_source:
abs_src_path = original_source[abs_src_path]
result = pool.apply_async(
self.util.get_git_commit_timestamp, args=(abs_src_path, is_first_commit)
)
assert Path(abs_src_path).exists()
abs_src_path = str(Path(abs_src_path).absolute())
result = pool.apply_async(self.util.get_git_commit_timestamp, args=(abs_src_path, is_first_commit))
results.append((abs_src_path, result))
pool.close()
pool.join()
Expand All @@ -173,18 +173,21 @@ def on_files(self, files: Files, config: MkDocsConfig):
"""
if not self.config.get("enabled") or not self.config.get("enable_parallel_processing"):
return

# Support monorepo/techdocs, which copies the docs_dir to a temporary directory
if "monorepo" in config.get('plugins', {}):
original_source = config.get('plugins').get('monorepo').merger.files_source_dir
if "monorepo" in config.get("plugins", {}):
original_source = config.get("plugins").get("monorepo").merger.files_source_dir
else:
original_source = None

if not self.last_revision_commits:
self.parallel_compute_commit_timestamps(files=files, original_source=original_source, is_first_commit=False)
if not self.created_commits:
self.parallel_compute_commit_timestamps(files=files, original_source=original_source, is_first_commit=True)

try:
if not self.last_revision_commits:
self.parallel_compute_commit_timestamps(files=files, original_source=original_source, is_first_commit=False)
if not self.created_commits:
self.parallel_compute_commit_timestamps(files=files, original_source=original_source, is_first_commit=True)
except Exception as e:
logging.warning(f"Parallel processing failed: {str(e)}.\n To fall back to serial processing, use 'enable_parallel_processing: False' setting.")


def on_page_markdown(self, markdown: str, page: Page, config: config_options.Config, files, **kwargs) -> str:
"""
Expand Down Expand Up @@ -240,7 +243,9 @@ def on_page_markdown(self, markdown: str, page: Page, config: config_options.Con
if getattr(page.file, "generated_by", None):
last_revision_hash, last_revision_timestamp = "", int(time.time())
else:
last_revision_hash, last_revision_timestamp = self.last_revision_commits.get(page.file.abs_src_path, (None, None))
last_revision_hash, last_revision_timestamp = self.last_revision_commits.get(
str(Path(page.file.abs_src_path).absolute()), (None, None)
)
if last_revision_timestamp is None:
last_revision_hash, last_revision_timestamp = self.util.get_git_commit_timestamp(
path=page.file.abs_src_path,
Expand Down Expand Up @@ -314,8 +319,10 @@ def on_page_markdown(self, markdown: str, page: Page, config: config_options.Con
if getattr(page.file, "generated_by", None):
first_revision_hash, first_revision_timestamp = "", int(time.time())
else:
first_revision_hash, first_revision_timestamp = self.created_commits.get(page.file.abs_src_path, (None, None))
if first_revision_timestamp is None:
first_revision_hash, first_revision_timestamp = self.created_commits.get(
str(Path(page.file.abs_src_path).absolute()), (None, None)
)
if first_revision_timestamp is None:
first_revision_hash, first_revision_timestamp = self.util.get_git_commit_timestamp(
path=page.file.abs_src_path,
is_first_commit=True,
Expand Down Expand Up @@ -374,8 +381,8 @@ def on_post_build(self, config: Dict[str, Any], **kwargs) -> None:
"js/timeago_mkdocs_material.js",
"css/timeago.css",
]
for file in files:
dest_file_path = os.path.join(config["site_dir"], file)
src_file_path = os.path.join(HERE, file)
assert os.path.exists(src_file_path)
copy_file(src_file_path, dest_file_path)
for f in files:
dest_file_path = Path(config["site_dir"]) / f
src_file_path = HERE / f
assert src_file_path.exists()
copy_file(str(src_file_path), str(dest_file_path))
3 changes: 2 additions & 1 deletion src/mkdocs_git_revision_date_localized_plugin/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import logging
import os
import time
from pathlib import Path

from mkdocs_git_revision_date_localized_plugin.ci import raise_ci_warnings
from mkdocs_git_revision_date_localized_plugin.dates import get_date_formats
Expand Down Expand Up @@ -34,7 +35,7 @@ def __init__(self, config: Dict, mkdocs_dir: str):
self.repo_cache = {}

ignore_commits_file = self.config.get("ignored_commits_file")
ignore_commits_filepath = os.path.join(mkdocs_dir, ignore_commits_file) if ignore_commits_file else None
ignore_commits_filepath = Path(mkdocs_dir) / ignore_commits_file if ignore_commits_file else None
self.ignored_commits = self.parse_git_ignore_revs(ignore_commits_filepath) if ignore_commits_file else []

def _get_repo(self, path: str) -> Git:
Expand Down
7 changes: 7 additions & 0 deletions tests/fixtures/basic_project/mkdocs_no_parallel.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
site_name: test gitrevisiondatelocalized_plugin
use_directory_urls: true

plugins:
- search
- git-revision-date-localized:
enable_parallel_processing: False
1 change: 0 additions & 1 deletion tests/fixtures/mkdocs-gen-files/gen_pages.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,3 @@

with mkdocs_gen_files.open("foo.md", "w") as f:
print("Hello, world!", file=f)

2 changes: 2 additions & 0 deletions tests/fixtures/monorepo/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
site/
build/
19 changes: 19 additions & 0 deletions tests/fixtures/monorepo/mkdocs_reverse_order.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
site_name: Cats API

nav:
- Intro: 'index.md'
- Authentication: 'authentication.md'
- Components: '*include ./components/*/mkdocs.yml'
- API:
- v1: '!include ./v1/mkdocs.yml'
- v2: '!include ./v2/mkdocs.yml'
- v3: '!include ./v3/mkdocs.yml'

theme:
name: material

plugins:
- search
# Note; here monorepo is defined after git-revision-date-localized
- monorepo
- git-revision-date-localized
91 changes: 46 additions & 45 deletions tests/fixtures/with_mknotebooks/docs/demo.ipynb

Large diffs are not rendered by default.

Loading