Skip to content

Commit 62dfd74

Browse files
committed
feat: Add console source integration option
1 parent 5fce814 commit 62dfd74

2 files changed

Lines changed: 37 additions & 11 deletions

File tree

src/markdown_exec/python.py

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
from markdown.core import Markdown
1111

12-
from markdown_exec.rendering import code_block, markdown, tabbed
12+
from markdown_exec.rendering import add_source, code_block, markdown
1313

1414

1515
def buffer_print(buffer: StringIO, *text: str, end: str = "\n", **kwargs: Any) -> None:
@@ -68,14 +68,5 @@ def exec_python( # noqa: WPS231
6868
output = f'<div markdown="0">{str(output)}</div>'
6969

7070
if source:
71-
source_block = code_block("python", code, **extra)
72-
if source == "above":
73-
output = source_block + "\n\n" + output
74-
elif source == "below":
75-
output = output + "\n\n" + source_block
76-
elif source == "tabbed-left":
77-
output = tabbed((source_tab_title, source_block), (result_tab_title, output))
78-
elif source == "tabbed-right":
79-
output = tabbed((result_tab_title, output), (source_tab_title, source_block))
80-
71+
output = add_source(source=code, location=source, output=output, language="python", tabs=tabs, **extra)
8172
return markdown.convert(output)

src/markdown_exec/rendering.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,41 @@ def tabbed(*tabs: tuple[str, str]) -> str:
4343
return "\n".join(parts)
4444

4545

46+
def add_source(*, source: str, location: str, output: str, language: str, tabs: tuple[str, str], **extra: str) -> str:
47+
"""Add source code block to the output.
48+
49+
Parameters:
50+
source: The source code block.
51+
location: Where to add the source (above, below, tabbed-left, tabbed-right, console).
52+
output: The current output.
53+
language: The code language.
54+
tabs: Tabs titles (if used).
55+
**extra: Extra options added back to source code block.
56+
57+
Raises:
58+
ValueError: When the given location is not supported.
59+
60+
Returns:
61+
The updated output.
62+
"""
63+
if location == "console":
64+
return code_block(language, source + "\n" + output, **extra)
65+
66+
source_block = code_block(language, source, **extra)
67+
if location == "above":
68+
return source_block + "\n\n" + output
69+
if location == "below":
70+
return output + "\n\n" + source_block
71+
72+
source_tab_title, result_tab_title = tabs
73+
if location == "tabbed-left":
74+
return tabbed((source_tab_title, source_block), (result_tab_title, output))
75+
if location == "tabbed-right":
76+
return tabbed((result_tab_title, output), (source_tab_title, source_block))
77+
78+
raise ValueError(f"unsupported location for sources: {location}")
79+
80+
4681
# code taken from mkdocstrings, credits to @oprypin
4782
class _IdPrependingTreeprocessor(Treeprocessor):
4883
"""Prepend the configured prefix to IDs of all HTML elements."""

0 commit comments

Comments
 (0)