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

1"""Formatter for executing shell console code.""" 

2 

3from __future__ import annotations 

4 

5import textwrap 

6from typing import TYPE_CHECKING, Any 

7 

8from markdown_exec.formatters.base import base_format 

9from markdown_exec.formatters.sh import _run_sh 

10from markdown_exec.logger import get_logger 

11 

12if TYPE_CHECKING: 

13 from markupsafe import Markup 

14 

15logger = get_logger(__name__) 

16 

17 

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) 

26 

27 

28def _format_console(**kwargs: Any) -> Markup: 

29 return base_format(language="console", run=_run_sh, transform_source=_transform_source, **kwargs)