2021-05-30 21:16:53 +01:00
|
|
|
#!/usr/bin/env python3
|
2021-07-16 14:15:31 +01:00
|
|
|
# SPDX-License-Identifier: MIT
|
|
|
|
|
2023-12-21 14:17:18 +00:00
|
|
|
"""
|
2024-05-13 15:21:30 +01:00
|
|
|
A Python script to add or update the "More information" link for all translations of a page.
|
|
|
|
|
|
|
|
Note: If the current directory or one of its parents is called "tldr", the script will assume it is the tldr root, i.e., the directory that contains a clone of https://github.com/tldr-pages/tldr
|
2024-05-18 01:33:40 +01:00
|
|
|
If the script doesn't find it in the current path, the environment variable TLDR_ROOT will be used as the tldr root. Also, ensure 'git' is available.
|
2024-05-13 15:21:30 +01:00
|
|
|
|
|
|
|
Usage:
|
2024-05-18 01:33:40 +01:00
|
|
|
python3 scripts/set-more-info-link.py [-p PAGE] [-S] [-l LANGUAGE] [-s] [-n] [LINK]
|
2024-05-13 15:21:30 +01:00
|
|
|
|
|
|
|
Options:
|
|
|
|
-p, --page PAGE
|
2024-05-18 01:33:40 +01:00
|
|
|
Specify the page in the format "platform/command". This option allows setting the link for a specific page.
|
2024-05-13 15:21:30 +01:00
|
|
|
-S, --sync
|
2024-05-18 01:33:40 +01:00
|
|
|
Synchronize each translation's "More information" link (if exists) with that of the English page.
|
|
|
|
-l, --language LANGUAGE
|
|
|
|
Specify the language, a POSIX Locale Name in the form of "ll" or "ll_CC" (e.g. "fr" or "pt_BR").
|
2024-05-13 15:21:30 +01:00
|
|
|
-s, --stage
|
|
|
|
Stage modified pages (requires 'git' on $PATH and TLDR_ROOT to be a Git repository).
|
|
|
|
-n, --dry-run
|
|
|
|
Show what changes would be made without actually modifying the page.
|
2023-12-21 14:17:18 +00:00
|
|
|
|
|
|
|
Positional Argument:
|
|
|
|
LINK The link to be set as the "More information" link.
|
|
|
|
|
|
|
|
Examples:
|
|
|
|
1. Set the link for a specific page:
|
2024-05-18 01:33:40 +01:00
|
|
|
python3 scripts/set-more-info-link.py -p common/tar https://example.com
|
|
|
|
python3 scripts/set-more-info-link.py --page common/tar https://example.com
|
2023-12-21 14:17:18 +00:00
|
|
|
|
2024-05-18 01:33:40 +01:00
|
|
|
2. Read English pages and synchronize the "More information" link across translations:
|
2023-12-21 14:17:18 +00:00
|
|
|
python3 scripts/set-more-info-link.py -S
|
2024-05-13 15:21:30 +01:00
|
|
|
python3 scripts/set-more-info-link.py --sync
|
2023-12-21 14:17:18 +00:00
|
|
|
|
2024-05-18 01:33:40 +01:00
|
|
|
3. Read English pages and synchronize the "More information" link for Brazilian Portuguese pages only:
|
|
|
|
python3 scripts/set-more-info-link.py -S -l pt_BR
|
|
|
|
python3 scripts/set-more-info-link.py --sync --language pt_BR
|
|
|
|
|
|
|
|
4. Read English pages, synchronize the "More information" link across translations and stage modified pages for commit:
|
2023-12-21 14:17:18 +00:00
|
|
|
python3 scripts/set-more-info-link.py -Ss
|
|
|
|
python3 scripts/set-more-info-link.py --sync --stage
|
2024-01-02 05:53:39 +00:00
|
|
|
|
2024-05-18 01:33:40 +01:00
|
|
|
5. Show what changes would be made across translations:
|
2024-01-02 05:53:39 +00:00
|
|
|
python3 scripts/set-more-info-link.py -Sn
|
|
|
|
python3 scripts/set-more-info-link.py --sync --dry-run
|
2023-12-21 14:17:18 +00:00
|
|
|
"""
|
|
|
|
|
2021-04-01 16:54:56 +01:00
|
|
|
import re
|
2024-02-13 13:44:50 +00:00
|
|
|
from pathlib import Path
|
2024-05-18 01:33:40 +01:00
|
|
|
from _common import (
|
|
|
|
IGNORE_FILES,
|
|
|
|
Colors,
|
|
|
|
get_tldr_root,
|
|
|
|
get_pages_dir,
|
|
|
|
get_target_paths,
|
|
|
|
get_locale,
|
|
|
|
get_status,
|
|
|
|
stage,
|
|
|
|
create_colored_line,
|
|
|
|
create_argument_parser,
|
|
|
|
)
|
2021-04-01 16:54:56 +01:00
|
|
|
|
|
|
|
labels = {
|
2021-09-03 16:17:51 +01:00
|
|
|
"en": "More information:",
|
2021-10-05 22:03:46 +01:00
|
|
|
"ar": "لمزيد من التفاصيل:",
|
2021-10-20 18:25:03 +01:00
|
|
|
"bn": "আরও তথ্য পাবেন:",
|
2021-09-03 16:17:51 +01:00
|
|
|
"bs": "Više informacija:",
|
2023-10-15 21:17:23 +01:00
|
|
|
"cs": "Více informací:",
|
2022-02-19 00:55:31 +00:00
|
|
|
"ca": "Més informació:",
|
2021-09-03 16:17:51 +01:00
|
|
|
"da": "Mere information:",
|
|
|
|
"de": "Weitere Informationen:",
|
|
|
|
"es": "Más información:",
|
|
|
|
"fa": "اطلاعات بیشتر:",
|
2023-11-06 17:44:12 +00:00
|
|
|
"fi": "Lisätietoja:",
|
2021-10-07 08:37:39 +01:00
|
|
|
"fr": "Plus d'informations :",
|
2021-09-03 16:17:51 +01:00
|
|
|
"sh": "Više informacija:",
|
|
|
|
"hi": "अधिक जानकारी:",
|
|
|
|
"id": "Informasi lebih lanjut:",
|
|
|
|
"it": "Maggiori informazioni:",
|
|
|
|
"ja": "詳しくはこちら:",
|
|
|
|
"ko": "더 많은 정보:",
|
2022-10-24 13:15:57 +01:00
|
|
|
"lo": "ຂໍ້ມູນເພີ່ມເຕີມ:",
|
2021-09-03 16:17:51 +01:00
|
|
|
"ml": "കൂടുതൽ വിവരങ്ങൾ:",
|
2021-11-06 20:41:18 +00:00
|
|
|
"ne": "थप जानकारी:",
|
2021-09-03 16:17:51 +01:00
|
|
|
"nl": "Meer informatie:",
|
|
|
|
"no": "Mer informasjon:",
|
|
|
|
"pl": "Więcej informacji:",
|
|
|
|
"pt_BR": "Mais informações:",
|
|
|
|
"pt_PT": "Mais informações:",
|
2021-09-16 00:14:00 +01:00
|
|
|
"ro": "Mai multe informații:",
|
2021-09-03 16:17:51 +01:00
|
|
|
"ru": "Больше информации:",
|
2021-10-20 18:25:03 +01:00
|
|
|
"sr": "Više informacija na:",
|
2021-09-03 16:17:51 +01:00
|
|
|
"sv": "Mer information:",
|
2021-11-01 19:29:28 +00:00
|
|
|
"ta": "மேலும் விவரத்திற்கு:",
|
2022-10-02 15:34:41 +01:00
|
|
|
"th": "ข้อมูลเพิ่มเติม:",
|
2022-10-31 09:52:35 +00:00
|
|
|
"tr": "Daha fazla bilgi için:",
|
2021-10-20 18:25:03 +01:00
|
|
|
"uk": "Більше інформації:",
|
2021-12-26 15:57:58 +00:00
|
|
|
"uz": "Ko'proq malumot:",
|
2021-09-03 16:17:51 +01:00
|
|
|
"zh_TW": "更多資訊:",
|
|
|
|
"zh": "更多信息:",
|
2021-04-01 16:54:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-05-18 01:33:40 +01:00
|
|
|
def set_link(
|
|
|
|
path: Path, link: str, dry_run: bool = False, language_to_update: str = ""
|
|
|
|
) -> str:
|
2024-05-13 15:21:30 +01:00
|
|
|
"""
|
2024-05-18 01:33:40 +01:00
|
|
|
Write a "More information" link in a page to disk.
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
path (string): Path to a page
|
|
|
|
link (string): The "More information" link to insert.
|
|
|
|
dry_run (bool): Whether to perform a dry-run, i.e. only show the changes that would be made.
|
|
|
|
language_to_update (string): Optionally, the language of the translation to be updated.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
str: Execution status
|
|
|
|
"" if the page does not need an update or if the locale does not match language_to_update.
|
|
|
|
"\x1b[36mlink added"
|
|
|
|
"\x1b[34mlink updated"
|
|
|
|
"\x1b[36mlink would be added"
|
|
|
|
"\x1b[34mlink would updated"
|
2024-05-13 15:21:30 +01:00
|
|
|
"""
|
2021-04-01 16:54:56 +01:00
|
|
|
|
2024-05-18 01:33:40 +01:00
|
|
|
locale = get_locale(path)
|
|
|
|
if language_to_update != "" and locale != language_to_update:
|
|
|
|
# return empty status to indicate that no changes were made
|
|
|
|
return ""
|
2021-04-01 16:54:56 +01:00
|
|
|
|
2024-02-13 13:44:50 +00:00
|
|
|
with path.open(encoding="utf-8") as f:
|
2021-04-01 16:54:56 +01:00
|
|
|
lines = f.readlines()
|
|
|
|
|
|
|
|
desc_start = 0
|
|
|
|
desc_end = 0
|
|
|
|
|
|
|
|
# find start and end of description
|
|
|
|
for i, line in enumerate(lines):
|
2021-09-03 16:17:51 +01:00
|
|
|
if line.startswith(">") and desc_start == 0:
|
2021-04-01 16:54:56 +01:00
|
|
|
desc_start = i
|
2021-09-03 16:17:51 +01:00
|
|
|
if not lines[i + 1].startswith(">") and desc_start != 0:
|
2021-04-01 16:54:56 +01:00
|
|
|
desc_end = i
|
|
|
|
break
|
|
|
|
|
|
|
|
# build new line
|
2023-11-06 17:44:12 +00:00
|
|
|
if locale in ["bn", "hi", "ne"]:
|
2021-10-03 19:22:06 +01:00
|
|
|
new_line = f"> {labels[locale]} <{link}>।\n"
|
2023-10-04 09:32:15 +01:00
|
|
|
elif locale in ["ja", "th"]:
|
2021-10-05 21:10:14 +01:00
|
|
|
new_line = f"> {labels[locale]} <{link}>\n"
|
2023-10-04 09:32:15 +01:00
|
|
|
elif locale in ["zh", "zh_TW"]:
|
2021-10-05 21:10:14 +01:00
|
|
|
new_line = f"> {labels[locale]}<{link}>.\n"
|
2021-08-31 08:13:49 +01:00
|
|
|
else:
|
2021-09-03 16:17:51 +01:00
|
|
|
new_line = f"> {labels[locale]} <{link}>.\n"
|
2021-04-01 16:54:56 +01:00
|
|
|
|
|
|
|
if lines[desc_end] == new_line:
|
|
|
|
# return empty status to indicate that no changes were made
|
2021-09-03 16:17:51 +01:00
|
|
|
return ""
|
2021-04-01 16:54:56 +01:00
|
|
|
|
2021-09-03 16:17:51 +01:00
|
|
|
if re.search(r"^>.*<.+>", lines[desc_end]):
|
2021-04-01 16:54:56 +01:00
|
|
|
# overwrite last line
|
|
|
|
lines[desc_end] = new_line
|
2024-01-02 05:53:39 +00:00
|
|
|
action = "updated"
|
2021-04-01 16:54:56 +01:00
|
|
|
else:
|
|
|
|
# add new line
|
|
|
|
lines.insert(desc_end + 1, new_line)
|
2024-01-02 05:53:39 +00:00
|
|
|
action = "added"
|
|
|
|
|
2024-05-18 01:33:40 +01:00
|
|
|
status = get_status(action, dry_run, "link")
|
2021-04-01 16:54:56 +01:00
|
|
|
|
2024-05-18 01:33:40 +01:00
|
|
|
if not dry_run: # Only write to the path during a non-dry-run
|
2024-02-13 13:44:50 +00:00
|
|
|
with path.open("w", encoding="utf-8") as f:
|
2024-01-02 05:53:39 +00:00
|
|
|
f.writelines(lines)
|
2021-04-01 16:54:56 +01:00
|
|
|
|
|
|
|
return status
|
|
|
|
|
|
|
|
|
2024-02-13 13:44:50 +00:00
|
|
|
def get_link(path: Path) -> str:
|
2024-05-18 01:33:40 +01:00
|
|
|
"""
|
|
|
|
Determine whether the given path has a "More information" link.
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
path (Path): Path to a page
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
str: "" If the path doesn't exit or does not have a link,
|
|
|
|
otherwise return the "More information" link.
|
|
|
|
"""
|
|
|
|
|
|
|
|
if not path.exists():
|
|
|
|
return ""
|
2024-02-13 13:44:50 +00:00
|
|
|
with path.open(encoding="utf-8") as f:
|
2021-09-16 00:14:00 +01:00
|
|
|
lines = f.readlines()
|
|
|
|
|
|
|
|
desc_start = 0
|
|
|
|
desc_end = 0
|
|
|
|
|
|
|
|
# find start and end of description
|
|
|
|
for i, line in enumerate(lines):
|
|
|
|
if line.startswith(">") and desc_start == 0:
|
|
|
|
desc_start = i
|
|
|
|
if not lines[i + 1].startswith(">") and desc_start != 0:
|
|
|
|
desc_end = i
|
|
|
|
break
|
|
|
|
|
|
|
|
# match link
|
|
|
|
if re.search(r"^>.*<.+>", lines[desc_end]):
|
|
|
|
return re.search("<(.+)>", lines[desc_end]).group(1)
|
|
|
|
else:
|
|
|
|
return ""
|
|
|
|
|
|
|
|
|
2024-02-13 13:44:50 +00:00
|
|
|
def sync(
|
2024-05-18 01:33:40 +01:00
|
|
|
root: Path,
|
|
|
|
pages_dirs: list[Path],
|
|
|
|
command: str,
|
|
|
|
link: str,
|
|
|
|
dry_run: bool = False,
|
|
|
|
language_to_update: str = "",
|
|
|
|
) -> list[Path]:
|
|
|
|
"""
|
|
|
|
Synchronize a "More information" link into all translations.
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
root (Path): TLDR_ROOT
|
|
|
|
pages_dirs (list of Path's): Path's of page entry and platform, e.g. "page.fr/common".
|
|
|
|
command (str): A command like "tar".
|
|
|
|
link (str): A link like "https://example.com".
|
|
|
|
dry_run (bool): Whether to perform a dry-run, i.e. only show the changes that would be made.
|
|
|
|
language_to_update (str): Optionally, the language of the translation to be updated.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
list (list of Path's): A list of Path's to be staged into git, using by --stage option.
|
|
|
|
"""
|
2024-02-13 13:44:50 +00:00
|
|
|
paths = []
|
2021-09-16 00:14:00 +01:00
|
|
|
for page_dir in pages_dirs:
|
2024-02-13 13:44:50 +00:00
|
|
|
path = root / page_dir / command
|
|
|
|
if path.exists():
|
2024-05-18 01:33:40 +01:00
|
|
|
status = set_link(path, link, dry_run, language_to_update)
|
2021-09-16 00:14:00 +01:00
|
|
|
if status != "":
|
2024-05-18 01:33:40 +01:00
|
|
|
rel_path = "/".join(path.parts[-3:])
|
|
|
|
paths.append(rel_path)
|
|
|
|
print(create_colored_line(Colors.GREEN, f"{rel_path} {status}"))
|
2024-02-13 13:44:50 +00:00
|
|
|
return paths
|
2021-09-16 00:14:00 +01:00
|
|
|
|
|
|
|
|
2021-04-01 16:54:56 +01:00
|
|
|
def main():
|
2024-05-18 01:33:40 +01:00
|
|
|
parser = create_argument_parser(
|
|
|
|
'Sets the "More information" link for all translations of a page'
|
2024-01-02 05:53:39 +00:00
|
|
|
)
|
2021-09-16 00:14:00 +01:00
|
|
|
parser.add_argument("link", type=str, nargs="?", default="")
|
2021-04-01 16:54:56 +01:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
root = get_tldr_root()
|
2024-05-18 01:33:40 +01:00
|
|
|
pages_dirs = get_pages_dir(root)
|
2021-04-01 16:54:56 +01:00
|
|
|
|
2024-02-13 13:44:50 +00:00
|
|
|
target_paths = []
|
2021-04-01 16:54:56 +01:00
|
|
|
|
2021-09-16 00:14:00 +01:00
|
|
|
# Use '--page' option
|
|
|
|
if args.page != "":
|
2024-05-18 01:33:40 +01:00
|
|
|
target_paths += get_target_paths(args.page, pages_dirs)
|
2021-09-16 00:14:00 +01:00
|
|
|
|
|
|
|
for path in target_paths:
|
2024-02-13 13:44:50 +00:00
|
|
|
rel_path = "/".join(path.parts[-3:])
|
2024-05-18 01:33:40 +01:00
|
|
|
status = set_link(path, args.link, args.dry_run, args.language)
|
2021-09-16 00:14:00 +01:00
|
|
|
if status != "":
|
2024-05-18 01:33:40 +01:00
|
|
|
print(create_colored_line(Colors.GREEN, f"{rel_path} {status}"))
|
2021-09-16 00:14:00 +01:00
|
|
|
|
|
|
|
# Use '--sync' option
|
|
|
|
elif args.sync:
|
2024-02-13 13:44:50 +00:00
|
|
|
pages_dirs.remove(root / "pages")
|
|
|
|
en_path = root / "pages"
|
|
|
|
platforms = [i.name for i in en_path.iterdir() if i.name not in IGNORE_FILES]
|
2021-04-01 16:54:56 +01:00
|
|
|
for platform in platforms:
|
2024-02-13 13:44:50 +00:00
|
|
|
platform_path = en_path / platform
|
2021-09-16 00:14:00 +01:00
|
|
|
commands = [
|
2024-02-13 13:44:50 +00:00
|
|
|
f"{platform}/{page.name}"
|
|
|
|
for page in platform_path.iterdir()
|
2024-05-18 01:33:40 +01:00
|
|
|
if page.name not in IGNORE_FILES
|
2021-09-16 00:14:00 +01:00
|
|
|
]
|
|
|
|
for command in commands:
|
2024-02-13 13:44:50 +00:00
|
|
|
link = get_link(root / "pages" / command)
|
2021-09-16 00:14:00 +01:00
|
|
|
if link != "":
|
2024-05-18 01:33:40 +01:00
|
|
|
target_paths += sync(
|
|
|
|
root, pages_dirs, command, link, args.dry_run, args.language
|
|
|
|
)
|
2021-04-01 16:54:56 +01:00
|
|
|
|
2024-05-18 01:33:40 +01:00
|
|
|
# Use '--stage' option
|
2024-02-13 13:44:50 +00:00
|
|
|
if args.stage and not args.dry_run and len(target_paths) > 0:
|
2024-05-18 01:33:40 +01:00
|
|
|
stage(target_paths)
|
2021-04-01 16:54:56 +01:00
|
|
|
|
|
|
|
|
2021-09-03 16:17:51 +01:00
|
|
|
if __name__ == "__main__":
|
2021-04-01 16:54:56 +01:00
|
|
|
main()
|