Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

scripts/set-alias-page.py, scripts/set-more-info-link.py: sync documentation and get_tldr_root #12744

Merged
merged 12 commits into from
May 13, 2024
Merged
33 changes: 21 additions & 12 deletions scripts/set-alias-page.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
isn't suggested to use the sync option. If used, only stage changes
and commit verified changes for your language.

Note: If there is a symlink error when using the stage flag remove the `pages.en`
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.
sebastiaanspeck marked this conversation as resolved.
Show resolved Hide resolved
If you aren't, the script will use TLDR_ROOT as the tldr root. Also, ensure '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.
sebastiaanspeck marked this conversation as resolved.
Show resolved Hide resolved

Usage:
Expand All @@ -24,15 +26,23 @@
-n, --dry-run
Show what changes would be made without actually modifying the page.

Positional Argument:
COMMAND The command to be set as the alias command.

Examples:
1. Add 'vi' as an alias page of 'vim':
python3 scripts/set-alias-page.py -p common/vi vim
python3 scripts/set-alias-page.py --page common/vi vim

2. Read English alias pages and synchronize them into all translations:
python3 scripts/set-alias-page.py -S
python3 scripts/set-alias-page.py --sync

3. Read English alias pages and show what changes would be made:
3. Read English alias pages, synchronize them into all translations and stage modified pages for commit:
python3 scripts/set-more-info-link.py -Ss
python3 scripts/set-more-info-link.py --sync --stage

4. Read English alias pages and show what changes would be made:
python3 scripts/set-alias-page.py -Sn
python3 scripts/set-alias-page.py --sync --dry-run
"""
Expand All @@ -41,6 +51,7 @@
import os
import re
import subprocess
from pathlib import Path

IGNORE_FILES = (".DS_Store", "tldr.md", "aria2.md")

Expand All @@ -51,16 +62,14 @@ def get_tldr_root():
"""

# If this script is running from tldr/scripts, the parent's parent is the root
f = os.path.normpath(__file__)
if f.endswith("tldr/scripts/set-alias-page.py"):
return os.path.dirname(os.path.dirname(f))

if "TLDR_ROOT" in os.environ:
return os.environ["TLDR_ROOT"]
else:
raise SystemExit(
"\x1b[31mPlease set TLDR_ROOT to the location of a clone of https://github.com/tldr-pages/tldr.\x1b[0m"
)
f = Path(__file__).resolve()
if "tldr" in str(f):
return next(path for path in f.parents if path.name == "tldr")
elif "TLDR_ROOT" in os.environ:
return Path(os.environ["TLDR_ROOT"])
raise SystemExit(
"\x1b[31mPlease set TLDR_ROOT to the location of a clone of https://github.com/tldr-pages/tldr."
)
sebastiaanspeck marked this conversation as resolved.
Show resolved Hide resolved


def get_templates(root):
Expand Down
29 changes: 15 additions & 14 deletions scripts/set-more-info-link.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,22 @@
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.
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.
If you aren't, the script will use TLDR_ROOT as the tldr root. Also, ensure 'git' is available.
sebastiaanspeck marked this conversation as resolved.
Show resolved Hide resolved
If there is a symlink error when using the stage flag remove the `pages.en`
directory temporarily and try executing it again.
sebastiaanspeck marked this conversation as resolved.
Show resolved Hide resolved

Usage: python3 scripts/set-more-info-link.py [-p PAGE] [-s] [-S] [-n] [LINK]

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.
-n, --dry-run Show what changes would be made without actually modifying the page.
-p, --page 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.
-n, --dry-run
Show what changes would be made without actually modifying the page.

Positional Argument:
LINK The link to be set as the "More information" link.
Expand Down Expand Up @@ -91,12 +91,13 @@


def get_tldr_root() -> Path:
# If this script is running from tldr/scripts, the parent's parent is the root
f = Path(__file__).resolve()
return next(path for path in f.parents if path.name == "tldr")

if "TLDR_ROOT" in os.environ:
if "tldr" in str(f):
return next(path for path in f.parents if path.name == "tldr")
elif "TLDR_ROOT" in os.environ:
return Path(os.environ["TLDR_ROOT"])
raise SystemError(
raise SystemExit(
"\x1b[31mPlease set TLDR_ROOT to the location of a clone of https://github.com/tldr-pages/tldr."
)

Expand Down