4
4
https://www.mkdocs.org/
5
5
https://github.com/timvink/mkdocs-git-revision-date-localized-plugin/
6
6
"""
7
+
7
8
import logging
8
9
import re
9
10
import os
26
27
27
28
HERE = os .path .dirname (os .path .abspath (__file__ ))
28
29
30
+
29
31
class GitRevisionDateLocalizedPlugin (BasePlugin ):
30
32
"""
31
33
Mkdocs plugin to add revision date from Git.
@@ -62,16 +64,18 @@ def on_config(self, config: config_options.Config, **kwargs) -> Dict[str, Any]:
62
64
Returns:
63
65
dict: global configuration object
64
66
"""
65
- if not self .config .get (' enabled' ):
67
+ if not self .config .get (" enabled" ):
66
68
return config
67
-
68
- assert self .config ['type' ] in ["date" ,"datetime" ,"iso_date" ,"iso_datetime" ,"timeago" ,"custom" ]
69
69
70
- self .util = Util (config = self .config , mkdocs_dir = os .path .abspath (os .path .dirname (config .get ('config_file_path' ))))
70
+ assert self .config ["type" ] in ["date" , "datetime" , "iso_date" , "iso_datetime" , "timeago" , "custom" ]
71
+
72
+ self .util = Util (
73
+ config = self .config , mkdocs_dir = os .path .abspath (os .path .dirname (config .get ("config_file_path" )))
74
+ )
71
75
72
76
# Save last commit timestamp for entire site
73
77
self .last_site_revision_hash , self .last_site_revision_timestamp = self .util .get_git_commit_timestamp (
74
- config .get (' docs_dir' )
78
+ config .get (" docs_dir" )
75
79
)
76
80
77
81
# Get locale from plugin configuration
@@ -80,24 +84,22 @@ def on_config(self, config: config_options.Config, **kwargs) -> Dict[str, Any]:
80
84
# theme locale
81
85
if "theme" in config and "language" in config .get ("theme" ):
82
86
custom_theme = config .get ("theme" )
83
- theme_locale = custom_theme [ "language" ] if Version ( mkdocs_version ) >= Version ( "1.6.0" ) else custom_theme . _vars . get ( "language" )
84
- logging . debug (
85
- "Locale '%s' extracted from the custom theme: '%s'"
86
- % ( theme_locale , custom_theme .name )
87
+ theme_locale = (
88
+ custom_theme [ "language" ]
89
+ if Version ( mkdocs_version ) >= Version ( "1.6.0" )
90
+ else custom_theme ._vars . get ( "language" )
87
91
)
92
+ logging .debug ("Locale '%s' extracted from the custom theme: '%s'" % (theme_locale , custom_theme .name ))
88
93
elif "theme" in config and "locale" in config .get ("theme" ):
89
94
custom_theme = config .get ("theme" )
90
- theme_locale = custom_theme .locale if Version (mkdocs_version ) >= Version ("1.6.0" ) else custom_theme ._vars .get ("locale" )
91
- logging .debug (
92
- "Locale '%s' extracted from the custom theme: '%s'"
93
- % (theme_locale , custom_theme .name )
95
+ theme_locale = (
96
+ custom_theme .locale if Version (mkdocs_version ) >= Version ("1.6.0" ) else custom_theme ._vars .get ("locale" )
94
97
)
98
+ logging .debug ("Locale '%s' extracted from the custom theme: '%s'" % (theme_locale , custom_theme .name ))
95
99
96
100
else :
97
101
theme_locale = None
98
- logging .debug (
99
- "No locale found in theme configuration (or no custom theme set)"
100
- )
102
+ logging .debug ("No locale found in theme configuration (or no custom theme set)" )
101
103
102
104
# First prio: plugin locale
103
105
if plugin_locale :
@@ -106,30 +108,25 @@ def on_config(self, config: config_options.Config, **kwargs) -> Dict[str, Any]:
106
108
# Second prio: theme locale
107
109
elif theme_locale :
108
110
locale_set = theme_locale
109
- logging .debug (
110
- "Locale not set in plugin. Fallback to theme configuration: %s"
111
- % locale_set
112
- )
111
+ logging .debug ("Locale not set in plugin. Fallback to theme configuration: %s" % locale_set )
113
112
# Lastly, fallback is English
114
113
else :
115
114
locale_set = "en"
116
115
logging .debug ("No locale set. Fallback to: %s" % locale_set )
117
116
118
117
# Validate locale
119
118
locale_set = str (locale_set )
120
- assert len (locale_set ) == 2 , "locale must be a 2 letter code, see https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes"
119
+ assert len (locale_set ) == 2 , (
120
+ "locale must be a 2 letter code, see https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes"
121
+ )
121
122
122
123
# set locale also in plugin configuration
123
124
self .config ["locale" ] = locale_set
124
125
125
126
# Add pointers to support files for timeago.js
126
127
if self .config .get ("type" ) == "timeago" :
127
- config ["extra_javascript" ] = ["js/timeago_mkdocs_material.js" ] + config [
128
- "extra_javascript"
129
- ]
130
- config ["extra_javascript" ] = ["js/timeago.min.js" ] + config [
131
- "extra_javascript"
132
- ]
128
+ config ["extra_javascript" ] = ["js/timeago_mkdocs_material.js" ] + config ["extra_javascript" ]
129
+ config ["extra_javascript" ] = ["js/timeago.min.js" ] + config ["extra_javascript" ]
133
130
config ["extra_css" ] = ["css/timeago.css" ] + config ["extra_css" ]
134
131
135
132
# Compatibility with mkdocs-static-i18n
@@ -139,12 +136,10 @@ def on_config(self, config: config_options.Config, **kwargs) -> Dict[str, Any]:
139
136
msg = "[git-revision-date-localized] should be defined after the i18n plugin in your mkdocs.yml file. "
140
137
msg += "This is because i18n adds a 'locale' variable to markdown pages that this plugin supports."
141
138
raise ConfigurationError (msg )
142
-
139
+
143
140
return config
144
141
145
- def on_page_markdown (
146
- self , markdown : str , page : Page , config : config_options .Config , files , ** kwargs
147
- ) -> str :
142
+ def on_page_markdown (self , markdown : str , page : Page , config : config_options .Config , files , ** kwargs ) -> str :
148
143
"""
149
144
Replace jinja2 tags in markdown and templates with the localized dates.
150
145
@@ -164,7 +159,7 @@ def on_page_markdown(
164
159
Returns:
165
160
str: Markdown source text of page as string
166
161
"""
167
- if not self .config .get (' enabled' ):
162
+ if not self .config .get (" enabled" ):
168
163
return markdown
169
164
170
165
# Exclude pages specified in config
@@ -185,31 +180,35 @@ def on_page_markdown(
185
180
# Second prio is a frontmatter variable 'locale' set in the markdown
186
181
if not locale :
187
182
if "locale" in page .meta :
188
- locale = page .meta [' locale' ]
183
+ locale = page .meta [" locale" ]
189
184
190
185
# Finally, if no page locale set, we take the locale determined on_config()
191
186
if not locale :
192
187
locale = self .config .get ("locale" )
193
-
188
+
194
189
# MkDocs supports 2-letter and 5-letter locales
195
190
# https://www.mkdocs.org/user-guide/localizing-your-theme/#supported-locales
196
191
# We need the 2 letter variant
197
192
if len (locale ) == 5 :
198
193
locale = locale [:2 ]
199
- assert len (locale ) == 2 , "locale must be a 2 letter code, see https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes"
200
-
194
+ assert len (locale ) == 2 , (
195
+ "locale must be a 2 letter code, see https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes"
196
+ )
197
+
201
198
# Retrieve git commit timestamp
202
199
# Except for generated pages (f.e. by mkdocs-gen-files plugin)
203
200
if getattr (page .file , "generated_by" , None ):
204
201
last_revision_hash , last_revision_timestamp = "" , int (time .time ())
205
202
else :
206
203
last_revision_hash , last_revision_timestamp = self .util .get_git_commit_timestamp (
207
- path = page .file .abs_src_path ,
208
- is_first_commit = False ,
204
+ path = page .file .abs_src_path ,
205
+ is_first_commit = False ,
209
206
)
210
207
211
208
# Last revision date
212
- revision_dates = self .util .get_date_formats_for_timestamp (last_revision_timestamp , locale = locale , add_spans = True )
209
+ revision_dates = self .util .get_date_formats_for_timestamp (
210
+ last_revision_timestamp , locale = locale , add_spans = True
211
+ )
213
212
revision_date = revision_dates [self .config ["type" ]]
214
213
215
214
# timeago output is dynamic, which breaks when you print a page
@@ -223,7 +222,9 @@ def on_page_markdown(
223
222
page .meta ["git_revision_date_localized" ] = revision_date
224
223
page .meta ["git_revision_date_localized_hash" ] = last_revision_hash
225
224
page .meta ["git_revision_date_localized_tag" ] = self .util .get_tag_name_for_commit (last_revision_hash )
226
- revision_dates_raw = self .util .get_date_formats_for_timestamp (last_revision_timestamp , locale = locale , add_spans = False )
225
+ revision_dates_raw = self .util .get_date_formats_for_timestamp (
226
+ last_revision_timestamp , locale = locale , add_spans = False
227
+ )
227
228
for date_type , date_string in revision_dates_raw .items ():
228
229
page .meta ["git_revision_date_localized_raw_%s" % date_type ] = date_string
229
230
@@ -237,13 +238,19 @@ def on_page_markdown(
237
238
238
239
# Also add site last updated information, for developers
239
240
page .meta ["git_site_revision_date_localized_hash" ] = self .last_site_revision_hash
240
- page .meta ["git_site_revision_date_localized_tag" ] = self .util .get_tag_name_for_commit (self .last_site_revision_hash )
241
- site_dates = self .util .get_date_formats_for_timestamp (self .last_site_revision_timestamp , locale = locale , add_spans = True )
241
+ page .meta ["git_site_revision_date_localized_tag" ] = self .util .get_tag_name_for_commit (
242
+ self .last_site_revision_hash
243
+ )
244
+ site_dates = self .util .get_date_formats_for_timestamp (
245
+ self .last_site_revision_timestamp , locale = locale , add_spans = True
246
+ )
242
247
site_date = site_dates [self .config ["type" ]]
243
248
if self .config ["type" ] == "timeago" :
244
249
site_date += site_dates ["iso_date" ]
245
250
page .meta ["git_site_revision_date_localized" ] = site_date
246
- site_dates_raw = self .util .get_date_formats_for_timestamp (self .last_site_revision_timestamp , locale = locale , add_spans = False )
251
+ site_dates_raw = self .util .get_date_formats_for_timestamp (
252
+ self .last_site_revision_timestamp , locale = locale , add_spans = False
253
+ )
247
254
for date_type , date_string in site_dates_raw .items ():
248
255
page .meta ["git_site_revision_date_localized_raw_%s" % date_type ] = date_string
249
256
@@ -255,12 +262,11 @@ def on_page_markdown(
255
262
flags = re .IGNORECASE ,
256
263
)
257
264
258
-
259
265
# If creation date not enabled, return markdown
260
266
# This is for speed: prevents another `git log` operation each file
261
267
if not self .config .get ("enable_creation_date" ):
262
268
return markdown
263
-
269
+
264
270
# Retrieve git commit timestamp
265
271
# Except for generated pages (f.e. by mkdocs-gen-files plugin)
266
272
if getattr (page .file , "generated_by" , None ):
@@ -279,7 +285,9 @@ def on_page_markdown(
279
285
first_revision_hash , first_revision_timestamp = last_revision_hash , last_revision_timestamp
280
286
281
287
# Creation date formats
282
- creation_dates = self .util .get_date_formats_for_timestamp (first_revision_timestamp , locale = locale , add_spans = True )
288
+ creation_dates = self .util .get_date_formats_for_timestamp (
289
+ first_revision_timestamp , locale = locale , add_spans = True
290
+ )
283
291
creation_date = creation_dates [self .config ["type" ]]
284
292
285
293
# timeago output is dynamic, which breaks when you print a page
@@ -293,7 +301,9 @@ def on_page_markdown(
293
301
page .meta ["git_creation_date_localized_hash" ] = first_revision_hash
294
302
page .meta ["git_creation_date_localized_tag" ] = self .util .get_tag_name_for_commit (first_revision_hash )
295
303
page .meta ["git_creation_date_localized" ] = creation_date
296
- creation_dates_raw = self .util .get_date_formats_for_timestamp (first_revision_timestamp , locale = locale , add_spans = False )
304
+ creation_dates_raw = self .util .get_date_formats_for_timestamp (
305
+ first_revision_timestamp , locale = locale , add_spans = False
306
+ )
297
307
for date_type , date_string in creation_dates_raw .items ():
298
308
page .meta ["git_creation_date_localized_raw_%s" % date_type ] = date_string
299
309
@@ -314,7 +324,7 @@ def on_post_build(self, config: Dict[str, Any], **kwargs) -> None:
314
324
Adds the timeago assets to the build.
315
325
"""
316
326
# Add timeago files:
317
- if self .config .get ("type" ) == "timeago" and self .config .get (' enabled' ):
327
+ if self .config .get ("type" ) == "timeago" and self .config .get (" enabled" ):
318
328
files = [
319
329
"js/timeago.min.js" ,
320
330
"js/timeago_mkdocs_material.js" ,
0 commit comments