@@ -81,22 +81,17 @@ def navbar_add_info(context: dict, skip: bool = True) -> dict:
81
81
``has_subitems`` that tells which one of them every element is. It
82
82
also adds a ``slug`` field to be used as a CSS id.
83
83
"""
84
- if skip :
85
- return context
86
-
87
- lang = context ["selected_language" ]
88
84
ignore = context ["translations" ]["ignore" ]
89
- default_language = context ["translations" ]["default_language" ]
90
- for i , item in enumerate (context ["navbar" ]):
91
- if item ["target" ] in ignore :
92
- if lang != default_language :
93
- item ["target" ] = "../" + item ["target" ]
94
-
95
- context ["navbar" ][i ] = dict (
96
- item ,
97
- has_subitems = isinstance (item ["target" ], list ),
98
- slug = item ["name" ].replace (" " , "-" ).lower (),
99
- )
85
+ for language in context ["languages" ]:
86
+ for i , item in enumerate (context ["navbar" ][language ]):
87
+ if item ["target" ] in ignore :
88
+ item ["target" ] = f"../{ item ['target' ]} "
89
+
90
+ context ["navbar" ][language ][i ] = dict (
91
+ item ,
92
+ has_subitems = isinstance (item ["target" ], list ),
93
+ slug = (item ["name" ].replace (" " , "-" ).lower ()),
94
+ )
100
95
return context
101
96
102
97
@staticmethod
@@ -396,7 +391,9 @@ def get_callable(obj_as_str: str) -> object:
396
391
return obj
397
392
398
393
399
- def get_context (config_fname : str , ** kwargs : dict ) -> dict :
394
+ def get_context (
395
+ config_fname : str , navbar_fname : str , languages : list [str ], ** kwargs : dict
396
+ ) -> dict :
400
397
"""
401
398
Load the config yaml as the base context, and enrich it with the
402
399
information added by the context preprocessors defined in the file.
@@ -405,6 +402,22 @@ def get_context(config_fname: str, **kwargs: dict) -> dict:
405
402
context = yaml .safe_load (f )
406
403
407
404
context ["source_path" ] = os .path .dirname (config_fname )
405
+
406
+ navbar = {}
407
+ context ["languages" ] = languages
408
+ default_language = context ["translations" ]["default_language" ]
409
+ default_prefix = context ["translations" ]["default_prefix" ]
410
+ for language in languages :
411
+ prefix = default_prefix if language == default_language else language
412
+ navbar_path = os .path .join (context ["source_path" ], prefix , navbar_fname )
413
+
414
+ with open (navbar_path , encoding = "utf-8" ) as f :
415
+ navbar_lang = yaml .safe_load (f )
416
+
417
+ navbar [language ] = navbar_lang ["navbar" ]
418
+
419
+ context ["navbar" ] = navbar
420
+
408
421
context .update (kwargs )
409
422
preprocessors = (
410
423
get_callable (context_prep )
@@ -418,43 +431,14 @@ def get_context(config_fname: str, **kwargs: dict) -> dict:
418
431
return context
419
432
420
433
421
- def update_navbar_context (context : dict ) -> dict :
422
- """
423
- Update the context with the navbar information for each language.
424
- """
425
- language = context ["selected_language" ]
426
- lang_prefix = (
427
- language if language != context ["translations" ]["default_language" ] else ""
428
- )
429
- navbar_path = os .path .join (context ["source_path" ], lang_prefix , "navbar.yml" )
430
- with open (navbar_path ) as f :
431
- navbar = yaml .safe_load (f )
432
-
433
- context .update (navbar )
434
- return Preprocessors .navbar_add_info (context , skip = False )
435
-
436
-
437
- def get_source_files (
438
- source_path : str , language : str , languages : list [str ]
439
- ) -> typing .Generator [str , None , None ]:
434
+ def get_source_files (source_path : str ) -> typing .Generator [str , None , None ]:
440
435
"""
441
436
Generate the list of files present in the source directory.
442
437
"""
443
- paths = []
444
- all_languages = languages [:]
445
- all_languages .remove (language )
446
438
for root , dirs , fnames in os .walk (source_path ):
447
439
root_rel_path = os .path .relpath (root , source_path )
448
440
for fname in fnames :
449
- path = os .path .join (root_rel_path , fname )
450
- for language in all_languages :
451
- if path .startswith (language + "/" ):
452
- break
453
- else :
454
- paths .append (path )
455
-
456
- for path in paths :
457
- yield path
441
+ yield os .path .join (root_rel_path , fname )
458
442
459
443
460
444
def extend_base_template (content : str , base_template : str ) -> str :
@@ -472,7 +456,6 @@ def extend_base_template(content: str, base_template: str) -> str:
472
456
def main (
473
457
source_path : str ,
474
458
target_path : str ,
475
- process_translations : bool = False ,
476
459
) -> int :
477
460
"""
478
461
Copy every file in the source directory to the target directory.
@@ -481,8 +464,6 @@ def main(
481
464
before copying them. ``.md`` files are transformed to HTML.
482
465
"""
483
466
base_folder = os .path .dirname (__file__ )
484
- base_source_path = source_path
485
- base_target_path = target_path
486
467
487
468
shutil .rmtree (target_path , ignore_errors = True )
488
469
os .makedirs (target_path , exist_ok = True )
@@ -491,12 +472,13 @@ def main(
491
472
sys .path .append (base_folder )
492
473
trans = importlib .import_module ("pandas_translations" )
493
474
translated_languages , languages = trans .process_translations (
494
- "config.yml" , source_path , process_translations
475
+ "config.yml" , source_path
495
476
)
496
477
497
478
sys .stderr .write ("Generating context...\n " )
498
479
context = get_context (
499
480
os .path .join (source_path , "config.yml" ),
481
+ navbar_fname = "navbar.yml" ,
500
482
target_path = target_path ,
501
483
languages = languages ,
502
484
)
@@ -505,52 +487,51 @@ def main(
505
487
templates_path = os .path .join (source_path , context ["main" ]["templates_path" ])
506
488
jinja_env = jinja2 .Environment (loader = jinja2 .FileSystemLoader (templates_path ))
507
489
508
- for language in languages :
509
- context ["selected_language" ] = language
510
- context = update_navbar_context (context )
511
- sys .stderr .write (f"\n Processing language: { language } ...\n \n " )
512
-
513
- if language != context ["translations" ]["default_language" ]:
514
- target_path = os .path .join (base_target_path , language )
515
- source_path = os .path .join (base_source_path , language )
490
+ default_language = context ["translations" ]["default_language" ]
491
+ for fname in get_source_files (source_path ):
492
+ selected_language = context ["translations" ]["default_language" ]
493
+ for language in translated_languages :
494
+ if fname .startswith (language + "/" ):
495
+ selected_language = language
496
+ break
516
497
517
- for fname in get_source_files (source_path , language , languages ):
518
- if os .path .normpath (fname ) in context ["main" ]["ignore" ]:
519
- continue
498
+ context ["selected_language" ] = selected_language
499
+ if os .path .normpath (fname ) in context ["main" ]["ignore" ]:
500
+ continue
501
+
502
+ sys .stderr .write (f"Processing { fname } \n " )
503
+ dirname = os .path .dirname (fname )
504
+ os .makedirs (os .path .join (target_path , dirname ), exist_ok = True )
505
+
506
+ extension = os .path .splitext (fname )[- 1 ]
507
+ if extension in (".html" , ".md" ):
508
+ with open (os .path .join (source_path , fname ), encoding = "utf-8" ) as f :
509
+ content = f .read ()
510
+ if extension == ".md" :
511
+ body = markdown .markdown (
512
+ content , extensions = context ["main" ]["markdown_extensions" ]
513
+ )
514
+ # Apply Bootstrap's table formatting manually
515
+ # Python-Markdown doesn't let us config table attributes by hand
516
+ body = body .replace ("<table>" , '<table class="table table-bordered">' )
517
+ content = extend_base_template (body , context ["main" ]["base_template" ])
520
518
521
- sys .stderr .write (f"Processing { fname } \n " )
522
- dirname = os .path .dirname (fname )
523
- os .makedirs (os .path .join (target_path , dirname ), exist_ok = True )
524
-
525
- extension = os .path .splitext (fname )[- 1 ]
526
- if extension in (".html" , ".md" ):
527
- with open (os .path .join (source_path , fname ), encoding = "utf-8" ) as f :
528
- content = f .read ()
529
- if extension == ".md" :
530
- body = markdown .markdown (
531
- content , extensions = context ["main" ]["markdown_extensions" ]
532
- )
533
- # Apply Bootstrap's table formatting manually
534
- # Python-Markdown doesn't let us config table attributes by hand
535
- body = body .replace (
536
- "<table>" , '<table class="table table-bordered">'
537
- )
538
- content = extend_base_template (
539
- body , context ["main" ]["base_template" ]
540
- )
519
+ context ["base_url" ] = "" .join (["../" ] * os .path .normpath (fname ).count ("/" ))
520
+ if selected_language != default_language :
541
521
context ["base_url" ] = "" .join (
542
- ["../" ] * os .path .normpath (fname ).count ("/" )
543
- )
544
- content = jinja_env .from_string (content ).render (** context )
545
- fname_html = os .path .splitext (fname )[0 ] + ".html"
546
- with open (
547
- os .path .join (target_path , fname_html ), "w" , encoding = "utf-8"
548
- ) as f :
549
- f .write (content )
550
- else :
551
- shutil .copy (
552
- os .path .join (source_path , fname ), os .path .join (target_path , dirname )
522
+ ["../" ] * (os .path .normpath (fname ).count ("/" ) - 1 )
553
523
)
524
+
525
+ content = jinja_env .from_string (content ).render (** context )
526
+ fname_html = os .path .splitext (fname )[0 ] + ".html"
527
+ with open (
528
+ os .path .join (target_path , fname_html ), "w" , encoding = "utf-8"
529
+ ) as f :
530
+ f .write (content )
531
+ else :
532
+ shutil .copy (
533
+ os .path .join (source_path , fname ), os .path .join (target_path , dirname )
534
+ )
554
535
return 0
555
536
556
537
@@ -562,6 +543,5 @@ def main(
562
543
parser .add_argument (
563
544
"--target-path" , default = "build" , help = "directory where to write the output"
564
545
)
565
- parser .add_argument ("-t" , "--translations" , action = "store_true" )
566
546
args = parser .parse_args ()
567
- sys .exit (main (args .source_path , args .target_path , args . translations ))
547
+ sys .exit (main (args .source_path , args .target_path ))
0 commit comments