Skip to content

Commit 1faaf2d

Browse files
committed
Add support for mkdocs-gen-files
1 parent b6a27cc commit 1faaf2d

File tree

8 files changed

+119
-48
lines changed

8 files changed

+119
-48
lines changed

mkdocs_git_revision_date_localized_plugin/plugin.py

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import logging
99
import re
1010
import os
11+
import time
1112

1213
# 3rd party
1314
from mkdocs.config import config_options
@@ -195,12 +196,15 @@ def on_page_markdown(
195196
locale = locale[:2]
196197
assert len(locale) == 2, "locale must be a 2 letter code, see https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes"
197198

198-
199199
# Retrieve git commit timestamp
200-
last_revision_timestamp = self.util.get_git_commit_timestamp(
201-
path=page.file.abs_src_path,
202-
is_first_commit=False,
203-
)
200+
# Except for generated pages (f.e. by mkdocs-gen-files plugin)
201+
if hasattr(page.file, "generated_by"):
202+
last_revision_timestamp = int(time.time())
203+
else:
204+
last_revision_timestamp = self.util.get_git_commit_timestamp(
205+
path=page.file.abs_src_path,
206+
is_first_commit=False,
207+
)
204208

205209
# Last revision date
206210
revision_dates = self.util.get_date_formats_for_timestamp(last_revision_timestamp, locale=locale, add_spans=True)
@@ -252,10 +256,14 @@ def on_page_markdown(
252256
return markdown
253257

254258
# Retrieve git commit timestamp
255-
first_revision_timestamp = self.util.get_git_commit_timestamp(
256-
path=page.file.abs_src_path,
257-
is_first_commit=True,
258-
)
259+
# Except for generated pages (f.e. by mkdocs-gen-files plugin)
260+
if hasattr(page.file, "generated_by"):
261+
first_revision_timestamp = int(time.time())
262+
else:
263+
first_revision_timestamp = self.util.get_git_commit_timestamp(
264+
path=page.file.abs_src_path,
265+
is_first_commit=True,
266+
)
259267

260268
# Creation date formats
261269
creation_dates = self.util.get_date_formats_for_timestamp(first_revision_timestamp, locale=locale, add_spans=True)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# mkdocs-gen-files
2+
3+
Test with mkdocs-gen-files
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# index
2+
3+
home page.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# test page
2+
3+
Tag <mark>\{\{ git_revision_date_localized \}\}</mark> renders as: {{ git_revision_date_localized }}
4+
5+
Tag <mark>\{\{ git_creation_date_localized \}\}</mark> renders as: {{ git_creation_date_localized }}
6+
7+
Tag <mark>\{\{ git_site_revision_date_localized \}\}</mark> renders as: {{ git_site_revision_date_localized }}
8+
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import mkdocs_gen_files
2+
3+
with mkdocs_gen_files.open("foo.md", "w") as f:
4+
print("Hello, world!", file=f)
5+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
site_name: demo revision date
2+
use_directory_urls: true
3+
4+
theme:
5+
name: material
6+
7+
plugins:
8+
- search
9+
- git-revision-date-localized:
10+
enable_creation_date: true
11+
- gen-files:
12+
scripts:
13+
- gen_pages.py # or any other name or path

tests/test_builds.py

Lines changed: 69 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import re
1717
import shutil
1818
from contextlib import contextmanager
19+
from pathlib import Path
1920

2021
# MkDocs
2122
from mkdocs.__main__ import build_command
@@ -112,6 +113,8 @@ def setup_clean_mkdocs_folder(mkdocs_yml_path, output_path):
112113
)
113114
shutil.rmtree(str(testproject_path))
114115

116+
# shutil.copytree(str(Path(mkdocs_yml_path).parent), testproject_path)
117+
115118
# Copy correct mkdocs.yml file and our test 'docs/'
116119
if "i18n" in mkdocs_yml_path:
117120
shutil.copytree("tests/fixtures/i18n/docs", str(testproject_path / "docs"))
@@ -120,6 +123,9 @@ def setup_clean_mkdocs_folder(mkdocs_yml_path, output_path):
120123

121124
shutil.copyfile(mkdocs_yml_path, str(testproject_path / "mkdocs.yml"))
122125

126+
if "gen-files" in mkdocs_yml_path:
127+
shutil.copyfile(str(Path(mkdocs_yml_path).parent / "gen_pages.py"), str(testproject_path / "gen_pages.py"))
128+
123129
return testproject_path
124130

125131

@@ -143,49 +149,54 @@ def setup_commit_history(testproject_path):
143149
repo = git.Repo.init(testproject_path, bare=False)
144150
author = "Test Person <[email protected]>"
145151

152+
146153
with working_directory(testproject_path):
147154

148155
# page_with_tags contains tags we replace and test
149-
repo.git.add("docs/page_with_tag.md")
150-
repo.git.commit(message="add homepage", author=author, date="1500854705") # Mon Jul 24 2017 00:05:05 GMT+0000
151-
152-
file_name = os.path.join(testproject_path, "docs/page_with_tag.md")
153-
with open(file_name, "a") as the_file:
154-
the_file.write("awa\n")
155-
repo.git.add("docs/page_with_tag.md")
156-
repo.git.commit(message="update homepage", author=author, date="1642911026") # Sun Jan 23 2022 04:10:26 GMT+0000
157-
158-
bf_file_name = os.path.join(testproject_path, "docs/page_with_renamed.md")
159-
af_file_name = os.path.join(testproject_path, "docs/subfolder/page_with_renamed.md")
160-
# Since git.mv would actually remove the file, move page_with_renamed.md back to docs if it has been moved
161-
if os.path.exists(af_file_name):
162-
os.replace(af_file_name, bf_file_name)
163-
repo.git.add("docs/page_with_renamed.md")
164-
repo.git.commit(message="page_with_renamed.md before renamed", author=author, date="1655229469") # Tue Jun 14 2022 17:57:49 GMT+0000
165-
repo.git.mv("docs/page_with_renamed.md", "docs/subfolder/page_with_renamed.md")
166-
repo.git.commit(message="page_with_renamed.md after renamed", author=author, date="1655229515") # Tue Jun 14 2022 17:58:35 GMT+0000
167-
168-
repo.git.add("docs/first_page.md")
169-
repo.git.commit(message="first page", author=author, date="1500854705") # Mon Jul 24 2017 00:05:05 GMT+0000
170-
file_name = os.path.join(testproject_path, "docs/first_page.md")
171-
with open(file_name, "w+") as the_file:
172-
the_file.write("Hello\n")
173-
repo.git.add("docs/first_page.md")
174-
repo.git.commit(message="first page update 1", author=author, date="1519964705") # Fri Mar 02 2018 04:25:05 GMT+0000
175-
with open(file_name, "w") as the_file:
176-
the_file.write("# First Test Page Edited\n\nSome Lorem text")
177-
repo.git.add("docs/first_page.md")
178-
repo.git.commit(message="first page update 2", author=author, date="1643911026") # Thu Feb 03 2022 17:57:06 GMT+0000
156+
if os.path.exists("docs/page_with_tag.md"):
157+
repo.git.add("docs/page_with_tag.md")
158+
repo.git.commit(message="add homepage", author=author, date="1500854705") # Mon Jul 24 2017 00:05:05 GMT+0000
159+
160+
file_name = os.path.join(testproject_path, "docs/page_with_tag.md")
161+
with open(file_name, "a") as the_file:
162+
the_file.write("awa\n")
163+
repo.git.add("docs/page_with_tag.md")
164+
repo.git.commit(message="update homepage", author=author, date="1642911026") # Sun Jan 23 2022 04:10:26 GMT+0000
165+
166+
if os.path.exists("docs/page_with_renamed.md"):
167+
bf_file_name = os.path.join(testproject_path, "docs/page_with_renamed.md")
168+
af_file_name = os.path.join(testproject_path, "docs/subfolder/page_with_renamed.md")
169+
# Since git.mv would actually remove the file, move page_with_renamed.md back to docs if it has been moved
170+
if os.path.exists(af_file_name):
171+
os.replace(af_file_name, bf_file_name)
172+
repo.git.add("docs/page_with_renamed.md")
173+
repo.git.commit(message="page_with_renamed.md before renamed", author=author, date="1655229469") # Tue Jun 14 2022 17:57:49 GMT+0000
174+
repo.git.mv("docs/page_with_renamed.md", "docs/subfolder/page_with_renamed.md")
175+
repo.git.commit(message="page_with_renamed.md after renamed", author=author, date="1655229515") # Tue Jun 14 2022 17:58:35 GMT+0000
176+
177+
if os.path.exists("docs/first_page.md"):
178+
repo.git.add("docs/first_page.md")
179+
repo.git.commit(message="first page", author=author, date="1500854705") # Mon Jul 24 2017 00:05:05 GMT+0000
180+
file_name = os.path.join(testproject_path, "docs/first_page.md")
181+
with open(file_name, "w+") as the_file:
182+
the_file.write("Hello\n")
183+
repo.git.add("docs/first_page.md")
184+
repo.git.commit(message="first page update 1", author=author, date="1519964705") # Fri Mar 02 2018 04:25:05 GMT+0000
185+
with open(file_name, "w") as the_file:
186+
the_file.write("# First Test Page Edited\n\nSome Lorem text")
187+
repo.git.add("docs/first_page.md")
188+
repo.git.commit(message="first page update 2", author=author, date="1643911026") # Thu Feb 03 2022 17:57:06 GMT+0000
179189

180190
repo.git.add("mkdocs.yml")
181191
repo.git.commit(message="add mkdocs", author=author, date="1500854705 -0700") # Mon Jul 24 2017 00:05:05 GMT+0000
182-
repo.git.add("docs/second_page.md")
183-
repo.git.commit(message="second page", author=author, date="1643911026") # Thu Feb 03 2022 17:57:06 GMT+0000
192+
193+
if os.path.exists("docs/second_page.md"):
194+
repo.git.add("docs/second_page.md")
195+
repo.git.commit(message="second page", author=author, date="1643911026") # Thu Feb 03 2022 17:57:06 GMT+0000
196+
184197
repo.git.add("docs/index.md")
185198
repo.git.commit(message="homepage", author=author, date="1643911026") # Thu Feb 03 2022 17:57:06 GMT+0000
186199

187-
188-
189200
return repo
190201

191202

@@ -264,11 +275,12 @@ def validate_build(testproject_path, plugin_config: dict = {}):
264275
searches = [x in contents for x in date_formats.values()]
265276
assert any(searches), "No correct creation date formats output was found"
266277

267-
commit_timestamp=repo.get_git_commit_timestamp(
268-
path=str(testproject_path / "docs/subfolder/page_with_renamed.md"),
269-
is_first_commit=True
270-
)
271-
assert commit_timestamp == 1655229469
278+
if os.path.exists(str(testproject_path / "docs/subfolder/page_with_renamed.md")):
279+
commit_timestamp=repo.get_git_commit_timestamp(
280+
path=str(testproject_path / "docs/subfolder/page_with_renamed.md"),
281+
is_first_commit=True
282+
)
283+
assert commit_timestamp == 1655229469
272284

273285
def validate_mkdocs_file(temp_path: str, mkdocs_yml_file: str):
274286
"""
@@ -642,3 +654,21 @@ def test_low_fetch_depth(tmp_path, caplog):
642654
assert result.exit_code == 0
643655
assert "Running on GitHub Actions might" in caplog.text
644656

657+
658+
659+
def test_mkdocs_genfiles_plugin(tmp_path):
660+
"""
661+
Make sure the mkdocs-gen-files plugin works correctly.
662+
"""
663+
testproject_path = setup_clean_mkdocs_folder(
664+
mkdocs_yml_path=f"tests/fixtures/mkdocs-gen-files/mkdocs.yml", output_path=tmp_path
665+
)
666+
setup_commit_history(testproject_path)
667+
result = build_docs_setup(testproject_path)
668+
assert result.exit_code == 0, f"'mkdocs build' command failed with {result.stdout}"
669+
670+
# validate the build
671+
plugin_config=get_plugin_config_from_mkdocs(str(testproject_path / "mkdocs.yml"))
672+
validate_build(
673+
testproject_path, plugin_config
674+
)

tests/test_requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ codecov
44
click
55
mkdocs-material
66
mkdocs-static-i18n
7+
mkdocs-gen-files

0 commit comments

Comments
 (0)