Coverage for src/markdown_exec/formatters/pycon.py: 100.00%
17 statements
« prev ^ index » next coverage.py v7.2.3, created at 2023-04-17 16:01 +0200
« prev ^ index » next coverage.py v7.2.3, created at 2023-04-17 16:01 +0200
1"""Formatter for executing `pycon` code."""
3from __future__ import annotations
5from typing import TYPE_CHECKING, Any
7from markdown_exec.formatters.base import base_format
8from markdown_exec.formatters.python import _run_python
9from markdown_exec.logger import get_logger
11if TYPE_CHECKING:
12 from markupsafe import Markup
14logger = get_logger(__name__)
17def _transform_source(code: str) -> tuple[str, str]:
18 python_lines = []
19 pycon_lines = []
20 for line in code.split("\n"):
21 if line.startswith((">>> ", "... ")):
22 pycon_lines.append(line)
23 python_lines.append(line[4:])
24 python_code = "\n".join(python_lines)
25 return python_code, "\n".join(pycon_lines)
28def _format_pycon(**kwargs: Any) -> Markup:
29 return base_format(language="pycon", run=_run_python, transform_source=_transform_source, **kwargs)