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
|
|
|
"""
|
|
|
|
This script sets the "More information" link for all translations of a page.
|
|
|
|
It can be used to add or update the links in translations.
|
|
|
|
|
|
|
|
Note: Before running this script, ensure that TLDR_ROOT is set to the location
|
|
|
|
of a clone of https://github.com/tldr-pages/tldr, and 'git' is available.
|
|
|
|
If there is a symlink error when using the stage flag remove the `pages.en`
|
|
|
|
directory temporarily and try executing it again.
|
|
|
|
|
2024-01-02 05:53:39 +00:00
|
|
|
Usage: python3 scripts/set-more-info-link.py [-p PAGE] [-s] [-S] [-n] [LINK]
|
2023-12-21 14:17:18 +00:00
|
|
|
|
|
|
|
Supported Arguments:
|
|
|
|
-p, --page Specify the page name in the format "platform/command.md".
|
|
|
|
This option allows setting the link for a specific page.
|
|
|
|
-s, --stage Stage modified pages for commit. This option requires 'git'
|
|
|
|
to be on the $PATH and TLDR_ROOT to be a Git repository.
|
|
|
|
-S, --sync Synchronize each translation's more information link (if
|
|
|
|
exists) with that of the English page. This is useful to
|
|
|
|
ensure consistency across translations.
|
2024-01-02 05:53:39 +00:00
|
|
|
-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:
|
|
|
|
python3 scripts/set-more-info-link.py -p common/tar.md https://example.com
|
|
|
|
|
|
|
|
2. Synchronize more information links across translations:
|
|
|
|
python3 scripts/set-more-info-link.py -S
|
|
|
|
|
|
|
|
3. Synchronize more information links across translations and stage modified pages for commit:
|
|
|
|
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
|
|
|
|
|
|
|
4. Show what changes would be made across translations:
|
|
|
|
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 argparse
|
|
|
|
import os
|
|
|
|
import re
|
|
|
|
import subprocess
|
2024-02-13 13:44:50 +00:00
|
|
|
from pathlib import Path
|
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
|
|
|
}
|
|
|
|
|
2021-09-03 16:17:51 +01:00
|
|
|
IGNORE_FILES = (".DS_Store",)
|
2021-04-01 16:54:56 +01:00
|
|
|
|
|
|
|
|
2024-02-13 13:44:50 +00:00
|
|
|
def get_tldr_root() -> Path:
|
|
|
|
f = Path(__file__).resolve()
|
|
|
|
return next(path for path in f.parents if path.name == "tldr")
|
2021-04-01 16:54:56 +01:00
|
|
|
|
2021-09-03 16:17:51 +01:00
|
|
|
if "TLDR_ROOT" in os.environ:
|
2024-02-13 13:44:50 +00:00
|
|
|
return Path(os.environ["TLDR_ROOT"])
|
|
|
|
raise SystemError(
|
2023-10-04 09:32:15 +01:00
|
|
|
"\x1b[31mPlease set TLDR_ROOT to the location of a clone of https://github.com/tldr-pages/tldr."
|
|
|
|
)
|
2021-04-01 16:54:56 +01:00
|
|
|
|
|
|
|
|
2024-02-13 13:44:50 +00:00
|
|
|
def set_link(path: Path, link: str, dry_run=False) -> str:
|
|
|
|
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
|
|
|
|
|
|
|
|
# compute locale
|
2024-02-13 13:44:50 +00:00
|
|
|
pages_dirname = path.parents[1].name
|
|
|
|
if "." in pages_dirname:
|
|
|
|
_, locale = pages_dirname.split(".")
|
2021-04-01 16:54:56 +01:00
|
|
|
else:
|
2021-09-03 16:17:51 +01:00
|
|
|
locale = "en"
|
2021-04-01 16:54:56 +01:00
|
|
|
|
|
|
|
# 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
|
|
|
|
2024-01-02 05:53:39 +00:00
|
|
|
status_prefix = "\x1b[36m" # Color code for pages
|
|
|
|
|
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
|
|
|
status_prefix = "\x1b[34m"
|
|
|
|
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
|
|
|
status_prefix = "\x1b[36m"
|
|
|
|
action = "added"
|
|
|
|
|
|
|
|
if dry_run:
|
|
|
|
status = f"link will be {action}"
|
|
|
|
else:
|
|
|
|
status = f"link {action}"
|
|
|
|
|
|
|
|
status = f"{status_prefix}{status}\x1b[0m"
|
2021-04-01 16:54:56 +01:00
|
|
|
|
2024-01-02 05:53:39 +00:00
|
|
|
if not 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:
|
|
|
|
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(
|
|
|
|
root: Path, pages_dirs: list[str], command: str, link: str, dry_run=False
|
|
|
|
) -> list[str]:
|
|
|
|
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():
|
|
|
|
rel_path = "/".join(path.parts[-3:])
|
2024-01-02 05:53:39 +00:00
|
|
|
status = set_link(path, link, dry_run)
|
2021-09-16 00:14:00 +01:00
|
|
|
if status != "":
|
2024-02-13 13:44:50 +00:00
|
|
|
paths.append(path)
|
2021-09-16 00:14:00 +01:00
|
|
|
print(f"\x1b[32m{rel_path} {status}\x1b[0m")
|
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():
|
|
|
|
parser = argparse.ArgumentParser(
|
2021-09-03 16:17:51 +01:00
|
|
|
description='Sets the "More information" link for all translations of a page'
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"-p",
|
|
|
|
"--page",
|
|
|
|
type=str,
|
2021-09-16 00:14:00 +01:00
|
|
|
required=False,
|
|
|
|
default="",
|
2021-09-03 16:17:51 +01:00
|
|
|
help='page name in the format "platform/command.md"',
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"-s",
|
|
|
|
"--stage",
|
|
|
|
action="store_true",
|
|
|
|
default=False,
|
|
|
|
help="stage modified pages (requires `git` to be on $PATH and TLDR_ROOT to be a Git repository)",
|
|
|
|
)
|
2021-09-16 00:14:00 +01:00
|
|
|
parser.add_argument(
|
|
|
|
"-S",
|
|
|
|
"--sync",
|
|
|
|
action="store_true",
|
|
|
|
default=False,
|
|
|
|
help="synchronize each translation's more information link (if exists) with that of English page",
|
|
|
|
)
|
2024-01-02 05:53:39 +00:00
|
|
|
parser.add_argument(
|
|
|
|
"-n",
|
|
|
|
"--dry-run",
|
|
|
|
action="store_true",
|
|
|
|
default=False,
|
|
|
|
help="show what changes would be made without actually modifying the pages",
|
|
|
|
)
|
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-02-13 13:44:50 +00:00
|
|
|
pages_dirs = [d for d in root.iterdir() if d.name.startswith("pages")]
|
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 != "":
|
|
|
|
if not args.page.lower().endswith(".md"):
|
|
|
|
args.page = f"{args.page}.md"
|
2024-02-13 13:44:50 +00:00
|
|
|
arg_platform, arg_page = args.page.split("/")
|
2021-09-16 00:14:00 +01:00
|
|
|
|
|
|
|
for pages_dir in pages_dirs:
|
2024-02-13 13:44:50 +00:00
|
|
|
page_path = pages_dir / arg_platform / arg_page
|
|
|
|
if not page_path.exists():
|
|
|
|
continue
|
|
|
|
target_paths.append(page_path)
|
2021-09-16 00:14:00 +01:00
|
|
|
|
|
|
|
target_paths.sort()
|
|
|
|
|
|
|
|
for path in target_paths:
|
2024-02-13 13:44:50 +00:00
|
|
|
rel_path = "/".join(path.parts[-3:])
|
2021-09-16 00:14:00 +01:00
|
|
|
status = set_link(path, args.link)
|
|
|
|
if status != "":
|
|
|
|
print(f"\x1b[32m{rel_path} {status}\x1b[0m")
|
|
|
|
|
|
|
|
# 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()
|
|
|
|
if page 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-02-13 13:44:50 +00:00
|
|
|
target_paths += sync(root, pages_dirs, command, link, args.dry_run)
|
2021-04-01 16:54:56 +01:00
|
|
|
|
2024-02-13 13:44:50 +00:00
|
|
|
if args.stage and not args.dry_run and len(target_paths) > 0:
|
|
|
|
subprocess.call(["git", "add", *target_paths], cwd=root)
|
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()
|