click-contrib / sphinx-click

A Sphinx plugin to automatically document click-based applications
MIT License
212 stars 57 forks source link

Remove any ANSI escape sequences from cli documentation #77

Closed alexey-tereshenkov-oxb closed 3 years ago

alexey-tereshenkov-oxb commented 3 years ago

Thank you for working on this project, I've used it for a while and it's really slick, kudos.

I have noticed that when ANSI escape sequences are used to denote colour of the text to use when rendering the text in the terminal, they are rendered "as-is". The sphinx-click parses the strings using statemachine.string2lines(); it does not show any consideration for any eventual escape sequences that may be present -- this results in raw escape sequences text being rendered in the HTML documentation.

I believe it may be beneficial to strip the escape sequences given that it's quite likely developers of the click applications may take advantage of custom colorisation of their help documentation that is going to be rendered in the terminal with pretty colours.

To illustrate the problem:

os_support = f'\033[94m{os_support}\033[0m'
print(os_support)
print(re.sub(r'\x1b\[([0-9,A-Z]{1,2}(;[0-9]{1,2})?(;[0-9]{3})?)?[m|K]?', '', os_support))

Screenshot 2021-03-16 at 11 59 44

The rendered HTML docs for the colorised epilog:

Screenshot 2021-03-16 at 12 01 07

Do you think it would be reasonable to strip the escape codes out of any string that is going to be read from the command line documentation (description, subcommands, options, etc) using a regexp such as the one used in the example above? Are there any complications that I may have overseen?

Many thanks.

stephenfin commented 3 years ago

This should be doable, hopefully without extra dependencies. Not sure when I'll have a chance to have a look at this but I'd be happy to guide anyone that was interested in fixing this.

stephenfin commented 3 years ago

This should be done in 2.7.0. Let me know if there are further tweaks needed.

AlexTereshenkov commented 3 years ago

This should be done in 2.7.0. Let me know if there are further tweaks needed.

Hey @stephenfin thanks a lot for this. Unfortunately, stripping is not done for the epilog and command options - I've raised a PR for this https://github.com/click-contrib/sphinx-click/pull/79 :)