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