44https://www.mkdocs.org/
55https://github.com/timvink/mkdocs-git-revision-date-localized-plugin/
66"""
7+
78import logging
89import re
910import os
2627
2728HERE = os .path .dirname (os .path .abspath (__file__ ))
2829
30+
2931class GitRevisionDateLocalizedPlugin (BasePlugin ):
3032 """
3133 Mkdocs plugin to add revision date from Git.
@@ -62,16 +64,18 @@ def on_config(self, config: config_options.Config, **kwargs) -> Dict[str, Any]:
6264 Returns:
6365 dict: global configuration object
6466 """
65- if not self .config .get (' enabled' ):
67+ if not self .config .get (" enabled" ):
6668 return config
67-
68- assert self .config ['type' ] in ["date" ,"datetime" ,"iso_date" ,"iso_datetime" ,"timeago" ,"custom" ]
6969
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+ )
7175
7276 # Save last commit timestamp for entire site
7377 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" )
7579 )
7680
7781 # Get locale from plugin configuration
@@ -80,24 +84,22 @@ def on_config(self, config: config_options.Config, **kwargs) -> Dict[str, Any]:
8084 # theme locale
8185 if "theme" in config and "language" in config .get ("theme" ):
8286 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" )
8791 )
92+ logging .debug ("Locale '%s' extracted from the custom theme: '%s'" % (theme_locale , custom_theme .name ))
8893 elif "theme" in config and "locale" in config .get ("theme" ):
8994 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" )
9497 )
98+ logging .debug ("Locale '%s' extracted from the custom theme: '%s'" % (theme_locale , custom_theme .name ))
9599
96100 else :
97101 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)" )
101103
102104 # First prio: plugin locale
103105 if plugin_locale :
@@ -106,30 +108,25 @@ def on_config(self, config: config_options.Config, **kwargs) -> Dict[str, Any]:
106108 # Second prio: theme locale
107109 elif theme_locale :
108110 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 )
113112 # Lastly, fallback is English
114113 else :
115114 locale_set = "en"
116115 logging .debug ("No locale set. Fallback to: %s" % locale_set )
117116
118117 # Validate locale
119118 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+ )
121122
122123 # set locale also in plugin configuration
123124 self .config ["locale" ] = locale_set
124125
125126 # Add pointers to support files for timeago.js
126127 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" ]
133130 config ["extra_css" ] = ["css/timeago.css" ] + config ["extra_css" ]
134131
135132 # Compatibility with mkdocs-static-i18n
@@ -139,12 +136,10 @@ def on_config(self, config: config_options.Config, **kwargs) -> Dict[str, Any]:
139136 msg = "[git-revision-date-localized] should be defined after the i18n plugin in your mkdocs.yml file. "
140137 msg += "This is because i18n adds a 'locale' variable to markdown pages that this plugin supports."
141138 raise ConfigurationError (msg )
142-
139+
143140 return config
144141
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 :
148143 """
149144 Replace jinja2 tags in markdown and templates with the localized dates.
150145
@@ -164,7 +159,7 @@ def on_page_markdown(
164159 Returns:
165160 str: Markdown source text of page as string
166161 """
167- if not self .config .get (' enabled' ):
162+ if not self .config .get (" enabled" ):
168163 return markdown
169164
170165 # Exclude pages specified in config
@@ -185,31 +180,35 @@ def on_page_markdown(
185180 # Second prio is a frontmatter variable 'locale' set in the markdown
186181 if not locale :
187182 if "locale" in page .meta :
188- locale = page .meta [' locale' ]
183+ locale = page .meta [" locale" ]
189184
190185 # Finally, if no page locale set, we take the locale determined on_config()
191186 if not locale :
192187 locale = self .config .get ("locale" )
193-
188+
194189 # MkDocs supports 2-letter and 5-letter locales
195190 # https://www.mkdocs.org/user-guide/localizing-your-theme/#supported-locales
196191 # We need the 2 letter variant
197192 if len (locale ) == 5 :
198193 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+
201198 # Retrieve git commit timestamp
202199 # Except for generated pages (f.e. by mkdocs-gen-files plugin)
203200 if getattr (page .file , "generated_by" , None ):
204201 last_revision_hash , last_revision_timestamp = "" , int (time .time ())
205202 else :
206203 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 ,
209206 )
210207
211208 # 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+ )
213212 revision_date = revision_dates [self .config ["type" ]]
214213
215214 # timeago output is dynamic, which breaks when you print a page
@@ -223,7 +222,9 @@ def on_page_markdown(
223222 page .meta ["git_revision_date_localized" ] = revision_date
224223 page .meta ["git_revision_date_localized_hash" ] = last_revision_hash
225224 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+ )
227228 for date_type , date_string in revision_dates_raw .items ():
228229 page .meta ["git_revision_date_localized_raw_%s" % date_type ] = date_string
229230
@@ -237,13 +238,19 @@ def on_page_markdown(
237238
238239 # Also add site last updated information, for developers
239240 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+ )
242247 site_date = site_dates [self .config ["type" ]]
243248 if self .config ["type" ] == "timeago" :
244249 site_date += site_dates ["iso_date" ]
245250 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+ )
247254 for date_type , date_string in site_dates_raw .items ():
248255 page .meta ["git_site_revision_date_localized_raw_%s" % date_type ] = date_string
249256
@@ -255,12 +262,11 @@ def on_page_markdown(
255262 flags = re .IGNORECASE ,
256263 )
257264
258-
259265 # If creation date not enabled, return markdown
260266 # This is for speed: prevents another `git log` operation each file
261267 if not self .config .get ("enable_creation_date" ):
262268 return markdown
263-
269+
264270 # Retrieve git commit timestamp
265271 # Except for generated pages (f.e. by mkdocs-gen-files plugin)
266272 if getattr (page .file , "generated_by" , None ):
@@ -279,7 +285,9 @@ def on_page_markdown(
279285 first_revision_hash , first_revision_timestamp = last_revision_hash , last_revision_timestamp
280286
281287 # 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+ )
283291 creation_date = creation_dates [self .config ["type" ]]
284292
285293 # timeago output is dynamic, which breaks when you print a page
@@ -293,7 +301,9 @@ def on_page_markdown(
293301 page .meta ["git_creation_date_localized_hash" ] = first_revision_hash
294302 page .meta ["git_creation_date_localized_tag" ] = self .util .get_tag_name_for_commit (first_revision_hash )
295303 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+ )
297307 for date_type , date_string in creation_dates_raw .items ():
298308 page .meta ["git_creation_date_localized_raw_%s" % date_type ] = date_string
299309
@@ -314,7 +324,7 @@ def on_post_build(self, config: Dict[str, Any], **kwargs) -> None:
314324 Adds the timeago assets to the build.
315325 """
316326 # 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" ):
318328 files = [
319329 "js/timeago.min.js" ,
320330 "js/timeago_mkdocs_material.js" ,
0 commit comments