set-alias-page.py 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. #!/usr/bin/env python3
  2. # SPDX-License-Identifier: MIT
  3. """
  4. A Python script to generate or update alias pages.
  5. 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.
  6. 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
  7. If you aren't, the script will use TLDR_ROOT as the tldr root. Also, ensure 'git' is available.
  8. Usage:
  9. python3 scripts/set-alias-page.py [-p PAGE] [-S] [-l LANGUAGE] [-s] [-n] [COMMAND]
  10. Options:
  11. -p, --page PAGE
  12. Specify the alias page in the format "platform/alias_command.md".
  13. -S, --sync
  14. Synchronize each translation's alias page (if exists) with that of the English page.
  15. -l, --language LANGUAGE
  16. Specify the language, a POSIX Locale Name in the form of "ll" or "ll_CC" (e.g. "fr" or "pt_BR").
  17. -s, --stage
  18. Stage modified pages (requires 'git' on $PATH and TLDR_ROOT to be a Git repository).
  19. -n, --dry-run
  20. Show what changes would be made without actually modifying the page.
  21. Positional Argument:
  22. COMMAND The command to be set as the alias command.
  23. Examples:
  24. 1. Add 'vi' as an alias page of 'vim':
  25. python3 scripts/set-alias-page.py -p common/vi vim
  26. python3 scripts/set-alias-page.py --page common/vi vim
  27. 2. Read English alias pages and synchronize them into all translations:
  28. python3 scripts/set-alias-page.py -S
  29. python3 scripts/set-alias-page.py --sync
  30. 3. Read English alias pages and synchronize them for Brazilian Portuguese pages only:
  31. python3 scripts/set-alias-page.py -S -l pt_BR
  32. python3 scripts/set-alias-page.py --sync --language pt_BR
  33. 4. Read English alias pages, synchronize them into all translations and stage modified pages for commit:
  34. python3 scripts/set-alias-page.py -Ss
  35. python3 scripts/set-alias-page.py --sync --stage
  36. 5. Read English alias pages and show what changes would be made:
  37. python3 scripts/set-alias-page.py -Sn
  38. python3 scripts/set-alias-page.py --sync --dry-run
  39. """
  40. import re
  41. from pathlib import Path
  42. from _common import (
  43. IGNORE_FILES,
  44. Colors,
  45. get_tldr_root,
  46. get_pages_dir,
  47. get_target_paths,
  48. get_locale,
  49. get_status,
  50. stage,
  51. create_colored_line,
  52. create_argument_parser,
  53. )
  54. IGNORE_FILES += ("tldr.md", "aria2.md")
  55. def test_ignore_files():
  56. assert IGNORE_FILES == (
  57. ".DS_Store",
  58. "tldr.md",
  59. "aria2.md",
  60. )
  61. assert ".DS_Store" in IGNORE_FILES
  62. assert "tldr.md" in IGNORE_FILES
  63. def get_templates(root: Path):
  64. """
  65. Get all alias page translation templates from
  66. TLDR_ROOT/contributing-guides/translation-templates/alias-pages.md.
  67. Parameters:
  68. root (Path): The path of local tldr repository, i.e., TLDR_ROOT.
  69. Returns:
  70. dict of (str, str): Language labels map to alias page templates.
  71. """
  72. template_file = root / "contributing-guides/translation-templates/alias-pages.md"
  73. with template_file.open(encoding="utf-8") as f:
  74. lines = f.readlines()
  75. # Parse alias-pages.md
  76. templates = {}
  77. i = 0
  78. while i < len(lines):
  79. if lines[i].startswith("###"):
  80. lang = lines[i][4:].strip("\n").strip(" ")
  81. while True:
  82. i = i + 1
  83. if lines[i].startswith("Not translated yet."):
  84. is_translated = False
  85. break
  86. elif lines[i].startswith("```markdown"):
  87. i = i + 1
  88. is_translated = True
  89. break
  90. if is_translated:
  91. text = ""
  92. while not lines[i].startswith("```"):
  93. text += lines[i]
  94. i = i + 1
  95. templates[lang] = text
  96. i = i + 1
  97. return templates
  98. def set_alias_page(
  99. path: Path, command: str, dry_run: bool = False, language_to_update: str = ""
  100. ) -> str:
  101. """
  102. Write an alias page to disk.
  103. Parameters:
  104. path (string): Path to an alias page
  105. command (string): The command that the alias stands for.
  106. dry_run (bool): Whether to perform a dry-run, i.e. only show the changes that would be made.
  107. language_to_update (string): Optionally, the language of the translation to be updated.
  108. Returns:
  109. str: Execution status
  110. "" if the alias page standing for the same command already exists or if the locale does not match language_to_update.
  111. "\x1b[36mpage added"
  112. "\x1b[34mpage updated"
  113. "\x1b[36mpage would be added"
  114. "\x1b[34mpage would updated"
  115. """
  116. locale = get_locale(path)
  117. if locale not in templates or (
  118. language_to_update != "" and locale != language_to_update
  119. ):
  120. # return empty status to indicate that no changes were made
  121. return ""
  122. # Test if the alias page already exists
  123. original_command = get_alias_page(path)
  124. if original_command == command:
  125. return ""
  126. status = get_status(
  127. "added" if original_command == "" else "updated", dry_run, "page"
  128. )
  129. if not dry_run: # Only write to the path during a non-dry-run
  130. alias_name = path.stem
  131. text = (
  132. templates[locale]
  133. .replace("example", alias_name, 1)
  134. .replace("example", command)
  135. )
  136. path.parent.mkdir(parents=True, exist_ok=True)
  137. with path.open("w", encoding="utf-8") as f:
  138. f.write(text)
  139. return status
  140. def get_alias_page(path: Path) -> str:
  141. """
  142. Determine whether the given path is an alias page.
  143. Parameters:
  144. path (Path): Path to a page
  145. Returns:
  146. str: "" If the path doesn't exit or is not an alias page,
  147. otherwise return what command the alias stands for.
  148. """
  149. if not path.exists():
  150. return ""
  151. with path.open(encoding="utf-8") as f:
  152. for line in f:
  153. # match alias (`tldr <alias>`)
  154. if match := re.search(r"^`tldr (.+)`", line):
  155. return match[1]
  156. return ""
  157. def sync(
  158. root: Path,
  159. pages_dirs: list[Path],
  160. alias_name: str,
  161. original_command: str,
  162. dry_run: bool = False,
  163. language_to_update: str = "",
  164. ) -> list[Path]:
  165. """
  166. Synchronize an alias page into all translations.
  167. Parameters:
  168. root (Path): TLDR_ROOT
  169. pages_dirs (list of Path's): Path's of page entry and platform, e.g. "page.fr/common".
  170. alias_name (str): An alias command with .md extension like "vi.md".
  171. original_command (str): An Original command like "vim".
  172. dry_run (bool): Whether to perform a dry-run, i.e. only show the changes that would be made.
  173. language_to_update (str): Optionally, the language of the translation to be updated.
  174. Returns:
  175. list (list of Path's): A list of Path's to be staged into git, using by --stage option.
  176. """
  177. paths = []
  178. for page_dir in pages_dirs:
  179. path = root / page_dir / alias_name
  180. status = set_alias_page(path, original_command, dry_run, language_to_update)
  181. if status != "":
  182. rel_path = "/".join(path.parts[-3:])
  183. paths.append(rel_path)
  184. print(create_colored_line(Colors.GREEN, f"{rel_path} {status}"))
  185. return paths
  186. def main():
  187. parser = create_argument_parser(
  188. "Sets the alias page for all translations of a page"
  189. )
  190. parser.add_argument("command", type=str, nargs="?", default="")
  191. args = parser.parse_args()
  192. root = get_tldr_root()
  193. # A dictionary of all alias page translations
  194. global templates
  195. templates = get_templates(root)
  196. pages_dirs = get_pages_dir(root)
  197. target_paths = []
  198. # Use '--page' option
  199. if args.page != "":
  200. target_paths += get_target_paths(args.page, pages_dirs)
  201. for path in target_paths:
  202. rel_path = "/".join(path.parts[-3:])
  203. status = set_alias_page(path, args.command, args.dry_run, args.language)
  204. if status != "":
  205. print(create_colored_line(Colors.GREEN, f"{rel_path} {status}"))
  206. # Use '--sync' option
  207. elif args.sync:
  208. pages_dirs.remove(root / "pages")
  209. en_path = root / "pages"
  210. platforms = [i.name for i in en_path.iterdir() if i.name not in IGNORE_FILES]
  211. for platform in platforms:
  212. platform_path = en_path / platform
  213. commands = [
  214. f"{platform}/{page.name}"
  215. for page in platform_path.iterdir()
  216. if page.name not in IGNORE_FILES
  217. ]
  218. for command in commands:
  219. original_command = get_alias_page(root / "pages" / command)
  220. if original_command != "":
  221. target_paths += sync(
  222. root,
  223. pages_dirs,
  224. command,
  225. original_command,
  226. args.dry_run,
  227. args.language,
  228. )
  229. # Use '--stage' option
  230. if args.stage and not args.dry_run and len(target_paths) > 0:
  231. stage(target_paths)
  232. if __name__ == "__main__":
  233. main()