Skip to content

Commit

Permalink
fix: Render source for non-HTML output (regression)
Browse files Browse the repository at this point in the history
  • Loading branch information
pawamoy committed Nov 13, 2022
1 parent fa7b0fa commit 3028dcd
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 13 deletions.
29 changes: 17 additions & 12 deletions src/markdown_exec/formatters/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,22 +43,27 @@ def base_format( # noqa: WPS231
"""
markdown = MarkdownConverter(md)
extra = options.get("extra", {})

try:
output = run(code, **extra)
except RuntimeError as error:
logger.warning(f"Execution of {language} code block exited with non-zero status")
return markdown.convert(str(error))
if html and source:
placeholder = str(uuid4())

if html:
if source:
placeholder = str(uuid4())
wrapped_output = add_source(
source=code, location=source, output=placeholder, language=language, tabs=tabs, **extra
)
return markdown.convert(wrapped_output, stash={placeholder: output})
return Markup(output)

wrapped_output = output
if result:
wrapped_output = code_block(result, output)
if source:
wrapped_output = add_source(
source=code, location=source, output=placeholder, language=language, tabs=tabs, **extra
source=code, location=source, output=wrapped_output, language=language, tabs=tabs, **extra
)
markup = markdown.convert(wrapped_output, stash={placeholder: output})
elif html:
markup = Markup(output)
elif result:
wrapped_output = code_block(result, output)
markup = markdown.convert(wrapped_output)
else:
markup = markdown.convert(output)
return markup
return markdown.convert(wrapped_output)
14 changes: 13 additions & 1 deletion tests/test_base_formatter.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Tests for the base formatter."""


import pytest
from markdown import Markdown

from markdown_exec.formatters.base import base_format
Expand All @@ -15,3 +15,15 @@ def test_no_p_around_html(md: Markdown) -> None:
code = "<pre><code>hello</code></pre>"
html = base_format("whatever", lambda _: _, code, md, html=True, source="", result="", tabs=("", ""))
assert html == code


@pytest.mark.parametrize("html", [True, False])
def test_render_source(md: Markdown, html: bool) -> None:
"""Assert source is rendered.
Parameters:
md: A Markdown instance (fixture).
html: Whether output is HTML or not.
"""
markup = base_format("python", lambda _: _, "hello", md, html, "tabbed-left", "", ("Source", "Output"))
assert "Source" in markup

0 comments on commit 3028dcd

Please # to comment.