Skip to content

Commit

Permalink
fix: Remove pycon output lines when rendering source as console
Browse files Browse the repository at this point in the history
  • Loading branch information
pawamoy committed Apr 17, 2023
1 parent a110d44 commit fb5a23d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/markdown_exec/formatters/pycon.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@

def _transform_source(code: str) -> tuple[str, str]:
python_lines = []
pycon_lines = []
for line in code.split("\n"):
if line.startswith((">>> ", "... ")):
pycon_lines.append(line)
python_lines.append(line[4:])
python_code = "\n".join(python_lines)
return python_code, code
return python_code, "\n".join(pycon_lines)


def _format_pycon(**kwargs: Any) -> Markup:
Expand Down
20 changes: 20 additions & 0 deletions tests/test_python.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,23 @@ def fraise():
assert "strawberry" in html
assert "fraise()" in caplog.text
assert 'raise RuntimeError("strawberry")' in caplog.text


def test_removing_output_from_pycon_code(md: Markdown) -> None:
"""Assert output lines are removed from pycon snippets.
Parameters:
md: A Markdown instance (fixture).
"""
html = md.convert(
dedent(
"""
```pycon exec="1" source="console"
>>> print("ok")
ko
```
""",
),
)
assert "ok" in html
assert "ko" not in html

0 comments on commit fb5a23d

Please sign in to comment.