-
Notifications
You must be signed in to change notification settings - Fork 39
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
rich markup shows when using sphinx-click #48
Comments
Hmm, I've not heard of click-man-page before, and I guess it's the sphinx-click plugin? I'd need to look into how they are fetching the click output first - if you're able to do some detective work that would help. However, hopefully #19 will help. The aim there is to make it easier to return a rich renderable object instead of always printing. Then it would be fairly easy to get a plaintext copy of the help without any markup. If the above plugins are using regular click function calls to fetch the help strings, it might be that we can refactor the rich-click code to better emulate native click. Bit of a long shot, but may be possible. |
As far as rst is concerned, we have it on our roadmap to support rst docstrings in #172. And HTML generation is supported via: Rendering the CLI help text as simple rst is a different story. We can potentially add this to the I don't know how high of a priority this is. Typer has a markdown rendering feature and I suppose it would be cool to keep up with them in terms of features. I'm marking this as being on the 1.9 roadmap but it's low on the 1.9 wishlist. |
I face a similar situation to this and #19. I'm trying to auto generate HTML documentation. What would be a way to get HTML output without printing to stdout? |
The solution currently appears to be that an application must manage its own custom subclass of The last piece to make it work requires modification from what appears to be an oversight so I'm going to open a PR to fix that. |
Hi, I'll look at this when I get a moment. For now, I will say, the intended behavior is: you run the command The All that said, the PR you've suggested seems pretty reasonable even without me fully understanding the issue; we welcome efforts to do more to make the |
Thanks! I think the missing context is that documentation generators such as the one I maintain and the one described by the opening post of this issue cannot use the CLI as we must go through the actual APIs for introspection in order to construct the desired HTML e.g. segmented into sections, custom table formatting, branded coloring, etc. |
Thanks, and that makes sense. I've left a comment on your PR. Note I do not really have plans to make outputting to html or svg any more accessible than |
I think the downside of what you're saying is the impact on users. In my application I just so happen to have complex requirements where I already had a custom command and context class but the vast majority of users will not. For those users who use |
If that's the case, I'm not even 100% sure that To be honest, I am reluctant to expand the API here to use environment variables. API scope creep is dangerous for maintainability, and creating niche things that others rely on is not something I want to do when I believe there are other solutions. Before I continue, can I get a bit of clarification here?: are you actually relying on all the behavior of It seems like the latter may be true in your case. In which case I would recommend setting that directly. For the current version of rich-click, on your end, you can do something like this: def _build_command_context(
prog_name: str, command: click.Command, parent: click.Context | None
) -> click.Context:
# https://github.com/pallets/click/blob/8.1.8/src/click/core.py#L859-L862
ctx = command.context_class(
cast(click.Command, command),
info_name=prog_name,
parent=parent,
**command.context_settings,
)
try:
import rich_click
except ImportError:
pass
else:
if isinstance(command, rich_click.RichCommand):
ctx.console.record = True
ctx.console.file = open(os.devnull, "w")
return ctx That said, I do think we can come to some sort of agreement on a set of code changes that would be beneficial for you. For internal code changes, I would be happy to make it so that, by default, rich-click does not eagerly write to the console, but instead writes to a buffer which ❯ rich-click flask --help
Usage: flask [OPTIONS] COMMAND [ARGS]...
A general utility script for Flask applications.
An application to load must be given with the '--app' option, 'FLASK_APP' environment variable, or with a 'wsgi.py' or 'app.py' file in the current directory.
╭─ Options ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ --env-file -e FILE Load environment variables from this file, taking precedence over those set by '.env' and '.flaskenv'. Variables set │
│ directly in the environment take highest precedence. python-dotenv must be installed. │
│ --app -A IMPORT The Flask application or factory function to load, in the form 'module:name'. Module can be a dotted import or file path. │
│ Name is not required if it is 'app', 'application', 'create_app', or 'make_app', and can be 'name(args)' to pass arguments. │
│ --debug/--no-debug Set debug mode. │
│ --version Show the Flask version. │
│ --help Show this message and exit. │
╰──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
Error: Could not locate a Flask application. Use the 'flask --app' option, 'FLASK_APP' environment variable, or a 'wsgi.py' or 'app.py' file in the current directory.
Error: Could not locate a Flask application. Use the 'flask --app' option, 'FLASK_APP' environment variable, or a 'wsgi.py' or 'app.py' file in the current directory.
Error: Could not locate a Flask application. Use the 'flask --app' option, 'FLASK_APP' environment variable, or a 'wsgi.py' or 'app.py' file in the current directory.
Error: Could not locate a Flask application. Use the 'flask --app' option, 'FLASK_APP' environment variable, or a 'wsgi.py' or 'app.py' file in the current directory.
╭─ Commands ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ routes Show the routes for the app. │
│ run Run a development server. │
│ shell Run a shell in the app context. │
╰──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯ versus: ❯ flask --help
Error: Could not locate a Flask application. Use the 'flask --app' option, 'FLASK_APP' environment variable, or a 'wsgi.py' or 'app.py' file in the current directory.
Usage: flask [OPTIONS] COMMAND [ARGS]...
A general utility script for Flask applications.
An application to load must be given with the '--app' option, 'FLASK_APP'
environment variable, or with a 'wsgi.py' or 'app.py' file in the current
directory.
Options:
-e, --env-file FILE Load environment variables from this file, taking
precedence over those set by '.env' and '.flaskenv'.
Variables set directly in the environment take highest
precedence. python-dotenv must be installed.
-A, --app IMPORT The Flask application or factory function to load, in
the form 'module:name'. Module can be a dotted import
or file path. Name is not required if it is 'app',
'application', 'create_app', or 'make_app', and can be
'name(args)' to pass arguments.
--debug / --no-debug Set debug mode.
--version Show the Flask version.
--help Show this message and exit.
Commands:
routes Show the routes for the app.
run Run a development server.
shell Run a shell in the app context. So you can see for If by default, the Console() rich-click provides writes to devnull, has record=True, and has a value retrievable by |
No I mean the user would do that like: @rich_click.rich_config(
help_config=rich_click.RichHelpConfiguration(
export_console_as="html" if os.environ.get("BUILDING_DOCS") == "true" else None,
),
)
A way to set the latter I think because I don't know what else it does 😅
Following the way edit: Hopefully configurable actually because we would require the help text to be HTML whereas we would prefer the usage string (e.g. |
OK, I think I am making sense of what you need and want to do. I will be honest though, I'm not sure to what extent I would fully support it. I would still think about adding explicit support in your code for rich-click (I can potentially contribute it if you want), which would be an even easier path for users as they would have to do nothing. The usage string will be HTML, there is no way around that; we are just running The default output of I see in your code you print out the command output, which is easier, but can run into some limitations, as is the case here. Forgive me if I am overstepping here in suggesting this, but Typer's markdown parsing may be of interest for your code, and could make things really clean and effective: https://github.com/fastapi/typer/blob/46f4a0f470400aef45fd3d433c5066687f4d61e8/typer/cli.py#L195 Basically, they parse out all the objects inside of a If you are time constrained and would want assistance in implementing Typer's implementation of markdown parsing, let me know and I'd be glad to open a PR in mkdocs-click. |
Talking this out is very helpful so I appreciate your patience! Upon further thinking, I actually do not need HTML and I was just lucky in my tests that the currently rendered HTML looks nice. What I actually want ubiquitously is the raw text without ANSI codes so that I can dump the user-defined Markdown help text within the generated page. The So final answer is I just need (and the library this issue mentions needs) only the raw text please! |
Yep, that's what I figured when I was asking for clarification, that the We can make it so What do you think about the Typer code I linked to with |
|
Stripping ANSI can be done with regex. And ahhh, I see. OK. Didn't read the code carefully. 🤦 The issue is specifically the |
Currently, the click-man-page rendering and sphinx html docs get pretty messed up when using rich-click.
Here's an example of a man page output:
Notice the
--env
option - it is not immediately obvious if that is weird markup or actual option choices.I think it would make sense to somehow throw all the markup out in manpages. With sphinx-generated html or similar things that could render them - not sure how to proceed.
The text was updated successfully, but these errors were encountered: