2021-05-30 21:16:53 +01:00
|
|
|
#!/usr/bin/env python3
|
2021-07-16 14:15:31 +01:00
|
|
|
# SPDX-License-Identifier: MIT
|
|
|
|
|
2021-04-01 16:54:56 +01:00
|
|
|
import argparse
|
|
|
|
import os
|
|
|
|
import re
|
|
|
|
import subprocess
|
|
|
|
import sys
|
|
|
|
|
|
|
|
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:",
|
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": "اطلاعات بیشتر:",
|
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
|
|
|
|
|
|
|
|
|
|
|
def get_tldr_root():
|
|
|
|
# if this script is running from tldr/scripts, the parent's parent is the root
|
|
|
|
f = os.path.normpath(__file__)
|
2021-09-03 16:17:51 +01:00
|
|
|
if f.endswith("tldr/scripts/set-more-info-link.py"):
|
2021-04-01 16:54:56 +01:00
|
|
|
return os.path.dirname(os.path.dirname(f))
|
|
|
|
|
2021-09-03 16:17:51 +01:00
|
|
|
if "TLDR_ROOT" in os.environ:
|
|
|
|
return os.environ["TLDR_ROOT"]
|
2021-04-01 16:54:56 +01:00
|
|
|
else:
|
|
|
|
print(
|
2021-09-03 16:17:51 +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
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
|
|
|
|
def set_link(file, link):
|
|
|
|
with open(file) as f:
|
|
|
|
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
|
|
|
|
pages_dir = os.path.basename(os.path.dirname(os.path.dirname(file)))
|
2021-09-03 16:17:51 +01:00
|
|
|
if "." in pages_dir:
|
|
|
|
_, locale = pages_dir.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
|
2021-10-05 21:10:14 +01:00
|
|
|
if locale == "hi":
|
2021-10-03 19:22:06 +01:00
|
|
|
new_line = f"> {labels[locale]} <{link}>।\n"
|
2021-10-05 21:10:14 +01:00
|
|
|
elif locale == "ja":
|
|
|
|
new_line = f"> {labels[locale]} <{link}>\n"
|
|
|
|
elif locale == "zh" or locale == "zh_TW":
|
|
|
|
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
|
2021-09-03 16:17:51 +01:00
|
|
|
status = "\x1b[34mlink updated"
|
2021-04-01 16:54:56 +01:00
|
|
|
else:
|
|
|
|
# add new line
|
|
|
|
lines.insert(desc_end + 1, new_line)
|
2021-09-03 16:17:51 +01:00
|
|
|
status = "\x1b[36mlink added"
|
2021-04-01 16:54:56 +01:00
|
|
|
|
2021-09-03 16:17:51 +01:00
|
|
|
with open(file, "w") as f:
|
2021-04-01 16:54:56 +01:00
|
|
|
f.writelines(lines)
|
|
|
|
|
|
|
|
return status
|
|
|
|
|
|
|
|
|
2021-09-16 00:14:00 +01:00
|
|
|
def get_link(file):
|
|
|
|
with open(file) as f:
|
|
|
|
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 ""
|
|
|
|
|
|
|
|
|
|
|
|
def sync(root, pages_dirs, command, link):
|
|
|
|
rel_paths = []
|
|
|
|
for page_dir in pages_dirs:
|
|
|
|
path = os.path.join(root, page_dir, command)
|
|
|
|
if os.path.exists(path):
|
|
|
|
rel_path = path.replace(f"{root}/", "")
|
|
|
|
rel_paths.append(rel_path)
|
|
|
|
status = set_link(path, link)
|
|
|
|
if status != "":
|
|
|
|
print(f"\x1b[32m{rel_path} {status}\x1b[0m")
|
|
|
|
return rel_paths
|
|
|
|
|
|
|
|
|
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",
|
|
|
|
)
|
|
|
|
parser.add_argument("link", type=str, nargs="?", default="")
|
2021-04-01 16:54:56 +01:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
root = get_tldr_root()
|
2021-09-03 16:17:51 +01:00
|
|
|
pages_dirs = [d for d in os.listdir(root) if d.startswith("pages")]
|
2021-04-01 16:54:56 +01:00
|
|
|
|
|
|
|
rel_paths = []
|
|
|
|
|
2021-09-16 00:14:00 +01:00
|
|
|
# Use '--page' option
|
|
|
|
if args.page != "":
|
|
|
|
target_paths = []
|
|
|
|
|
|
|
|
if not args.page.lower().endswith(".md"):
|
|
|
|
args.page = f"{args.page}.md"
|
|
|
|
|
|
|
|
for pages_dir in pages_dirs:
|
|
|
|
pages_dir_path = os.path.join(root, pages_dir)
|
|
|
|
platforms = [i for i in os.listdir(pages_dir_path) if i not in IGNORE_FILES]
|
|
|
|
for platform in platforms:
|
|
|
|
platform_path = os.path.join(pages_dir_path, platform)
|
|
|
|
commands = [
|
|
|
|
f"{platform}/{p}"
|
|
|
|
for p in os.listdir(platform_path)
|
|
|
|
if p not in IGNORE_FILES
|
|
|
|
]
|
|
|
|
if args.page in commands:
|
|
|
|
path = os.path.join(pages_dir_path, args.page)
|
|
|
|
target_paths.append(path)
|
|
|
|
|
|
|
|
target_paths.sort()
|
|
|
|
|
|
|
|
for path in target_paths:
|
|
|
|
rel_path = path.replace(f"{root}/", "")
|
|
|
|
rel_paths.append(rel_path)
|
|
|
|
status = set_link(path, args.link)
|
|
|
|
if status != "":
|
|
|
|
print(f"\x1b[32m{rel_path} {status}\x1b[0m")
|
|
|
|
|
|
|
|
# Use '--sync' option
|
|
|
|
elif args.sync:
|
|
|
|
pages_dirs.remove("pages")
|
|
|
|
en_page = os.path.join(root, "pages")
|
|
|
|
platforms = [i for i in os.listdir(en_page) if i not in IGNORE_FILES]
|
2021-04-01 16:54:56 +01:00
|
|
|
for platform in platforms:
|
2021-09-16 00:14:00 +01:00
|
|
|
platform_path = os.path.join(en_page, platform)
|
|
|
|
commands = [
|
|
|
|
f"{platform}/{p}"
|
|
|
|
for p in os.listdir(platform_path)
|
|
|
|
if p not in IGNORE_FILES
|
|
|
|
]
|
|
|
|
for command in commands:
|
|
|
|
link = get_link(os.path.join(root, "pages", command))
|
|
|
|
if link != "":
|
|
|
|
rel_paths += sync(root, pages_dirs, command, link)
|
2021-04-01 16:54:56 +01:00
|
|
|
|
|
|
|
if args.stage:
|
2021-09-03 16:17:51 +01:00
|
|
|
subprocess.call(["git", "add", *rel_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()
|