17
17
# 3rd party
18
18
import markdown
19
19
from git import GitCommandError , GitCommandNotFound , InvalidGitRepositoryError , Repo
20
- from mkdocs .config .config_options import Config
20
+ from mkdocs .config .defaults import MkDocsConfig
21
21
from mkdocs .plugins import get_plugin_logger
22
22
from mkdocs .structure .pages import Page
23
23
from mkdocs .utils import get_build_datetime
@@ -162,11 +162,11 @@ def get_value_from_dot_key(self, data: dict, dot_key: str | bool) -> Any:
162
162
def get_file_dates (
163
163
self ,
164
164
in_page : Page ,
165
- source_date_creation : str = "git" ,
166
- source_date_update : str = "git" ,
167
- meta_datetime_format : str = "%Y-%m-%d %H:%M" ,
168
- meta_default_timezone : str = "UTC" ,
169
- meta_default_time : datetime | None = None ,
165
+ source_date_creation : str ,
166
+ source_date_update : str ,
167
+ meta_datetime_format : str ,
168
+ meta_default_timezone : str ,
169
+ meta_default_time : datetime ,
170
170
) -> tuple [datetime , datetime ]:
171
171
"""Extract creation and update dates from page metadata (yaml frontmatter) or
172
172
git log for given file.
@@ -189,6 +189,7 @@ def get_file_dates(
189
189
:return: tuple of timestamps (creation date, last commit date)
190
190
:rtype: Tuple[datetime, datetime]
191
191
"""
192
+ logger .debug (f"Extracting dates for { in_page .file .src_uri } " )
192
193
# empty vars
193
194
dt_created = dt_updated = None
194
195
if meta_default_time is None :
@@ -233,13 +234,13 @@ def get_file_dates(
233
234
)
234
235
235
236
if isinstance (dt_updated , str ):
236
- logger .info (
237
- f"Update date of { in_page .file .abs_src_path } is an "
238
- f"a character string: { dt_updated } ({ type (dt_updated )} )"
237
+ logger .debug (
238
+ f"Update date of { in_page .file .abs_src_path } is a "
239
+ f"character string: { dt_updated } ({ type (dt_updated )} )"
239
240
)
240
241
241
242
elif dt_updated is None :
242
- logger .info (
243
+ logger .debug (
243
244
f"Update date of { in_page .file .abs_src_path } is an "
244
245
f"unrecognized type: { dt_updated } ({ type (dt_updated )} )"
245
246
)
@@ -287,8 +288,6 @@ def get_file_dates(
287
288
dt_updated = set_datetime_zoneinfo (
288
289
datetime .fromtimestamp (float (dt_updated )), meta_default_timezone
289
290
)
290
- else :
291
- pass
292
291
293
292
# results
294
293
if all ([dt_created , dt_updated ]):
@@ -297,25 +296,31 @@ def get_file_dates(
297
296
dt_updated ,
298
297
)
299
298
elif dt_created :
300
- logger . info (
301
- f "Updated date could not be retrieved for page: "
302
- f"{ in_page .file .abs_src_path } . Maybe it has never been committed yet? "
299
+ log_msg = (
300
+ "Updated date could not be retrieved for page: "
301
+ f"{ in_page .file .abs_src_path } . Fallback to build date. "
303
302
)
303
+ if self .use_git :
304
+ log_msg += "Maybe it has never been committed yet?"
305
+ logger .debug (log_msg )
304
306
return (
305
307
dt_created ,
306
308
get_build_datetime (),
307
309
)
308
310
elif dt_updated :
309
- logger . info (
310
- f "Creation date could not be retrieved for page: "
311
- f"{ in_page .file .abs_src_path } . Maybe it has never been committed yet? "
311
+ log_msg = (
312
+ "Creation date could not be retrieved for page: "
313
+ f"{ in_page .file .abs_src_path } . Fallback to build date. "
312
314
)
315
+ if self .use_git :
316
+ log_msg += "Maybe it has never been committed yet?"
317
+ logger .debug (log_msg )
313
318
return (
314
319
get_build_datetime (),
315
320
dt_updated ,
316
321
)
317
322
else :
318
- logging . warning (
323
+ logger . info (
319
324
f"Dates could not be retrieved for page: { in_page .file .abs_src_path } ."
320
325
)
321
326
return (
@@ -363,7 +368,7 @@ def get_authors_from_meta(self, in_page: Page) -> tuple[str] | None:
363
368
364
369
def get_categories_from_meta (
365
370
self , in_page : Page , categories_labels : Iterable
366
- ) -> tuple :
371
+ ) -> list | None :
367
372
"""Returns category from page meta.
368
373
369
374
:param in_page: input page to parse
@@ -372,7 +377,7 @@ def get_categories_from_meta(
372
377
:type categories_labels: Iterable
373
378
374
379
:return: found categories
375
- :rtype: tuple
380
+ :rtype: list
376
381
"""
377
382
if not categories_labels :
378
383
return None
@@ -384,8 +389,6 @@ def get_categories_from_meta(
384
389
output_categories .extend (in_page .meta .get (category_label ))
385
390
elif isinstance (in_page .meta .get (category_label ), str ):
386
391
output_categories .append (in_page .meta .get (category_label ))
387
- else :
388
- pass
389
392
else :
390
393
continue
391
394
return sorted (output_categories )
@@ -426,7 +429,7 @@ def get_date_from_meta(
426
429
date = date_metatag_value , time = meta_default_time .time ()
427
430
)
428
431
else :
429
- logger .info (
432
+ logger .debug (
430
433
f"Incompatible date type: { type (date_metatag_value )} . It must be: "
431
434
"date, datetime or str (complying with defined strftime format)."
432
435
)
@@ -551,6 +554,7 @@ def get_image(self, in_page: Page, base_url: str) -> tuple[str, str, int] | None
551
554
)
552
555
553
556
if img_local_path .is_file ():
557
+ logger .debug ("Local image already exists. Using it to get its length." )
554
558
img_length = img_local_path .stat ().st_size
555
559
else :
556
560
logger .debug (
@@ -585,7 +589,7 @@ def get_image(self, in_page: Page, base_url: str) -> tuple[str, str, int] | None
585
589
# return final tuple
586
590
return (img_url , mime_type , img_length )
587
591
588
- def get_local_image_length (self , page_path : str , path_to_append : str ) -> int :
592
+ def get_local_image_length (self , page_path : str , path_to_append : str ) -> int | None :
589
593
"""Calculates local image size in octets.
590
594
591
595
Args:
@@ -653,7 +657,7 @@ def get_remote_image_length(
653
657
return int (img_length )
654
658
655
659
@staticmethod
656
- def get_site_url (mkdocs_config : Config ) -> str | None :
660
+ def get_site_url (mkdocs_config : MkDocsConfig ) -> str | None :
657
661
"""Extract site URL from MkDocs configuration and enforce the behavior to ensure \
658
662
returning a str with length > 0 or None. If exists, it adds an ending slash.
659
663
@@ -679,7 +683,7 @@ def get_site_url(mkdocs_config: Config) -> str | None:
679
683
680
684
return site_url
681
685
682
- def guess_locale (self , mkdocs_config : Config ) -> str | None :
686
+ def guess_locale (self , mkdocs_config : MkDocsConfig ) -> str | None :
683
687
"""Extract language code from MkDocs or Theme configuration.
684
688
685
689
:param mkdocs_config: configuration object
0 commit comments