scripts/set-more-info-link.py: use the template files to generate a more info link (#19995)

This commit is contained in:
Managor
2025-12-15 06:50:54 +02:00
committed by GitHub
parent 7bdd254153
commit 15f033abbc
3 changed files with 70 additions and 94 deletions
+45
View File
@@ -91,6 +91,51 @@ def test_get_tldr_root():
os.environ["TLDR_ROOT"] = original_env
def get_templates(root: Path, filename: str):
"""
Get all more information line translation templates from
TLDR_ROOT/contributing-guides/translation-templates/filename.
Parameters:
root (Path): The path of local tldr repository, i.e., TLDR_ROOT.
filename (str): Specifies which template to fetch.
Returns:
dict of (str, str): Language labels map to alias page templates.
"""
template_file = root / "contributing-guides/translation-templates" / filename
with template_file.open(encoding="utf-8") as f:
lines = f.readlines()
# Parse alias-pages.md
templates = {}
i = 0
while i < len(lines):
if lines[i].startswith("###"):
lang = lines[i][4:].strip("\n").strip(" ")
while True:
i += 1
if lines[i].startswith("Not translated yet."):
is_translated = False
break
elif lines[i].startswith("```markdown"):
i += 1
is_translated = True
break
if is_translated:
text = ""
while not lines[i].startswith("```"):
text += lines[i]
i += 1
templates[lang] = text
i += 1
return templates
def get_pages_dirs(root: Path) -> list[Path]:
"""
Get pages directories for all languages.
+2 -45
View File
@@ -63,6 +63,7 @@ from _common import (
IGNORE_FILES,
Colors,
get_tldr_root,
get_templates,
get_pages_dirs,
get_target_paths,
get_locale,
@@ -115,50 +116,6 @@ def test_ignore_files():
assert "tldr.md" in IGNORE_FILES
def get_templates(root: Path):
"""
Get all alias page translation templates from
TLDR_ROOT/contributing-guides/translation-templates/alias-pages.md.
Parameters:
root (Path): The path of local tldr repository, i.e., TLDR_ROOT.
Returns:
dict of (str, str): Language labels map to alias page templates.
"""
template_file = root / "contributing-guides/translation-templates/alias-pages.md"
with template_file.open(encoding="utf-8") as f:
lines = f.readlines()
# Parse alias-pages.md
templates = {}
i = 0
while i < len(lines):
if lines[i].startswith("###"):
lang = lines[i][4:].strip("\n").strip(" ")
while True:
i = i + 1
if lines[i].startswith("Not translated yet."):
is_translated = False
break
elif lines[i].startswith("```markdown"):
i = i + 1
is_translated = True
break
if is_translated:
text = ""
while not lines[i].startswith("```"):
text += lines[i]
i = i + 1
templates[lang] = text
i = i + 1
return templates
def generate_alias_page_content(
template_content: str,
page_content: AliasPageContent,
@@ -488,7 +445,7 @@ def main():
root = get_tldr_root()
pages_dirs = get_pages_dirs(root)
templates = get_templates(root)
templates = get_templates(root, "alias-pages.md")
global config
config = Config(
+23 -49
View File
@@ -50,10 +50,12 @@ Examples:
import re
import sys
from pathlib import Path
from dataclasses import dataclass
from _common import (
IGNORE_FILES,
Colors,
get_tldr_root,
get_templates,
get_pages_dirs,
get_target_paths,
get_locale,
@@ -63,44 +65,16 @@ from _common import (
create_argument_parser,
)
labels = {
"en": "More information:",
"ar": "لمزيد من التفاصيل:",
"bn": "আরও তথ্য পাবেন:",
"bs": "Više informacija:",
"ca": "Més informació:",
"cs": "Více informací:",
"da": "Mere information:",
"de": "Weitere Informationen:",
"es": "Más información:",
"fa": "اطلاعات بیشتر:",
"fi": "Lisätietoa:",
"fr": "Plus d'informations :",
"hi": "अधिक जानकारी:",
"id": "Informasi lebih lanjut:",
"it": "Maggiori informazioni:",
"ja": "もっと詳しく:",
"ko": "더 많은 정보:",
"lo": "ຂໍ້ມູນເພີ່ມເຕີມ:",
"ml": "കൂടുതൽ വിവരങ്ങൾ:",
"ne": "थप जानकारी:",
"nl": "Meer informatie:",
"no": "Mer informasjon:",
"pl": "Więcej informacji:",
"pt_BR": "Mais informações:",
"pt_PT": "Mais informações:",
"ro": "Mai multe informații:",
"ru": "Больше информации:",
"sr": "Više informacija na:",
"sv": "Mer information:",
"ta": "மேலும் விவரத்திற்கு:",
"th": "ข้อมูลเพิ่มเติม:",
"tr": "Daha fazla bilgi için:",
"uk": "Більше інформації:",
"uz": "Ko'proq malumot:",
"zh_TW": "更多資訊:",
"zh": "更多信息:",
}
@dataclass
class Config:
"""Global configuration for the script"""
root: Path
pages_dirs: list[Path]
templates: dict[str, str]
dry_run: bool = False
language: str = ""
def set_link(
@@ -143,17 +117,7 @@ def set_link(
desc_end = i
break
# build new line
if locale in ["bn", "hi", "ne"]:
new_line = f"> {labels[locale]} <{link}>।\n"
elif locale in ["ja"]:
new_line = f"> {labels[locale]} <{link}>。\n"
elif locale in ["th"]:
new_line = f"> {labels[locale]} <{link}>\n"
elif locale in ["zh", "zh_TW"]:
new_line = f"> {labels[locale]}<{link}>.\n"
else:
new_line = f"> {labels[locale]} <{link}>.\n"
new_line = config.templates[locale].replace("https://example.com", link)
if lines[desc_end] == new_line:
# return empty status to indicate that no changes were made
@@ -260,6 +224,16 @@ def main():
root = get_tldr_root()
pages_dirs = get_pages_dirs(root)
templates = get_templates(root, "more-info-link.md")
global config
config = Config(
root=root,
pages_dirs=pages_dirs,
templates=templates,
dry_run=args.dry_run,
language=args.language,
)
target_paths = []