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

🐛 Fix markdown formatting #815

Draft
wants to merge 14 commits into
base: master
Choose a base branch
from
Draft
147 changes: 147 additions & 0 deletions tests/test_rich_markup_mode.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from typing import List

import pytest
import typer
import typer.completion
from typer.testing import CliRunner
Expand Down Expand Up @@ -38,3 +41,147 @@ def main(arg: str):

result = runner.invoke(app, ["--help"])
assert any(c in result.stdout for c in rounded)


@pytest.mark.parametrize(
"mode,lines",
[
("markdown", ["First line", "Line 1", "", "Line 2", "", "Line 3", ""]),
("rich", ["First line", "", "Line 1", "", "Line 2", "", "Line 3", ""]),
("none", ["First line", "", "Line 1", "Line 2", "Line 3", ""]),
],
)
def test_markup_mode_newline_pr815(mode: str, lines: List[str]):
app = typer.Typer(rich_markup_mode=mode)

@app.command()
def main(arg: str):
"""First line

Line 1

Line 2

Line 3
"""
print(f"Hello {arg}")

assert app.rich_markup_mode == mode

result = runner.invoke(app, ["World"])
assert "Hello World" in result.stdout

result = runner.invoke(app, ["--help"])
result_lines = [line.strip() for line in result.stdout.split("\n")]
assert any(c in result.stdout for c in rounded)
help_start = result_lines.index("First line")
arg_start = [i for i, row in enumerate(result_lines) if "Arguments" in row][0]
assert help_start != -1
assert result_lines[help_start:arg_start] == lines


@pytest.mark.parametrize(
"mode,lines",
[
("markdown", ["First line", "Line 1 Line 2 Line 3", ""]),
("rich", ["First line", "", "Line 1", "Line 2", "Line 3", ""]),
("none", ["First line", "", "Line 1 Line 2 Line 3", ""]),
],
)
def test_markup_mode_newline_issue447(mode: str, lines: List[str]):
app = typer.Typer(rich_markup_mode=mode)

@app.command()
def main(arg: str):
"""First line

Line 1
Line 2
Line 3
"""
print(f"Hello {arg}")

assert app.rich_markup_mode == mode

result = runner.invoke(app, ["World"])
assert "Hello World" in result.stdout

result = runner.invoke(app, ["--help"])
result_lines = [line.strip() for line in result.stdout.split("\n")]
assert any(c in result.stdout for c in rounded)
help_start = result_lines.index("First line")
arg_start = [i for i, row in enumerate(result_lines) if "Arguments" in row][0]
assert help_start != -1
assert result_lines[help_start:arg_start] == lines


@pytest.mark.parametrize(
"mode,lines",
[
("markdown", ["First line", "", "• 1", "• 2", "• 3", ""]),
("rich", ["First line", "", "- 1", "- 2", "- 3", ""]),
("none", ["First line", "", "- 1 - 2 - 3", ""]),
],
)
def test_markup_mode_bullets(mode: str, lines: List[str]):
app = typer.Typer(rich_markup_mode=mode)

@app.command()
def main(arg: str):
"""First line

- 1
- 2
- 3
"""
print(f"Hello {arg}")

assert app.rich_markup_mode == mode

result = runner.invoke(app, ["World"])
assert "Hello World" in result.stdout

result = runner.invoke(app, ["--help"])
result_lines = [line.strip() for line in result.stdout.split("\n")]
assert any(c in result.stdout for c in rounded)
help_start = result_lines.index("First line")
arg_start = [i for i, row in enumerate(result_lines) if "Arguments" in row][0]
assert help_start != -1
assert result_lines[help_start:arg_start] == lines


@pytest.mark.parametrize(
"mode,lines",
[
("markdown", ["First line", "", "• 1", "• 2", "• a", "• b", "• 3", ""]),
("rich", ["First line", "", "- 1", "- 2", "- a", "- b", "- 3", ""]),
("none", ["First line", "", "- 1 - 2 - a - b - 3", ""]),
],
)
def test_markup_mode_nested_bullets(mode: str, lines: List[str]):
app = typer.Typer(rich_markup_mode=mode)

@app.command()
def main(arg: str):
"""First line

- 1
- 2
- a
- b
- 3
"""
print(f"Hello {arg}")

assert app.rich_markup_mode == mode

result = runner.invoke(app, ["World"])
assert "Hello World" in result.stdout

result = runner.invoke(app, ["--help"])
result_lines = [line.strip() for line in result.stdout.split("\n")]
assert any(c in result.stdout for c in rounded)
help_start = result_lines.index("First line")
arg_start = [i for i, row in enumerate(result_lines) if "Arguments" in row][0]
assert help_start != -1
assert result_lines[help_start:arg_start] == lines
10 changes: 6 additions & 4 deletions typer/rich_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,8 @@ 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", " ")
Expand All @@ -201,9 +202,10 @@ def _get_help_text(
)

# 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:
if markup_mode != MARKUP_MODE_MARKDOWN:
yield Text("")
if markup_mode not in (MARKUP_MODE_RICH, MARKUP_MODE_MARKDOWN):
# Remove single linebreaks
remaining_paragraphs = [
x.replace("\n", " ").strip()
Expand All @@ -214,7 +216,7 @@ def _get_help_text(
# Join back together
remaining_lines = "\n".join(remaining_paragraphs)
else:
# Join with double linebreaks if markdown
# Join with double linebreaks if markdown or Rich markup
remaining_lines = "\n\n".join(remaining_paragraphs)

yield _make_rich_text(
Expand Down
Loading