@@ -100,20 +100,17 @@ def blog_add_posts(context):
100
100
posts = []
101
101
# posts from the file system
102
102
if context ["blog" ]["posts_path" ]:
103
- posts_path = os . path . join (
104
- context ["source_path" ], * context [ " blog" ]["posts_path" ]. split ( "/" )
103
+ posts_path = pathlib . Path ( context [ "source_path" ]) / pathlib . Path (
104
+ context ["blog" ]["posts_path" ]
105
105
)
106
- for fname in os . listdir ( posts_path ):
107
- if fname .startswith ("index." ):
106
+ for fname in posts_path . iterdir ( ):
107
+ if fname .name . startswith ("index." ):
108
108
continue
109
- link = (
110
- f"/{ context ['blog' ]['posts_path' ]} "
111
- f"/{ os .path .splitext (fname )[0 ]} .html"
112
- )
109
+ link = f"/{ context ['blog' ]['posts_path' ]} /{ fname .stem } .html"
113
110
md = markdown .Markdown (
114
111
extensions = context ["main" ]["markdown_extensions" ]
115
112
)
116
- with open (os . path . join ( posts_path , fname ), encoding = "utf-8" ) as f :
113
+ with fname . open (encoding = "utf-8" ) as f :
117
114
html = md .convert (f .read ())
118
115
title = md .Meta ["title" ][0 ]
119
116
summary = re .sub (tag_expr , "" , html )
@@ -394,7 +391,7 @@ def get_context(config_fname: str, **kwargs):
394
391
with open (config_fname , encoding = "utf-8" ) as f :
395
392
context = yaml .safe_load (f )
396
393
397
- context ["source_path" ] = os . path . dirname (config_fname )
394
+ context ["source_path" ] = pathlib . Path (config_fname ). parent
398
395
context .update (kwargs )
399
396
400
397
preprocessors = (
@@ -414,9 +411,9 @@ def get_source_files(source_path: str) -> typing.Generator[str, None, None]:
414
411
Generate the list of files present in the source directory.
415
412
"""
416
413
for root , dirs , fnames in os .walk (source_path ):
417
- root_rel_path = os . path . relpath (root , source_path )
414
+ root_rel_path = pathlib . Path (root ). relative_to ( source_path )
418
415
for fname in fnames :
419
- yield os . path . join (root_rel_path , fname )
416
+ yield str (root_rel_path / fname )
420
417
421
418
422
419
def extend_base_template (content : str , base_template : str ) -> str :
@@ -442,16 +439,16 @@ def main(
442
439
before copying them. ``.md`` files are transformed to HTML.
443
440
"""
444
441
# Sanity check: validate that versions.json is valid JSON
445
- versions_path = os . path . join (source_path , "versions.json" )
446
- with open (versions_path , encoding = "utf-8" ) as f :
442
+ versions_path = pathlib . Path (source_path ) / "versions.json"
443
+ with versions_path . open (encoding = "utf-8" ) as f :
447
444
try :
448
445
json .load (f )
449
446
except json .JSONDecodeError as e :
450
447
raise RuntimeError (
451
448
f"Invalid versions.json: { e } . Ensure it is valid JSON."
452
449
) from e
453
450
454
- config_fname = os . path . join (source_path , "config.yml" )
451
+ config_fname = pathlib . Path (source_path ) / "config.yml"
455
452
456
453
shutil .rmtree (target_path , ignore_errors = True )
457
454
os .makedirs (target_path , exist_ok = True )
@@ -460,20 +457,19 @@ def main(
460
457
context = get_context (config_fname , target_path = target_path )
461
458
sys .stderr .write ("Context generated\n " )
462
459
463
- templates_path = os . path . join (source_path , context ["main" ]["templates_path" ])
460
+ templates_path = pathlib . Path (source_path ) / context ["main" ]["templates_path" ]
464
461
jinja_env = jinja2 .Environment (loader = jinja2 .FileSystemLoader (templates_path ))
465
462
466
463
for fname in get_source_files (source_path ):
467
- if os . path . normpath (fname ) in context ["main" ]["ignore" ]:
464
+ if str ( pathlib . Path (fname ) ) in context ["main" ]["ignore" ]:
468
465
continue
469
-
470
466
sys .stderr .write (f"Processing { fname } \n " )
471
- dirname = os . path . dirname (fname )
472
- os . makedirs ( os . path . join ( target_path , dirname ), exist_ok = True )
467
+ dirname = pathlib . Path (fname ). parent
468
+ ( target_path / dirname ). mkdir ( parents = True , exist_ok = True )
473
469
474
- extension = os . path . splitext (fname )[ - 1 ]
470
+ extension = pathlib . Path (fname ). suffix
475
471
if extension in (".html" , ".md" ):
476
- with open ( os . path . join (source_path , fname ), encoding = "utf-8" ) as f :
472
+ with ( pathlib . Path (source_path ) / fname ). open ( encoding = "utf-8" ) as f :
477
473
content = f .read ()
478
474
if extension == ".md" :
479
475
if "pdeps/" in fname :
@@ -503,17 +499,17 @@ def main(
503
499
# Python-Markdown doesn't let us config table attributes by hand
504
500
body = body .replace ("<table>" , '<table class="table table-bordered">' )
505
501
content = extend_base_template (body , context ["main" ]["base_template" ])
506
- context ["base_url" ] = "" .join (["../" ] * os .path .normpath (fname ).count ("/" ))
502
+ context ["base_url" ] = "" .join (
503
+ ["../" ] * pathlib .Path (fname ).parts .count ("/" )
504
+ )
507
505
content = jinja_env .from_string (content ).render (** context )
508
- fname_html = os . path . splitext (fname )[ 0 ] + ".html"
509
- with open (
510
- os . path . join ( target_path , fname_html ), "w" , encoding = "utf-8"
506
+ fname_html = pathlib . Path (fname ). with_suffix ( ".html" ). name
507
+ with ( pathlib . Path ( target_path ) / fname_html ). open (
508
+ "w" , encoding = "utf-8"
511
509
) as f :
512
510
f .write (content )
513
511
else :
514
- shutil .copy (
515
- os .path .join (source_path , fname ), os .path .join (target_path , dirname )
516
- )
512
+ shutil .copy (pathlib .Path (source_path ) / fname , target_path / dirname )
517
513
518
514
519
515
if __name__ == "__main__" :
0 commit comments