Skip to content

feat(formatting) allow empty string in result to imply code format #81

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/markdown_exec/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def validator(
id_prefix_value = inputs.pop("idprefix", None)
html_value = _to_bool(inputs.pop("html", "no"))
source_value = inputs.pop("source", "")
result_value = inputs.pop("result", "")
result_value = inputs.pop("result", None)
returncode_value = int(inputs.pop("returncode", "0"))
session_value = inputs.pop("session", "")
update_toc_value = _to_bool(inputs.pop("updatetoc", "yes"))
Expand Down
4 changes: 2 additions & 2 deletions src/markdown_exec/formatters/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def base_format(
md: Markdown,
html: bool = False,
source: str = "",
result: str = "",
result: str = None,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
result: str = None,
result: str | None = None,

tabs: tuple[str, str] = default_tabs,
id: str = "", # noqa: A002
id_prefix: str | None = None,
Expand Down Expand Up @@ -172,7 +172,7 @@ def base_format(
return Markup(output)

wrapped_output = output
if result and source != "console":
if result is not None and source != "console":
wrapped_output = code_block(result, output)
if source:
wrapped_output = add_source(
Expand Down
16 changes: 16 additions & 0 deletions tests/test_base_formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,19 @@ def test_console_width(md: Markdown) -> None:
width=width,
)
assert f"width: {width}" in markup


def test_render_as_code_for_empty_string(md: Markdown) -> None:
"""Assert backticks are added for empty string

Parameters:
md: A Markdown instance (fixture).
"""
markup = base_format(
language="python",
run=lambda code, **_: code,
code="print('hello')",
result="",
md=md,
)
assert "<code>" in markup
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like it should assert a bit more. Were you able to confirm the test didn't pass without the change?

Loading