2021-10-11 21:40:03 +01:00
#!/usr/bin/env python3
# SPDX-License-Identifier: MIT
"""
A Python script to generate or update alias pages .
2024-05-13 15:21:30 +01:00
Disclaimer : This script generates a lot of false positives so it isn ' t suggested to use the sync option. If used, only stage changes and commit verified changes for your language by using -l LANGUAGE.
2021-10-11 21:40:03 +01:00
2024-05-13 15:21:30 +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
If you aren ' t, the script will use TLDR_ROOT as the tldr root. Also, ensure ' git ' is available.
2023-12-21 14:17:18 +00:00
Usage :
2024-05-13 15:21:30 +01:00
python3 scripts / set - alias - page . py [ - p PAGE ] [ - S ] [ - l LANGUAGE ] [ - s ] [ - n ] [ COMMAND ]
2023-12-21 14:17:18 +00:00
Options :
- p , - - page PAGE
Specify the alias page in the format " platform/alias_command.md " .
2024-05-13 15:21:30 +01:00
- S , - - sync
Synchronize each translation ' s alias page (if exists) with that of the English page.
2024-05-09 12:01:01 +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 " ) .
2023-12-21 14:17:18 +00:00
- s , - - stage
Stage modified pages ( requires ' git ' on $ PATH and TLDR_ROOT to be a Git repository ) .
2024-01-02 05:53:39 +00:00
- n , - - dry - run
Show what changes would be made without actually modifying the page .
2024-05-13 15:21:30 +01:00
Positional Argument :
COMMAND The command to be set as the alias command .
2023-12-21 14:17:18 +00:00
Examples :
1. Add ' vi ' as an alias page of ' vim ' :
2024-01-02 05:53:39 +00:00
python3 scripts / set - alias - page . py - p common / vi vim
2024-05-13 15:21:30 +01:00
python3 scripts / set - alias - page . py - - page common / vi vim
2023-12-21 14:17:18 +00:00
2. Read English alias pages and synchronize them into all translations :
2024-01-02 05:53:39 +00:00
python3 scripts / set - alias - page . py - S
2024-05-13 15:21:30 +01:00
python3 scripts / set - alias - page . py - - sync
2024-01-02 05:53:39 +00:00
2024-05-13 15:21:30 +01:00
3. Read English alias pages and synchronize them for Brazilian Portuguese pages only :
2024-05-09 12:01:01 +01:00
python3 scripts / set - alias - page . py - S - l pt_BR
python3 scripts / set - alias - page . py - - sync - - language pt_BR
2024-05-13 15:21:30 +01:00
4. Read English alias pages , synchronize them into all translations and stage modified pages for commit :
2024-05-18 01:33:40 +01:00
python3 scripts / set - alias - page . py - Ss
python3 scripts / set - alias - page . py - - sync - - stage
2024-05-13 15:21:30 +01:00
5. 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
2021-10-11 21:40:03 +01:00
"""
import re
2024-05-13 15:21:30 +01: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 ,
)
IGNORE_FILES + = ( " tldr.md " , " aria2.md " )
def test_ignore_files ( ) :
assert IGNORE_FILES == (
" .DS_Store " ,
" tldr.md " ,
" aria2.md " ,
2024-05-13 15:21:30 +01:00
)
2024-05-18 01:33:40 +01:00
assert " .DS_Store " in IGNORE_FILES
assert " tldr.md " in IGNORE_FILES
2021-10-11 21:40:03 +01:00
2024-05-18 01:33:40 +01:00
def get_templates ( root : Path ) :
2021-10-11 21:40:03 +01:00
"""
Get all alias page translation templates from
TLDR_ROOT / contributing - guides / translation - templates / alias - pages . md .
Parameters :
2024-05-18 01:33:40 +01:00
root ( Path ) : The path of local tldr repository , i . e . , TLDR_ROOT .
2021-10-11 21:40:03 +01:00
Returns :
dict of ( str , str ) : Language labels map to alias page templates .
"""
2024-05-18 01:33:40 +01:00
template_file = root / " contributing-guides/translation-templates/alias-pages.md "
with template_file . open ( encoding = " utf-8 " ) as f :
2021-10-11 21:40:03 +01:00
lines = f . readlines ( )
# Parse alias-pages.md
templates = { }
i = 0
while i < len ( lines ) :
if lines [ i ] . startswith ( " ### " ) :
lang = lines [ i ] [ 4 : ] . strip ( " \n " ) . strip ( " " )
while True :
i = i + 1
if lines [ i ] . startswith ( " Not translated yet. " ) :
is_translated = False
break
elif lines [ i ] . startswith ( " ```markdown " ) :
i = i + 1
is_translated = True
break
if is_translated :
text = " "
while not lines [ i ] . startswith ( " ``` " ) :
text + = lines [ i ]
i = i + 1
templates [ lang ] = text
i = i + 1
return templates
2024-05-18 01:33:40 +01:00
def set_alias_page (
path : Path , command : str , dry_run : bool = False , language_to_update : str = " "
) - > str :
2021-10-11 21:40:03 +01:00
"""
Write an alias page to disk .
Parameters :
2024-05-18 01:33:40 +01:00
path ( string ) : Path to an alias page
2021-10-11 21:40:03 +01:00
command ( string ) : The command that the alias stands for .
2024-05-09 12:01:01 +01:00
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 .
2021-10-11 21:40:03 +01:00
Returns :
str : Execution status
2024-05-18 01:33:40 +01:00
" " if the alias page standing for the same command already exists or if the locale does not match language_to_update .
" \x1b [36mpage added "
" \x1b [34mpage updated "
" \x1b [36mpage would be added "
" \x1b [34mpage would updated "
2021-10-11 21:40:03 +01:00
"""
2024-05-18 01:33:40 +01:00
locale = get_locale ( path )
2024-05-09 12:01:01 +01:00
if locale not in templates or (
language_to_update != " " and locale != language_to_update
) :
2024-05-18 01:33:40 +01:00
# return empty status to indicate that no changes were made
2021-10-11 21:40:03 +01:00
return " "
# Test if the alias page already exists
2024-05-18 01:33:40 +01:00
original_command = get_alias_page ( path )
if original_command == command :
2021-10-11 21:40:03 +01:00
return " "
2024-01-02 05:53:39 +00:00
2024-05-18 01:33:40 +01:00
status = get_status (
" added " if original_command == " " else " updated " , dry_run , " page "
)
2024-01-02 05:53:39 +00:00
2024-05-18 01:33:40 +01:00
if not dry_run : # Only write to the path during a non-dry-run
alias_name = path . stem
2024-01-02 05:53:39 +00:00
text = (
templates [ locale ]
. replace ( " example " , alias_name , 1 )
. replace ( " example " , command )
)
2024-05-18 01:33:40 +01:00
path . parent . mkdir ( parents = True , exist_ok = True )
with path . open ( " w " , encoding = " utf-8 " ) as f :
2024-01-02 05:53:39 +00:00
f . write ( text )
2021-10-11 21:40:03 +01:00
return status
2024-05-18 01:33:40 +01:00
def get_alias_page ( path : Path ) - > str :
"""
Determine whether the given path is an alias page .
Parameters :
path ( Path ) : Path to a page
Returns :
str : " " If the path doesn ' t exit or is not an alias page,
otherwise return what command the alias stands for .
"""
if not path . exists ( ) :
return " "
with path . open ( encoding = " utf-8 " ) as f :
for line in f :
# match alias (`tldr <alias>`)
if match := re . search ( r " ^`tldr (.+)` " , line ) :
return match [ 1 ]
return " "
2024-05-09 12:01:01 +01:00
def sync (
2024-05-18 01:33:40 +01:00
root : Path ,
pages_dirs : list [ Path ] ,
alias_name : str ,
original_command : str ,
dry_run : bool = False ,
language_to_update : str = " " ,
) - > list [ Path ] :
2021-10-11 21:40:03 +01:00
"""
2024-01-02 05:53:39 +00:00
Synchronize an alias page into all translations .
2021-10-11 21:40:03 +01:00
Parameters :
2024-05-18 01:33:40 +01:00
root ( Path ) : TLDR_ROOT
pages_dirs ( list of Path ' s): Path ' s of page entry and platform , e . g . " page.fr/common " .
2021-10-11 21:40:03 +01:00
alias_name ( str ) : An alias command with . md extension like " vi.md " .
2024-05-18 01:33:40 +01:00
original_command ( str ) : An Original command like " vim " .
2024-05-09 12:01:01 +01:00
dry_run ( bool ) : Whether to perform a dry - run , i . e . only show the changes that would be made .
2024-05-18 01:33:40 +01:00
language_to_update ( str ) : Optionally , the language of the translation to be updated .
2021-10-11 21:40:03 +01:00
Returns :
2024-05-18 01:33:40 +01:00
list ( list of Path ' s): A list of Path ' s to be staged into git , using by - - stage option .
2021-10-11 21:40:03 +01:00
"""
2024-05-18 01:33:40 +01:00
paths = [ ]
2021-10-11 21:40:03 +01:00
for page_dir in pages_dirs :
2024-05-18 01:33:40 +01:00
path = root / page_dir / alias_name
status = set_alias_page ( path , original_command , dry_run , language_to_update )
2021-10-11 21:40:03 +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 } " ) )
return paths
2021-10-11 21:40:03 +01:00
def main ( ) :
2024-05-18 01:33:40 +01:00
parser = create_argument_parser (
" Sets the alias page for all translations of a page "
2024-01-02 05:53:39 +00:00
)
2021-10-11 21:40:03 +01:00
parser . add_argument ( " command " , type = str , nargs = " ? " , default = " " )
args = parser . parse_args ( )
root = get_tldr_root ( )
# A dictionary of all alias page translations
global templates
templates = get_templates ( root )
2024-05-18 01:33:40 +01:00
pages_dirs = get_pages_dir ( root )
target_paths = [ ]
2021-10-11 21:40:03 +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-10-11 21:40:03 +01:00
for path in target_paths :
2024-05-18 01:33:40 +01:00
rel_path = " / " . join ( path . parts [ - 3 : ] )
status = set_alias_page ( path , args . command , args . dry_run , args . language )
2021-10-11 21:40:03 +01:00
if status != " " :
2024-05-18 01:33:40 +01:00
print ( create_colored_line ( Colors . GREEN , f " { rel_path } { status } " ) )
2021-10-11 21:40:03 +01:00
# Use '--sync' option
elif args . sync :
2024-05-18 01:33:40 +01: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-10-11 21:40:03 +01:00
for platform in platforms :
2024-05-18 01:33:40 +01:00
platform_path = en_path / platform
2021-10-11 21:40:03 +01:00
commands = [
2024-05-18 01:33:40 +01:00
f " { platform } / { page . name } "
for page in platform_path . iterdir ( )
if page . name not in IGNORE_FILES
2021-10-11 21:40:03 +01:00
]
for command in commands :
2024-05-18 01:33:40 +01:00
original_command = get_alias_page ( root / " pages " / command )
if original_command != " " :
target_paths + = sync (
2024-05-09 12:01:01 +01:00
root ,
pages_dirs ,
command ,
2024-05-18 01:33:40 +01:00
original_command ,
2024-05-09 12:01:01 +01:00
args . dry_run ,
args . language ,
2024-01-02 05:53:39 +00:00
)
2021-10-11 21:40:03 +01:00
2024-01-02 05:53:39 +00:00
# Use '--stage' option
2024-05-18 01:33:40 +01:00
if args . stage and not args . dry_run and len ( target_paths ) > 0 :
stage ( target_paths )
2021-10-11 21:40:03 +01:00
if __name__ == " __main__ " :
main ( )