From 15f033abbc48a70da8694c8489ab5cc40bbe63d5 Mon Sep 17 00:00:00 2001 From: Managor <42655600+Managor@users.noreply.github.com> Date: Mon, 15 Dec 2025 06:50:54 +0200 Subject: [PATCH] scripts/set-more-info-link.py: use the template files to generate a more info link (#19995) --- scripts/_common.py | 45 ++++++++++++++++++++++ scripts/set-alias-page.py | 47 +---------------------- scripts/set-more-info-link.py | 72 +++++++++++------------------------ 3 files changed, 70 insertions(+), 94 deletions(-) diff --git a/scripts/_common.py b/scripts/_common.py index 775b2f6067..8715488924 100644 --- a/scripts/_common.py +++ b/scripts/_common.py @@ -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. diff --git a/scripts/set-alias-page.py b/scripts/set-alias-page.py index 8d7c6b047b..2ae199330d 100755 --- a/scripts/set-alias-page.py +++ b/scripts/set-alias-page.py @@ -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( diff --git a/scripts/set-more-info-link.py b/scripts/set-more-info-link.py index f1118c3a90..43d27972ca 100755 --- a/scripts/set-more-info-link.py +++ b/scripts/set-more-info-link.py @@ -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 = []