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: Improve sync translations #15389

Open
wants to merge 44 commits into
base: main
Choose a base branch
from

Conversation

Zamoca42
Copy link
Collaborator

@Zamoca42 Zamoca42 commented Dec 30, 2024

Fixes: #14929

This PR improves the user experience of the set-alias-page.py script by adding an interactive wizard and refactoring the sync functionality.

Changes

Interactive Wizard

  • Added interactive wizard for creating alias pages with clear guidance
  • Improved user interface with colored output for better readability
  • Added input validation for required fields (title, original command)
  • Added preview of the page before creation
  • Added detailed explanations for each input field with examples

Sync Functionality Refactoring

  • Added SyncConfig and AliasPage dataclasses for better code organization
  • Improved title and commands extraction from alias pages
  • Enhanced sync functionality to handle titles in translations
  • Improved handling of alias patterns across different languages

Before

  • Required command line arguments for all inputs
  • No preview of the page before creation
  • Less structured sync implementation
  • No validation for required fields

After

  • Interactive wizard guides users through page creation
  • Clear preview of the final page with color-coded sections
  • More maintainable sync implementation using dataclasses
  • Input validation with clear error messages
  • Improved handling of alias patterns across different languages

@github-actions github-actions bot added documentation Issues/PRs modifying the documentation. tooling Helper tools, scripts and automated processes. translation Issues requesting translating pages from English to other languages. labels Dec 30, 2024
@tldr-bot

This comment was marked as off-topic.

@sebastiaanspeck
Copy link
Member

Could you remove the Korean changes? I think you have committed them by accident when testing

@sebastiaanspeck sebastiaanspeck changed the title scripts/set-alias-page.py: Fix status message in set-alias-page.py for existing files scripts/set-alias-page.py: Fix status message for existing files Dec 31, 2024
Copy link
Member

@sebastiaanspeck sebastiaanspeck left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After removing the Korean changes (or moving them to a separate PR), this LGTM!

@Zamoca42
Copy link
Collaborator Author

Could you remove the Korean changes? I think you have committed them by accident when testing

Let's separate the Korean alias page into a different PR

@Zamoca42 Zamoca42 removed the translation Issues requesting translating pages from English to other languages. label Dec 31, 2024
@sebastiaanspeck
Copy link
Member

sebastiaanspeck commented Jan 3, 2025

Looks a lot better now, minor thing that remains; the title for a page;
pages/common/docker-container-diff.md with # docker container diff copies as # docker container-diff.

I understand these issues were initially out-of-scope of this PR, but I guess it is better to fix it properly now than iterate multiple times with still a "broken" script.

@Zamoca42
Copy link
Collaborator Author

Zamoca42 commented Jan 3, 2025

Looks a lot better now, minor thing that remains; the title for a page; pages/common/docker-container-diff.md with # docker container diff copies as # docker container-diff.

I understand these issues were initially out-of-scope of this PR, but I guess it is better to fix it properly now than iterate multiple times with still a "broken" script.

I also think it's better to fix it properly before moving on.
Regarding the title, it is indeed challenging to account for all possible patterns.
For instance, in pages/common/pacman-r.md, the title is # pacman -R,
whereas in pages/common/npm-run-script.md, the title is # npm run-script.

Currently, the title is parsed from the file path, but it might be better to extract it directly from the original alias content instead.

@sebastiaanspeck
Copy link
Member

Looks a lot better now, minor thing that remains; the title for a page; pages/common/docker-container-diff.md with # docker container diff copies as # docker container-diff.
I understand these issues were initially out-of-scope of this PR, but I guess it is better to fix it properly now than iterate multiple times with still a "broken" script.

I also think it's better to fix it properly before moving on. Regarding the title, it is indeed challenging to account for all possible patterns. For instance, in pages/common/pacman-r.md, the title is # pacman -R, whereas in pages/common/npm-run-script.md, the title is # npm run-script.

Currently, the title is parsed from the file path, but it might be better to extract it directly from the original alias content instead.

Looks a lot better now, minor thing that remains; the title for a page; pages/common/docker-container-diff.md with # docker container diff copies as # docker container-diff.
I understand these issues were initially out-of-scope of this PR, but I guess it is better to fix it properly now than iterate multiple times with still a "broken" script.
Currently, the title is parsed from the file path, but it might be better to extract it directly from the original alias content instead.

I agree, we just need to copy the title just like it exists in pages/. If we are creating a new page, we could add a third optional argument, just like you did with DOCUMENTATION_COMMAND.

@@ -309,7 +309,7 @@ def create_argument_parser(description: str) -> argparse.ArgumentParser:

parser = argparse.ArgumentParser(description=description)
parser.add_argument(
"-p",
"-P",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe we still can use -p. Explain why not

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's typically because of the documentation command exception of the "osx/g*.md" document in "osx/gsum.md ". If "-p linux sum" is used as an argument, it recognizes "-p" and causes an error.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

However, instead of taking title and commands as arguments, we can use "-p" again if you make it into a wizard form

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feel free to modify it, but that feels like a new PR. Let's fix the current issues and then accept this PR for now.

- Change -P/--page option to -p/--page
- Remove positional arguments (ORIGINAL_CMD, DOCUMENTATION_CMD)
- Add interactive wizard for creating alias pages
- Refactor sync functionality with dataclasses (SyncConfig, AliasPage)
- Extract title from alias pages for better sync
- Update documentation and examples
- Improve code formatting and readability
- Add detailed prompts with examples for each input field
- Show preview of the page before creation
@Zamoca42 Zamoca42 changed the title scripts/set-alias-page.py: fix status message for existing files scripts/set-alias-page.py: Improve sync translations Jan 4, 2025
- Update Config dataclass for global script configuration
- Move dataclass definitions to top of file
- Remove redundant dry_run and language parameters from functions
- Update function signatures to use global config
- Refactor AliasPage to use AliasPageContent
- Update function signatures to use new dataclass
  * get_alias_command_in_page returns AliasPageContent
  * set_alias_page takes AliasPageContent parameter
  * generate_alias_page_content takes AliasPageContent
- Simplify get_english_alias_pages to return list[AliasPage]
@sebastiaanspeck
Copy link
Member

IMO this PR is going too broad for the initial scope. Please keep it simple for now and introduce new features in a new PR.

- Move templates into Config class as a field
  * Remove global templates variable
  * Update all template references to use config.templates
- Add pages_dirs to Config class
  * Remove redundant pages_dirs variable passing
  * Use config.pages_dirs throughout the code
- Move sync_translations logic into main
  * Remove sync_translations function
  * Integrate sync logic directly into args.sync block
  * Simplify code flow for sync operation
- Rename functions and variables
  * sync -> sync_alias_page_to_locale
  * existing_page_content -> existing_locale_page_content
  * locale_page_content -> new_locale_page_content
- Remove redundant comments
@Zamoca42
Copy link
Collaborator Author

Zamoca42 commented Jan 4, 2025

IMO this PR is going too broad for the initial scope. Please keep it simple for now and introduce new features in a new PR.

There is no new feature here, just add an argument to the title and get it as input.
Changes in sync also separated variable names and functions.

@sebastiaanspeck
Copy link
Member

IMO this PR is going too broad for the initial scope. Please keep it simple for now and introduce new features in a new PR.

There is no new feature here, just add an argument to the title and get it as input.

Changes in sync also separated variable names and functions.

The three scripts looked almost identical and now they do not anymore. IMO the benefit is minimal ATM. I would prefer fixing the # page-title bug instead of introducing the wizard etc.

@Zamoca42
Copy link
Collaborator Author

Zamoca42 commented Jan 4, 2025

The three scripts looked almost identical and now they do not anymore. IMO the benefit is minimal ATM. I would prefer fixing the # page-title bug instead of introducing the wizard etc.

The separation of title and commands from options in prompt_alias_page_info is a deliberate isolation measure to prevent argument parsing bugs. If these were added as positional arguments, titles like pacman -S would be incorrectly interpreted as options, causing errors. The wizard interface simply serves as an additional guide for input.

For example:

  • Safe (current): Wizard prompts for "pacman -S" as title
  • Unsafe (if positional): python3 scripts/set-alias-page.py -p linux/pacman-s "pacman -S" "pacman --sync" "pacman sync" would treat -S and --sync as an option flag

@sebastiaanspeck
Copy link
Member

The three scripts looked almost identical and now they do not anymore. IMO the benefit is minimal ATM. I would prefer fixing the # page-title bug instead of introducing the wizard etc.

The separation of title and commands from options in prompt_alias_page_info is a deliberate isolation measure to prevent argument parsing bugs. If these were added as positional arguments, titles like pacman -S would be incorrectly interpreted as options, causing errors. The wizard interface simply serves as an additional guide for input.

For example:

  • Safe (current): Wizard prompts for "pacman -S" as title

  • Unsafe (if positional): python3 scripts/set-alias-page.py -p linux/pacman-s "pacman -S" "pacman --sync" "pacman sync" would treat -S and --sync as an option flag

Fine by me, but change it for all scripts then to keep them sort-of in sync with each other.

scripts/set-alias-page.py Outdated Show resolved Hide resolved

target_paths = []

# Use '--page' option
if args.page != "":
target_paths += get_target_paths(args.page, pages_dirs)
page_info = prompt_alias_page_info(args.page)
target_paths += get_target_paths(args.page, config.pages_dirs)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not working for creating new alias pages. It can only update current alias pages.

Copy link
Collaborator Author

@Zamoca42 Zamoca42 Jan 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

scripts/_common.py

def get_target_paths(page: Path, pages_dirs: Path) -> list[Path]:
    """
    Get all paths in all languages that match the page.

    Parameters:
    page (Path): the page to search for.

    Returns:
    list (list of Path's): A list of Path's.
    """

    target_paths = []

    if not page.lower().endswith(".md"):
        page = f"{page}.md"
    arg_platform, arg_page = page.split("/")

    for pages_dir in pages_dirs:
        page_path = pages_dir / arg_platform / arg_page

        if not page_path.exists():
            continue # Skip if page is missing
        target_paths.append(page_path)

    target_paths.sort()

    return target_paths

Looking at these files, I noticed that 8 months ago there was a change to prevent creating new pages. The change was in get_target_paths() where it skips paths that don't exist.
Is there a specific reason for this change?
From my understanding, the other scripts are designed to modify existing pages

Co-authored-by: Sebastiaan Speck <[email protected]>
@tldr-bot
Copy link

tldr-bot commented Jan 6, 2025

The build for this PR failed with the following error(s):

would reformat /home/runner/work/tldr/tldr/scripts/set-alias-page.py

Oh no! 💥 💔 💥
1 file would be reformatted, 6 files would be left unchanged.

Please fix the error(s) and push again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Issues/PRs modifying the documentation. tooling Helper tools, scripts and automated processes.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Update on Missing Alias Pages in Translation Dashboard for ko
3 participants