1010from click import Command , Group , Option
1111
1212from . import __version__
13- from .core import HAS_RICH
13+ from .core import HAS_RICH , MARKUP_MODE_KEY
1414
1515default_app_names = ("app" , "cli" , "main" )
1616default_func_names = ("main" , "cli" , "app" )
@@ -199,8 +199,12 @@ def get_docs_for_click(
199199 if not title :
200200 title = f"`{ command_name } `" if command_name else "CLI"
201201 docs += f" { title } \n \n "
202+ rich_markup_mode = None
203+ if hasattr (ctx , "obj" ) and isinstance (ctx .obj , dict ):
204+ rich_markup_mode = ctx .obj .get (MARKUP_MODE_KEY , None )
205+ to_parse : bool = bool (HAS_RICH and (rich_markup_mode == "rich" ))
202206 if obj .help :
203- docs += f"{ _parse_html (obj .help )} \n \n "
207+ docs += f"{ _parse_html (to_parse , obj .help )} \n \n "
204208 usage_pieces = obj .collect_usage_pieces (ctx )
205209 if usage_pieces :
206210 docs += "**Usage**:\n \n "
@@ -224,15 +228,15 @@ def get_docs_for_click(
224228 for arg_name , arg_help in args :
225229 docs += f"* `{ arg_name } `"
226230 if arg_help :
227- docs += f": { _parse_html (arg_help )} "
231+ docs += f": { _parse_html (to_parse , arg_help )} "
228232 docs += "\n "
229233 docs += "\n "
230234 if opts :
231235 docs += "**Options**:\n \n "
232236 for opt_name , opt_help in opts :
233237 docs += f"* `{ opt_name } `"
234238 if opt_help :
235- docs += f": { _parse_html (opt_help )} "
239+ docs += f": { _parse_html (to_parse , opt_help )} "
236240 docs += "\n "
237241 docs += "\n "
238242 if obj .epilog :
@@ -248,7 +252,7 @@ def get_docs_for_click(
248252 docs += f"* `{ command_obj .name } `"
249253 command_help = command_obj .get_short_help_str ()
250254 if command_help :
251- docs += f": { _parse_html (command_help )} "
255+ docs += f": { _parse_html (to_parse , command_help )} "
252256 docs += "\n "
253257 docs += "\n "
254258 for command in commands :
@@ -263,8 +267,8 @@ def get_docs_for_click(
263267 return docs
264268
265269
266- def _parse_html (input_text : str ) -> str :
267- if not HAS_RICH : # pragma: no cover
270+ def _parse_html (to_parse : bool , input_text : str ) -> str :
271+ if not to_parse :
268272 return input_text
269273 from . import rich_utils
270274
@@ -294,6 +298,11 @@ def docs(
294298 if not typer_obj :
295299 typer .echo ("No Typer app found" , err = True )
296300 raise typer .Abort ()
301+ if hasattr (typer_obj , "rich_markup_mode" ):
302+ if not hasattr (ctx , "obj" ) or ctx .obj is None :
303+ ctx .ensure_object (dict )
304+ if isinstance (ctx .obj , dict ):
305+ ctx .obj [MARKUP_MODE_KEY ] = typer_obj .rich_markup_mode
297306 click_obj = typer .main .get_command (typer_obj )
298307 docs = get_docs_for_click (obj = click_obj , ctx = ctx , name = name , title = title )
299308 clean_docs = f"{ docs .strip ()} \n "
0 commit comments