@@ -66,6 +66,29 @@ class Preprocessors:
66
66
anything else needed just be added with context preprocessors.
67
67
"""
68
68
69
+ @staticmethod
70
+ def process_translations (context : dict ) -> dict :
71
+ """
72
+ Download the translations from the GitHub repository and extract them.
73
+ """
74
+ base_folder = os .path .dirname (__file__ )
75
+ extract_path = os .path .join (base_folder , context ["translations" ]["source_path" ])
76
+ shutil .rmtree (extract_path , ignore_errors = True )
77
+ response = requests .get (context ["translations" ]["url" ])
78
+ if response .status_code == 200 :
79
+ with tarfile .open (None , "r:gz" , io .BytesIO (response .content )) as tar :
80
+ tar .extractall (base_folder )
81
+ else :
82
+ raise Exception (f"Failed to download translations: { response .status_code } " )
83
+
84
+ for lang in list (context ["translations" ]["languages" ].keys ())[1 :]:
85
+ shutil .rmtree (os .path .join (base_folder , "pandas" , lang ), ignore_errors = True )
86
+ shutil .move (
87
+ os .path .join (extract_path , lang ),
88
+ os .path .join (base_folder , "pandas" , lang ),
89
+ )
90
+ return context
91
+
69
92
@staticmethod
70
93
def current_year (context : dict ) -> dict :
71
94
"""
@@ -83,14 +106,38 @@ def navbar_add_info(context: dict) -> dict:
83
106
``has_subitems`` that tells which one of them every element is. It
84
107
also adds a ``slug`` field to be used as a CSS id.
85
108
"""
86
- for language in context ["translations" ]["languages" ]:
87
- for i , item in enumerate (context ["navbar" ][language ]):
88
- if not item .get ("translated" , True ):
89
- item ["target" ] = f"../{ item ['target' ]} "
90
109
91
- context ["navbar" ][language ][i ] = dict (
110
+ def update_target (item : dict , url_prefix : str ) -> None :
111
+ if item .get ("translated" , True ):
112
+ item ["target" ] = f"{ url_prefix } { item ['target' ]} "
113
+ else :
114
+ item ["target" ] = f"../{ item ['target' ]} "
115
+
116
+ context ["navbar" ] = {}
117
+ for lang in context ["translations" ]["languages" ]:
118
+ prefix = "" if lang == "en" else lang
119
+ url_prefix = "" if lang == "en" else lang + "/"
120
+ with open (
121
+ os .path .join (
122
+ context ["source_path" ], prefix , context ["main" ]["navbar_fname" ]
123
+ ),
124
+ encoding = "utf-8" ,
125
+ ) as f :
126
+ navbar_lang = yaml .safe_load (f )
127
+
128
+ context ["navbar" ][lang ] = navbar_lang ["navbar" ]
129
+ for i , item in enumerate (navbar_lang ["navbar" ]):
130
+ has_subitems = isinstance (item ["target" ], list )
131
+ if lang != "en" :
132
+ if has_subitems :
133
+ for sub_item in item ["target" ]:
134
+ update_target (sub_item , url_prefix )
135
+ else :
136
+ update_target (item , url_prefix )
137
+
138
+ context ["navbar" ][lang ][i ] = dict (
92
139
item ,
93
- has_subitems = isinstance ( item [ "target" ], list ) ,
140
+ has_subitems = has_subitems ,
94
141
slug = (item ["name" ].replace (" " , "-" ).lower ()),
95
142
)
96
143
return context
@@ -392,7 +439,7 @@ def get_callable(obj_as_str: str) -> object:
392
439
return obj
393
440
394
441
395
- def get_context (config_fname : str , navbar_fname : str , ** kwargs : dict ) -> dict :
442
+ def get_context (config_fname : str , ** kwargs : dict ) -> dict :
396
443
"""
397
444
Load the config yaml as the base context, and enrich it with the
398
445
information added by the context preprocessors defined in the file.
@@ -401,24 +448,8 @@ def get_context(config_fname: str, navbar_fname: str, **kwargs: dict) -> dict:
401
448
context = yaml .safe_load (f )
402
449
403
450
context ["source_path" ] = os .path .dirname (config_fname )
404
-
405
- default_language = context ["translations" ]["default_language" ]
406
- default_prefix = context ["translations" ]["default_prefix" ]
407
- translated_languages = context ["translations" ]["languages" ].copy ()
408
- translated_languages .pop (default_language )
409
- context ["translated_languages" ] = translated_languages
410
- download_and_extract_translations (context )
411
- navbar = {}
412
- for language in context ["translations" ]["languages" ]:
413
- prefix = default_prefix if language == default_language else language
414
- with open (
415
- os .path .join (context ["source_path" ], prefix , navbar_fname ), encoding = "utf-8"
416
- ) as f :
417
- navbar_lang = yaml .safe_load (f )
418
- navbar [language ] = navbar_lang ["navbar" ]
419
- context ["navbar" ] = navbar
420
-
421
451
context .update (kwargs )
452
+
422
453
preprocessors = (
423
454
get_callable (context_prep )
424
455
for context_prep in context ["main" ]["context_preprocessors" ]
@@ -453,27 +484,6 @@ def extend_base_template(content: str, base_template: str) -> str:
453
484
return result
454
485
455
486
456
- def download_and_extract_translations (context : dict ) -> None :
457
- """
458
- Download the translations from the GitHub repository and extract them.
459
- """
460
- base_folder = os .path .dirname (__file__ )
461
- extract_path = os .path .join (base_folder , context ["translations" ]["folder" ])
462
- shutil .rmtree (extract_path , ignore_errors = True )
463
- response = requests .get (context ["translations" ]["url" ])
464
- if response .status_code == 200 :
465
- with tarfile .open (None , "r:gz" , io .BytesIO (response .content )) as tar :
466
- tar .extractall (os .path .join (base_folder , context ["translations" ]["folder" ]))
467
- else :
468
- raise Exception (f"Failed to download translations: { response .status_code } " )
469
- for lang in context ["translated_languages" ]:
470
- shutil .rmtree (os .path .join (base_folder , "pandas" , lang ), ignore_errors = True )
471
- shutil .move (
472
- os .path .join (extract_path , context ["translations" ]["source_path" ], lang ),
473
- os .path .join (base_folder , "pandas" , lang ),
474
- )
475
-
476
-
477
487
def main (
478
488
source_path : str ,
479
489
target_path : str ,
@@ -490,22 +500,14 @@ def main(
490
500
sys .stderr .write ("Generating context...\n " )
491
501
context = get_context (
492
502
os .path .join (source_path , "config.yml" ),
493
- navbar_fname = "navbar.yml" ,
494
503
target_path = target_path ,
495
504
)
496
505
sys .stderr .write ("Context generated\n " )
497
506
498
507
templates_path = os .path .join (source_path , context ["main" ]["templates_path" ])
499
508
jinja_env = jinja2 .Environment (loader = jinja2 .FileSystemLoader (templates_path ))
500
-
501
509
for fname in get_source_files (source_path ):
502
- selected_language = context ["translations" ]["default_language" ]
503
- for language in context ["translated_languages" ]:
504
- if fname .startswith (language + "/" ):
505
- selected_language = language
506
- break
507
-
508
- context ["selected_language" ] = selected_language
510
+ context ["lang" ] = fname [0 :2 ] if fname [2 ] == "/" else "en"
509
511
if os .path .normpath (fname ) in context ["main" ]["ignore" ]:
510
512
continue
511
513
@@ -527,11 +529,6 @@ def main(
527
529
content = extend_base_template (body , context ["main" ]["base_template" ])
528
530
529
531
context ["base_url" ] = "" .join (["../" ] * os .path .normpath (fname ).count ("/" ))
530
- if selected_language != context ["translations" ]["default_language" ]:
531
- context ["base_url" ] = "" .join (
532
- ["../" ] * (os .path .normpath (fname ).count ("/" ) - 1 )
533
- )
534
-
535
532
content = jinja_env .from_string (content ).render (** context )
536
533
fname_html = os .path .splitext (fname )[0 ] + ".html"
537
534
with open (
0 commit comments