Skip to content

Commit 5245445

Browse files
committed
Split preprocessor logic and more code review changes
1 parent ac0b8f0 commit 5245445

File tree

3 files changed

+64
-38
lines changed

3 files changed

+64
-38
lines changed

.gitignore

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -141,12 +141,3 @@ doc/source/savefig/
141141
# Pyodide/WASM related files #
142142
##############################
143143
/.pyodide-xbuildenv-*
144-
145-
146-
# Web & Translations #
147-
##############################
148-
web/preview/
149-
web/pandas-translations-main/
150-
web/pandas/es/
151-
web/pandas/pt/
152-
web/pandas/fr/

web/pandas/config.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ main:
88
- config.yml
99
github_repo_url: pandas-dev/pandas
1010
context_preprocessors:
11-
- pandas_web.Preprocessors.process_translations
1211
- pandas_web.Preprocessors.current_year
12+
- pandas_web.Preprocessors.download_translated_content
13+
- pandas_web.Preprocessors.add_navbar_content
1314
- pandas_web.Preprocessors.navbar_add_info
15+
- pandas_web.Preprocessors.navbar_add_translated_info
1416
- pandas_web.Preprocessors.blog_add_posts
1517
- pandas_web.Preprocessors.maintainers_add_info
1618
- pandas_web.Preprocessors.home_add_releases

web/pandas_web.py

Lines changed: 61 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,25 @@ class Preprocessors:
6767
"""
6868

6969
@staticmethod
70-
def process_translations(context: dict) -> dict:
70+
def current_year(context: dict) -> dict:
71+
"""
72+
Add the current year to the context, so it can be used for the copyright
73+
note, or other places where it is needed.
74+
"""
75+
context["current_year"] = datetime.datetime.now().year
76+
return context
77+
78+
@staticmethod
79+
def download_translated_content(context: dict) -> dict:
7180
"""
72-
Download the translations from the GitHub repository and extract them.
81+
Download the translations from the mirror translations repository.
82+
https://github.com/Scientific-Python-Translations/pandas-translations
83+
84+
All translated languages are downloaded, extracted and place inside the
85+
``pandas`` folder in a separate folder for each language (e.g. ``pandas/es``).
86+
87+
The extracted folder and the translations folders are deleted before
88+
downloading the information, so the translations are always up to date.
7389
"""
7490
base_folder = os.path.dirname(__file__)
7591
extract_path = os.path.join(base_folder, context["translations"]["source_path"])
@@ -90,50 +106,67 @@ def process_translations(context: dict) -> dict:
90106
return context
91107

92108
@staticmethod
93-
def current_year(context: dict) -> dict:
109+
def add_navbar_content(context: dict) -> dict:
94110
"""
95-
Add the current year to the context, so it can be used for the copyright
96-
note, or other places where it is needed.
111+
Add the navbar content to the context.
112+
113+
The navbar content is loaded for all available languages.
97114
"""
98-
context["current_year"] = datetime.datetime.now().year
115+
context["navbar"] = {}
116+
for lang in context["translations"]["languages"]:
117+
with open(
118+
os.path.join(
119+
context["source_path"],
120+
"" if lang == "en" else f"{lang}",
121+
context["main"]["navbar_fname"],
122+
),
123+
encoding="utf-8",
124+
) as f:
125+
navbar_lang = yaml.safe_load(f)
126+
context["navbar"][lang] = navbar_lang["navbar"]
99127
return context
100128

101129
@staticmethod
102130
def navbar_add_info(context: dict) -> dict:
103131
"""
132+
Items in the main navigation bar can be direct links, or dropdowns with
133+
subitems. This context preprocessor adds a boolean field
134+
``has_subitems`` that tells which one of them every element is. It
135+
also adds a ``slug`` field to be used as a CSS id.
136+
"""
137+
for i, item in enumerate(context["navbar"]["en"]):
138+
context["navbar"]["en"][i] = dict(
139+
item,
140+
has_subitems=isinstance(item["target"], list),
141+
slug=(item["name"].replace(" ", "-").lower()),
142+
)
143+
return context
144+
145+
@staticmethod
146+
def navbar_add_translated_info(context: dict) -> dict:
147+
"""
148+
Prepare the translated navbar information for the template.
149+
104150
Items in the main navigation bar can be direct links, or dropdowns with
105151
subitems. This context preprocessor adds a boolean field
106152
``has_subitems`` that tells which one of them every element is. It
107153
also adds a ``slug`` field to be used as a CSS id.
108154
"""
109155

110-
def update_target(item: dict, url_prefix: str) -> None:
156+
def update_target(item: dict, prefix: str) -> None:
111157
if item.get("translated", True):
112-
item["target"] = f"{url_prefix}{item['target']}"
158+
item["target"] = f"{prefix}/{item['target']}"
113159
else:
114160
item["target"] = f"../{item['target']}"
115161

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"]):
162+
for lang in list(context["translations"]["languages"].keys())[1:]:
163+
for i, item in enumerate(context["navbar"][lang]):
130164
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)
165+
if has_subitems:
166+
for sub_item in item["target"]:
167+
update_target(sub_item, lang)
168+
else:
169+
update_target(item, lang)
137170

138171
context["navbar"][lang][i] = dict(
139172
item,

0 commit comments

Comments
 (0)