diff --git a/src/markdown_exec/__init__.py b/src/markdown_exec/__init__.py index 13a6af3..8269e96 100644 --- a/src/markdown_exec/__init__.py +++ b/src/markdown_exec/__init__.py @@ -46,6 +46,7 @@ def validator( options["exec"] = exec_value options["isolate"] = isolate_value options["show_source"] = show_source_value + options["extra"] = inputs return True diff --git a/src/markdown_exec/markdown_helpers.py b/src/markdown_exec/markdown_helpers.py index 6b7b631..4e3bfb0 100644 --- a/src/markdown_exec/markdown_helpers.py +++ b/src/markdown_exec/markdown_helpers.py @@ -5,17 +5,19 @@ from textwrap import indent -def code_block(language: str, source: str) -> str: +def code_block(language: str, source: str, **options: str) -> str: """Format source as a code block. Parameters: language: The code block language. source: The source code to format. + **options: Additional options passed from the source, to add back to the generated code block. Returns: The formatted code block. """ - return f"```{language}\n{source}\n```" + opts = " ".join(f'{opt_name}="{opt_value}"' for opt_name, opt_value in options.items()) + return f"```{language} {opts}\n{source}\n```" def tabbed(*tabs: tuple[str, str]) -> str: diff --git a/src/markdown_exec/python.py b/src/markdown_exec/python.py index 20d341e..08c57c3 100644 --- a/src/markdown_exec/python.py +++ b/src/markdown_exec/python.py @@ -71,6 +71,7 @@ def exec_python( # noqa: WPS231 exec_source = f"def _function():\n{indent(source, prefix=' ' * 4)}\n_function()\n" else: exec_source = source + extra = options.get("extra", {}) try: exec(exec_source) # noqa: S102 except MarkdownOutput as raised_output: @@ -78,9 +79,9 @@ def exec_python( # noqa: WPS231 except HTMLOutput as raised_output: output = f'
{str(raised_output)}
' except Exception: - output = code_block("python", traceback.format_exc()) + output = code_block("python", traceback.format_exc(), **extra) if show_source: - source_block = code_block("python", source) + source_block = code_block("python", source, **extra) if show_source == "above": output = source_block + "\n\n" + output elif show_source == "below":