Skip to content

Commit 6caf88c

Browse files
authored
Merge pull request #169 from timvink/fix_windows
Disable windows edge-case of monorepo compatibility
2 parents 5955b20 + a5692df commit 6caf88c

File tree

12 files changed

+289
-284
lines changed

12 files changed

+289
-284
lines changed

src/mkdocs_git_revision_date_localized_plugin/dates.py

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -43,49 +43,49 @@ def get_date_formats(
4343
def strftime_to_babel_format(fmt: str) -> str:
4444
"""
4545
Convert strftime format string to Babel format pattern.
46-
46+
4747
Args:
4848
fmt (str): strftime format string
49-
49+
5050
Returns:
5151
str: Babel format pattern
5252
"""
5353
# Dictionary mapping strftime directives to Babel format patterns
5454
mapping = {
55-
'%a': 'EEE', # Weekday abbreviated
56-
'%A': 'EEEE', # Weekday full
57-
'%b': 'MMM', # Month abbreviated
58-
'%B': 'MMMM', # Month full
59-
'%c': '', # Locale's date and time (not directly mappable)
60-
'%d': 'dd', # Day of month zero-padded
61-
'%-d': 'd', # Day of month
62-
'%e': 'd', # Day of month space-padded
63-
'%f': 'SSSSSS', # Microsecond
64-
'%H': 'HH', # Hour 24h zero-padded
65-
'%-H': 'H', # Hour 24h
66-
'%I': 'hh', # Hour 12h zero-padded
67-
'%-I': 'h', # Hour 12h
68-
'%j': 'DDD', # Day of year
69-
'%m': 'MM', # Month zero-padded
70-
'%-m': 'M', # Month
71-
'%M': 'mm', # Minute zero-padded
72-
'%-M': 'm', # Minute
73-
'%p': 'a', # AM/PM
74-
'%S': 'ss', # Second zero-padded
75-
'%-S': 's', # Second
76-
'%w': 'e', # Weekday as number
77-
'%W': 'w', # Week of year
78-
'%x': '', # Locale's date (not directly mappable)
79-
'%X': '', # Locale's time (not directly mappable)
80-
'%y': 'yy', # Year without century
81-
'%Y': 'yyyy', # Year with century
82-
'%z': 'Z', # UTC offset
83-
'%Z': 'z', # Timezone name
84-
'%%': '%' # Literal %
55+
"%a": "EEE", # Weekday abbreviated
56+
"%A": "EEEE", # Weekday full
57+
"%b": "MMM", # Month abbreviated
58+
"%B": "MMMM", # Month full
59+
"%c": "", # Locale's date and time (not directly mappable)
60+
"%d": "dd", # Day of month zero-padded
61+
"%-d": "d", # Day of month
62+
"%e": "d", # Day of month space-padded
63+
"%f": "SSSSSS", # Microsecond
64+
"%H": "HH", # Hour 24h zero-padded
65+
"%-H": "H", # Hour 24h
66+
"%I": "hh", # Hour 12h zero-padded
67+
"%-I": "h", # Hour 12h
68+
"%j": "DDD", # Day of year
69+
"%m": "MM", # Month zero-padded
70+
"%-m": "M", # Month
71+
"%M": "mm", # Minute zero-padded
72+
"%-M": "m", # Minute
73+
"%p": "a", # AM/PM
74+
"%S": "ss", # Second zero-padded
75+
"%-S": "s", # Second
76+
"%w": "e", # Weekday as number
77+
"%W": "w", # Week of year
78+
"%x": "", # Locale's date (not directly mappable)
79+
"%X": "", # Locale's time (not directly mappable)
80+
"%y": "yy", # Year without century
81+
"%Y": "yyyy", # Year with century
82+
"%z": "Z", # UTC offset
83+
"%Z": "z", # Timezone name
84+
"%%": "%", # Literal %
8585
}
86-
86+
8787
result = fmt
8888
for strftime_code, babel_code in mapping.items():
8989
result = result.replace(strftime_code, babel_code)
90-
91-
return result
90+
91+
return result

src/mkdocs_git_revision_date_localized_plugin/plugin.py

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import os
1111
import time
1212
import multiprocessing
13+
from pathlib import Path
1314

1415
from mkdocs import __version__ as mkdocs_version
1516
from mkdocs.config import config_options
@@ -28,7 +29,7 @@
2829

2930
from packaging.version import Version
3031

31-
HERE = os.path.dirname(os.path.abspath(__file__))
32+
HERE = Path(__file__).parent.absolute()
3233

3334

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

145146
return config
146147

147-
148148
def parallel_compute_commit_timestamps(self, files, original_source: Optional[Dict] = None, is_first_commit=False):
149149
pool = multiprocessing.Pool(processes=min(10, multiprocessing.cpu_count()))
150150
results = []
151-
for file in files:
152-
if file.is_documentation_page():
153-
abs_src_path = file.abs_src_path
151+
for f in files:
152+
if f.is_documentation_page():
153+
abs_src_path = f.abs_src_path
154154
# Support plugins like monorep that might have moved the files from the original source that is under git
155155
if original_source and abs_src_path in original_source:
156156
abs_src_path = original_source[abs_src_path]
157-
result = pool.apply_async(
158-
self.util.get_git_commit_timestamp, args=(abs_src_path, is_first_commit)
159-
)
157+
assert Path(abs_src_path).exists()
158+
abs_src_path = str(Path(abs_src_path).absolute())
159+
result = pool.apply_async(self.util.get_git_commit_timestamp, args=(abs_src_path, is_first_commit))
160160
results.append((abs_src_path, result))
161161
pool.close()
162162
pool.join()
@@ -173,18 +173,21 @@ def on_files(self, files: Files, config: MkDocsConfig):
173173
"""
174174
if not self.config.get("enabled") or not self.config.get("enable_parallel_processing"):
175175
return
176-
176+
177177
# Support monorepo/techdocs, which copies the docs_dir to a temporary directory
178-
if "monorepo" in config.get('plugins', {}):
179-
original_source = config.get('plugins').get('monorepo').merger.files_source_dir
178+
if "monorepo" in config.get("plugins", {}):
179+
original_source = config.get("plugins").get("monorepo").merger.files_source_dir
180180
else:
181181
original_source = None
182182

183-
if not self.last_revision_commits:
184-
self.parallel_compute_commit_timestamps(files=files, original_source=original_source, is_first_commit=False)
185-
if not self.created_commits:
186-
self.parallel_compute_commit_timestamps(files=files, original_source=original_source, is_first_commit=True)
187-
183+
try:
184+
if not self.last_revision_commits:
185+
self.parallel_compute_commit_timestamps(files=files, original_source=original_source, is_first_commit=False)
186+
if not self.created_commits:
187+
self.parallel_compute_commit_timestamps(files=files, original_source=original_source, is_first_commit=True)
188+
except Exception as e:
189+
logging.warning(f"Parallel processing failed: {str(e)}.\n To fall back to serial processing, use 'enable_parallel_processing: False' setting.")
190+
188191

189192
def on_page_markdown(self, markdown: str, page: Page, config: config_options.Config, files, **kwargs) -> str:
190193
"""
@@ -240,7 +243,9 @@ def on_page_markdown(self, markdown: str, page: Page, config: config_options.Con
240243
if getattr(page.file, "generated_by", None):
241244
last_revision_hash, last_revision_timestamp = "", int(time.time())
242245
else:
243-
last_revision_hash, last_revision_timestamp = self.last_revision_commits.get(page.file.abs_src_path, (None, None))
246+
last_revision_hash, last_revision_timestamp = self.last_revision_commits.get(
247+
str(Path(page.file.abs_src_path).absolute()), (None, None)
248+
)
244249
if last_revision_timestamp is None:
245250
last_revision_hash, last_revision_timestamp = self.util.get_git_commit_timestamp(
246251
path=page.file.abs_src_path,
@@ -314,8 +319,10 @@ def on_page_markdown(self, markdown: str, page: Page, config: config_options.Con
314319
if getattr(page.file, "generated_by", None):
315320
first_revision_hash, first_revision_timestamp = "", int(time.time())
316321
else:
317-
first_revision_hash, first_revision_timestamp = self.created_commits.get(page.file.abs_src_path, (None, None))
318-
if first_revision_timestamp is None:
322+
first_revision_hash, first_revision_timestamp = self.created_commits.get(
323+
str(Path(page.file.abs_src_path).absolute()), (None, None)
324+
)
325+
if first_revision_timestamp is None:
319326
first_revision_hash, first_revision_timestamp = self.util.get_git_commit_timestamp(
320327
path=page.file.abs_src_path,
321328
is_first_commit=True,
@@ -374,8 +381,8 @@ def on_post_build(self, config: Dict[str, Any], **kwargs) -> None:
374381
"js/timeago_mkdocs_material.js",
375382
"css/timeago.css",
376383
]
377-
for file in files:
378-
dest_file_path = os.path.join(config["site_dir"], file)
379-
src_file_path = os.path.join(HERE, file)
380-
assert os.path.exists(src_file_path)
381-
copy_file(src_file_path, dest_file_path)
384+
for f in files:
385+
dest_file_path = Path(config["site_dir"]) / f
386+
src_file_path = HERE / f
387+
assert src_file_path.exists()
388+
copy_file(str(src_file_path), str(dest_file_path))

src/mkdocs_git_revision_date_localized_plugin/util.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import logging
44
import os
55
import time
6+
from pathlib import Path
67

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

3637
ignore_commits_file = self.config.get("ignored_commits_file")
37-
ignore_commits_filepath = os.path.join(mkdocs_dir, ignore_commits_file) if ignore_commits_file else None
38+
ignore_commits_filepath = Path(mkdocs_dir) / ignore_commits_file if ignore_commits_file else None
3839
self.ignored_commits = self.parse_git_ignore_revs(ignore_commits_filepath) if ignore_commits_file else []
3940

4041
def _get_repo(self, path: str) -> Git:
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
site_name: test gitrevisiondatelocalized_plugin
2+
use_directory_urls: true
3+
4+
plugins:
5+
- search
6+
- git-revision-date-localized:
7+
enable_parallel_processing: False

tests/fixtures/mkdocs-gen-files/gen_pages.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,3 @@
22

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

tests/fixtures/monorepo/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
site/
2+
build/
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
site_name: Cats API
2+
3+
nav:
4+
- Intro: 'index.md'
5+
- Authentication: 'authentication.md'
6+
- Components: '*include ./components/*/mkdocs.yml'
7+
- API:
8+
- v1: '!include ./v1/mkdocs.yml'
9+
- v2: '!include ./v2/mkdocs.yml'
10+
- v3: '!include ./v3/mkdocs.yml'
11+
12+
theme:
13+
name: material
14+
15+
plugins:
16+
- search
17+
# Note; here monorepo is defined after git-revision-date-localized
18+
- monorepo
19+
- git-revision-date-localized

tests/fixtures/with_mknotebooks/docs/demo.ipynb

Lines changed: 46 additions & 45 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)