Skip to content

Commit 073b12c

Browse files
committed
Fix bug with raw page.meta creation tags, see #64
1 parent ff73362 commit 073b12c

File tree

3 files changed

+58
-58
lines changed

3 files changed

+58
-58
lines changed

mkdocs_git_revision_date_localized_plugin/plugin.py

Lines changed: 44 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -155,13 +155,14 @@ def on_page_markdown(
155155
logging.debug("Excluding page " + page.file.src_path)
156156
return markdown
157157

158-
# revision date
159-
revision_dates = self.util.get_date_formats_for_timestamp(
160-
commit_timestamp=self.util.get_git_commit_timestamp(
158+
# Retrieve git commit timestamp
159+
last_revision_timestamp = self.util.get_git_commit_timestamp(
161160
path=page.file.abs_src_path,
162161
is_first_commit=False,
163-
)
164162
)
163+
164+
# Last revision date
165+
revision_dates = self.util.get_date_formats_for_timestamp(last_revision_timestamp)
165166
revision_date = revision_dates[self.config["type"]]
166167

167168
# timeago output is dynamic, which breaks when you print a page
@@ -170,8 +171,13 @@ def on_page_markdown(
170171
if self.config["type"] == "timeago":
171172
revision_date += revision_dates["iso_date"]
172173

173-
# Add to page meta information
174+
# Add to page meta information, for developers
175+
# Include variants without the CSS <span> elements (raw date strings)
174176
page.meta["git_revision_date_localized"] = revision_date
177+
revision_dates_raw = self.util.get_date_formats_for_timestamp(last_revision_timestamp, add_spans=False)
178+
for date_type, date_string in revision_dates_raw.items():
179+
page.meta["git_revision_date_localized_raw_%s" % date_type] = date_string
180+
175181
# Replace any occurances in markdown page
176182
markdown = re.sub(
177183
r"\{\{\s*git_revision_date_localized\s*\}\}",
@@ -180,54 +186,41 @@ def on_page_markdown(
180186
flags=re.IGNORECASE,
181187
)
182188

183-
# Creation date
184-
if self.config.get("enable_creation_date"):
185-
creation_dates = self.util.get_date_formats_for_timestamp(
186-
commit_timestamp=self.util.get_git_commit_timestamp(
187-
path=page.file.abs_src_path,
188-
is_first_commit=True,
189-
)
190-
)
191-
creation_date = creation_dates[self.config["type"]]
192-
193-
# timeago output is dynamic, which breaks when you print a page
194-
# This ensures fallback to type "iso_date"
195-
# controlled via CSS (see on_post_build() event)
196-
if self.config["type"] == "timeago":
197-
creation_date += creation_dates["iso_date"]
198-
199-
page.meta["git_creation_date_localized"] = creation_date
200-
markdown = re.sub(
201-
r"\{\{\s*git_creation_date_localized\s*\}\}",
202-
creation_date,
203-
markdown,
204-
flags=re.IGNORECASE,
205-
)
206-
207-
# Developers might want access to the raw date strings
208-
# Let's expose them also
209-
revision_dates = self.util._date_formats(
210-
unix_timestamp=self.util.get_git_commit_timestamp(
211-
path=page.file.abs_src_path,
212-
is_first_commit=False,
213-
),
214-
time_zone=self.config.get("time_zone"),
215-
locale=self.config.get("locale")
189+
# If creation date not enabled, return markdown
190+
# This is for speed: prevents another `git log` operation each file
191+
if not self.config.get("enable_creation_date"):
192+
return markdown
193+
194+
# Retrieve git commit timestamp
195+
first_revision_timestamp = self.util.get_git_commit_timestamp(
196+
path=page.file.abs_src_path,
197+
is_first_commit=True,
216198
)
217-
for date_type, date_string in revision_dates.items():
218-
page.meta["git_revision_date_localized_raw_%s" % date_type] = date_string
219199

220-
if self.config.get("enable_creation_date"):
221-
creation_dates = self.util._date_formats(
222-
unix_timestamp=self.util.get_git_commit_timestamp(
223-
path=page.file.abs_src_path,
224-
is_first_commit=False,
225-
),
226-
time_zone=self.config.get("time_zone"),
227-
locale=self.config.get("locale")
228-
)
229-
for date_type, date_string in creation_dates.items():
230-
page.meta["git_creation_date_localized_raw_%s" % date_type] = date_string
200+
# Creation date formats
201+
creation_dates = self.util.get_date_formats_for_timestamp(first_revision_timestamp)
202+
creation_date = creation_dates[self.config["type"]]
203+
204+
# timeago output is dynamic, which breaks when you print a page
205+
# This ensures fallback to type "iso_date"
206+
# controlled via CSS (see on_post_build() event)
207+
if self.config["type"] == "timeago":
208+
creation_date += creation_dates["iso_date"]
209+
210+
# Add to page meta information, for developers
211+
# Include variants without the CSS <span> elements (raw date strings)
212+
page.meta["git_creation_date_localized"] = creation_date
213+
creation_dates_raw = self.util.get_date_formats_for_timestamp(first_revision_timestamp, add_spans=False)
214+
for date_type, date_string in creation_dates_raw.items():
215+
page.meta["git_creation_date_localized_raw_%s" % date_type] = date_string
216+
217+
# Replace any occurances in markdown page
218+
markdown = re.sub(
219+
r"\{\{\s*git_creation_date_localized\s*\}\}",
220+
creation_date,
221+
markdown,
222+
flags=re.IGNORECASE,
223+
)
231224

232225
return markdown
233226

mkdocs_git_revision_date_localized_plugin/util.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import logging
33
import os
44
import time
5-
from datetime import datetime
5+
from datetime import date, datetime
66

77
from mkdocs_git_revision_date_localized_plugin.ci import raise_ci_warnings
88

@@ -173,9 +173,10 @@ def get_git_commit_timestamp(
173173
def get_date_formats_for_timestamp(
174174
self,
175175
commit_timestamp: int,
176+
add_spans: bool = True
176177
) -> Dict[str, str]:
177178
"""
178-
Determine localized date variants for a given file.
179+
Determine localized date variants for a given timestamp.
179180
180181
Args:
181182
commit_timestamp (int): most recent commit date in unix timestamp.
@@ -185,19 +186,25 @@ def get_date_formats_for_timestamp(
185186
Returns:
186187
dict: Localized date variants.
187188
"""
188-
189189
date_formats = self._date_formats(
190190
unix_timestamp=commit_timestamp,
191191
time_zone=self.config.get("time_zone"),
192192
locale=self.config.get("locale")
193193
)
194+
if add_spans:
195+
date_formats = self.add_spans(date_formats)
196+
197+
return date_formats
194198

195-
# Wrap in <span> for styling
199+
200+
@staticmethod
201+
def add_spans(date_formats: Dict[str, str]) -> Dict[str, str]:
202+
"""
203+
Wraps the date string in <span> elements with CSS identifiers.
204+
"""
196205
for date_type, date_string in date_formats.items():
197206
date_formats[date_type] = (
198207
'<span class="git-revision-date-localized-plugin git-revision-date-localized-plugin-%s">%s</span>'
199208
% (date_type, date_string)
200209
)
201-
202210
return date_formats
203-

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
setup(
1414
name="mkdocs-git-revision-date-localized-plugin",
15-
version="0.10.1",
15+
version="0.10.2",
1616
description="Mkdocs plugin that enables displaying the localized date of the last git modification of a markdown file.",
1717
long_description=LONG_DESCRIPTION,
1818
long_description_content_type="text/markdown",

0 commit comments

Comments
 (0)