From c347a7b2e7c61d92436aa06991e7600de90e8f6d Mon Sep 17 00:00:00 2001 From: Garrett Smith Date: Tue, 30 Apr 2024 13:03:15 -0500 Subject: [PATCH] Fix markdown formatting Includes fix to typo in function name. Ref: #447 --- tests/test_rich_utils.py | 26 ++++++++++++++++++++++++++ typer/rich_utils.py | 19 ++++++++++--------- 2 files changed, 36 insertions(+), 9 deletions(-) diff --git a/tests/test_rich_utils.py b/tests/test_rich_utils.py index c62e3512aa..5459fed788 100644 --- a/tests/test_rich_utils.py +++ b/tests/test_rich_utils.py @@ -50,3 +50,29 @@ def main() -> None: assert result.exit_code == 0 assert "Show this message" in result.stdout + + +def test_markdown_pars(): + app = typer.Typer(rich_markup_mode="markdown") + + @app.command() + def main(): + """First line + + Line 1 + + Line 2 + """ + + result = runner.invoke(app, ["--help"]) + lines = [line.strip() for line in result.stdout.split("\n")] + help_start = lines.index("First line") + assert help_start != -1 + assert lines[help_start : help_start + 6] == [ + "First line", + "", + "Line 1", + "", + "Line 2", + "", + ] diff --git a/typer/rich_utils.py b/typer/rich_utils.py index cf0538e914..798f9b2996 100644 --- a/typer/rich_utils.py +++ b/typer/rich_utils.py @@ -144,7 +144,7 @@ def _get_rich_console(stderr: bool = False) -> Console: ) -def _make_rich_rext( +def _make_rich_text( *, text: str, style: str = "", markup_mode: MarkupMode ) -> Union[Markdown, Text]: """Take a string, remove indentations, and return styled text. @@ -190,20 +190,21 @@ def _get_help_text( help_text = help_text.partition("\f")[0] # Get the first paragraph - first_line = help_text.split("\n\n")[0] + first_line, *remaining_paragraphs = help_text.split("\n\n") + # Remove single linebreaks if markup_mode != MARKUP_MODE_MARKDOWN and not first_line.startswith("\b"): first_line = first_line.replace("\n", " ") - yield _make_rich_rext( + yield _make_rich_text( text=first_line.strip(), style=STYLE_HELPTEXT_FIRST_LINE, markup_mode=markup_mode, ) # Get remaining lines, remove single line breaks and format as dim - remaining_paragraphs = help_text.split("\n\n")[1:] if remaining_paragraphs: - if markup_mode != MARKUP_MODE_RICH: + yield Text("") + if markup_mode not in (MARKUP_MODE_RICH, MARKUP_MODE_MARKDOWN): # Remove single linebreaks remaining_paragraphs = [ x.replace("\n", " ").strip() @@ -217,7 +218,7 @@ def _get_help_text( # Join with double linebreaks if markdown remaining_lines = "\n\n".join(remaining_paragraphs) - yield _make_rich_rext( + yield _make_rich_text( text=remaining_lines, style=STYLE_HELPTEXT, markup_mode=markup_mode, @@ -272,7 +273,7 @@ def _get_parameter_help( for x in paragraphs ] items.append( - _make_rich_rext( + _make_rich_text( text="\n".join(paragraphs).strip(), style=STYLE_OPTION_HELP, markup_mode=markup_mode, @@ -331,7 +332,7 @@ def _make_command_help( paragraphs[0] = paragraphs[0].replace("\n", " ") elif paragraphs[0].startswith("\b"): paragraphs[0] = paragraphs[0].replace("\b\n", "") - return _make_rich_rext( + return _make_rich_text( text=paragraphs[0].strip(), style=STYLE_OPTION_HELP, markup_mode=markup_mode, @@ -674,7 +675,7 @@ def rich_format_help( # Remove single linebreaks, replace double with single lines = obj.epilog.split("\n\n") epilogue = "\n".join([x.replace("\n", " ").strip() for x in lines]) - epilogue_text = _make_rich_rext(text=epilogue, markup_mode=markup_mode) + epilogue_text = _make_rich_text(text=epilogue, markup_mode=markup_mode) console.print(Padding(Align(epilogue_text, pad=False), 1))