Coverage for src/markdown_exec/formatters/console.py: 42.86%
17 statements
« prev ^ index » next coverage.py v7.2.3, created at 2023-04-16 20:19 +0200
« prev ^ index » next coverage.py v7.2.3, created at 2023-04-16 20:19 +0200
1"""Formatter for executing shell console code."""
3from __future__ import annotations
5import textwrap
6from typing import TYPE_CHECKING, Any
8from markdown_exec.formatters.base import base_format
9from markdown_exec.formatters.sh import _run_sh
10from markdown_exec.logger import get_logger
12if TYPE_CHECKING:
13 from markupsafe import Markup
15logger = get_logger(__name__)
18def _transform_source(code: str) -> tuple[str, str]:
19 sh_lines = []
20 for line in code.split("\n"):
21 prompt = line[:2]
22 if prompt in {"$ ", "% "}:
23 sh_lines.append(line[2:])
24 sh_code = "\n".join(sh_lines)
25 return sh_code, textwrap.indent(sh_code, prompt)
28def _format_console(**kwargs: Any) -> Markup:
29 return base_format(language="console", run=_run_sh, transform_source=_transform_source, **kwargs)