2024-05-09 12:24:53 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# SPDX-License-Identifier: MIT
|
|
|
|
|
|
|
|
"""
|
2024-05-13 15:21:30 +01:00
|
|
|
A Python script to add or update the page title for all translations of a page.
|
2024-05-09 12:24:53 +01:00
|
|
|
|
|
|
|
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-09 12:24:53 +01:00
|
|
|
|
2024-05-13 15:21:30 +01:00
|
|
|
Usage:
|
2024-05-18 01:33:40 +01:00
|
|
|
python3 scripts/set-page-title.py [-p PAGE] [-S] [-l LANGUAGE] [-s] [-n] [TITLE]
|
2024-05-09 12:24:53 +01:00
|
|
|
|
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 title for a specific page.
|
2024-05-13 15:21:30 +01:00
|
|
|
-S, --sync
|
|
|
|
Synchronize each translation's title (if exists) with that of the English page.
|
2024-05-18 01:33:40 +01:00
|
|
|
-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.
|
2024-05-09 12:24:53 +01:00
|
|
|
|
|
|
|
Positional Argument:
|
|
|
|
TITLE The title to be set as the title.
|
|
|
|
|
|
|
|
Examples:
|
|
|
|
1. Set the title for a specific page:
|
2024-05-18 01:33:40 +01:00
|
|
|
python3 scripts/set-page-title.py -p common/tar tar
|
|
|
|
python3 scripts/set-page-title.py --page common/tar tar
|
2024-05-09 12:24:53 +01:00
|
|
|
|
|
|
|
2. Synchronize titles across translations:
|
|
|
|
python3 scripts/set-page-title.py -S
|
2024-05-13 15:21:30 +01:00
|
|
|
python3 scripts/set-page-title.py --sync
|
2024-05-09 12:24:53 +01:00
|
|
|
|
2024-05-18 01:33:40 +01:00
|
|
|
3. Read English pages and synchronize the title for Brazilian Portuguese pages only:
|
|
|
|
python3 scripts/set-page-title.py -S -l pt_BR
|
|
|
|
python3 scripts/set-page-title.py --sync --language pt_BR
|
|
|
|
|
|
|
|
4. Synchronize titles across translations and stage modified pages for commit:
|
2024-05-09 12:24:53 +01:00
|
|
|
python3 scripts/set-page-title.py -Ss
|
|
|
|
python3 scripts/set-page-title.py --sync --stage
|
|
|
|
|
2024-05-18 01:33:40 +01:00
|
|
|
5. Show what changes would be made across translations:
|
2024-05-09 12:24:53 +01:00
|
|
|
python3 scripts/set-page-title.py -Sn
|
|
|
|
python3 scripts/set-page-title.py --sync --dry-run
|
|
|
|
"""
|
|
|
|
|
|
|
|
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,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def set_page_title(
|
|
|
|
path: Path, title: 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 title in a page to disk.
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
path (string): Path to a page
|
|
|
|
title (string): The title 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[36mtitle added"
|
|
|
|
"\x1b[34mtitle updated"
|
|
|
|
"\x1b[36mtitle would be added"
|
|
|
|
"\x1b[34mtitle would updated"
|
2024-05-13 15:21:30 +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 ""
|
2024-05-09 12:24:53 +01:00
|
|
|
|
|
|
|
new_line = f"# {title}\n"
|
|
|
|
|
|
|
|
# Read the content of the Markdown file
|
|
|
|
with path.open(encoding="utf-8") as f:
|
|
|
|
lines = f.readlines()
|
|
|
|
|
|
|
|
if lines[0] == new_line:
|
|
|
|
# return empty status to indicate that no changes were made
|
|
|
|
return ""
|
|
|
|
|
2024-05-18 01:33:40 +01:00
|
|
|
status = get_status("updated", dry_run, "title")
|
2024-05-09 12:24:53 +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-05-09 12:24:53 +01:00
|
|
|
lines[0] = new_line
|
|
|
|
with path.open("w", encoding="utf-8") as f:
|
|
|
|
f.writelines(lines)
|
|
|
|
|
|
|
|
return status
|
|
|
|
|
|
|
|
|
2024-05-18 01:33:40 +01:00
|
|
|
def get_page_title(path: Path) -> str:
|
|
|
|
"""
|
|
|
|
Determine whether the given path has a title.
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
path (Path): Path to a page
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
str: "" If the path doesn't exit or does not have a title,
|
|
|
|
otherwise return the page title.
|
|
|
|
"""
|
|
|
|
|
|
|
|
if not path.exists():
|
|
|
|
return ""
|
2024-05-09 12:24:53 +01:00
|
|
|
with path.open(encoding="utf-8") as f:
|
|
|
|
first_line = f.readline().strip()
|
|
|
|
|
|
|
|
return first_line.split("#", 1)[-1].strip()
|
|
|
|
|
|
|
|
|
|
|
|
def sync(
|
2024-05-18 01:33:40 +01:00
|
|
|
root: Path,
|
|
|
|
pages_dirs: list[Path],
|
|
|
|
command: str,
|
|
|
|
title: str,
|
|
|
|
dry_run: bool = False,
|
|
|
|
language_to_update: str = "",
|
2024-05-09 12:24:53 +01:00
|
|
|
) -> list[str]:
|
2024-05-18 01:33:40 +01:00
|
|
|
"""
|
|
|
|
Synchronize a page title 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".
|
|
|
|
title (str): A title like "tar".
|
|
|
|
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-05-09 12:24:53 +01:00
|
|
|
paths = []
|
|
|
|
for page_dir in pages_dirs:
|
|
|
|
path = root / page_dir / command
|
|
|
|
if path.exists():
|
2024-05-18 01:33:40 +01:00
|
|
|
status = set_page_title(path, title, dry_run, language_to_update)
|
2024-05-09 12:24:53 +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-05-09 12:24:53 +01:00
|
|
|
return paths
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
2024-05-18 01:33:40 +01:00
|
|
|
parser = create_argument_parser("Sets the title for all translations of a page")
|
2024-05-09 12:24:53 +01:00
|
|
|
parser.add_argument("title", type=str, nargs="?", default="")
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
root = get_tldr_root()
|
2024-05-18 01:33:40 +01:00
|
|
|
pages_dirs = get_pages_dir(root)
|
2024-05-09 12:24:53 +01:00
|
|
|
|
|
|
|
target_paths = []
|
|
|
|
|
|
|
|
# Use '--page' option
|
|
|
|
if args.page != "":
|
2024-05-18 01:33:40 +01:00
|
|
|
target_paths += get_target_paths(args.page, pages_dirs)
|
2024-05-09 12:24:53 +01:00
|
|
|
|
|
|
|
for path in target_paths:
|
|
|
|
rel_path = "/".join(path.parts[-3:])
|
2024-05-18 01:33:40 +01:00
|
|
|
status = set_page_title(path, args.title)
|
2024-05-09 12:24:53 +01:00
|
|
|
if status != "":
|
2024-05-18 01:33:40 +01:00
|
|
|
print(create_colored_line(Colors.GREEN, f"{rel_path} {status}"))
|
2024-05-09 12:24:53 +01:00
|
|
|
|
|
|
|
# Use '--sync' option
|
|
|
|
elif args.sync:
|
|
|
|
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]
|
|
|
|
for platform in platforms:
|
|
|
|
platform_path = en_path / platform
|
|
|
|
commands = [
|
|
|
|
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
|
2024-05-09 12:24:53 +01:00
|
|
|
]
|
|
|
|
for command in commands:
|
2024-05-18 01:33:40 +01:00
|
|
|
title = get_page_title(root / "pages" / command)
|
2024-05-09 12:24:53 +01:00
|
|
|
if title != "":
|
2024-05-18 01:33:40 +01:00
|
|
|
target_paths += sync(
|
|
|
|
root, pages_dirs, command, title, args.dry_run, args.language
|
|
|
|
)
|
2024-05-09 12:24:53 +01:00
|
|
|
|
2024-05-18 01:33:40 +01:00
|
|
|
# Use '--stage' option
|
2024-05-09 12:24:53 +01: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)
|
2024-05-09 12:24:53 +01:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|