-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add support for shell code blocks
- Loading branch information
Showing
9 changed files
with
166 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# Python | ||
|
||
## Python console code | ||
|
||
Code blocks syntax-highlighted with the `pycon` identifier are also supported. | ||
These code blocks will be pre-processed to keep only the lines | ||
starting with `>>> `, and the chevrons (prompt) will be removed from these lines, | ||
so we can execute them. | ||
|
||
````md exec="1" source="tabbed-left" tabs="Markdown|Rendered" | ||
```pycon exec="1" source="console" | ||
--8<-- "source.pycon" | ||
``` | ||
```` | ||
|
||
It also means that multiple blocks of instructions will be concatenated, | ||
as well as their output: | ||
|
||
````md exec="1" source="tabbed-left" tabs="Markdown|Rendered" | ||
```pycon exec="1" source="console" | ||
--8<-- "multiple.pycon" | ||
``` | ||
```` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Shell | ||
|
||
Shell code blocks are executed using the same interpreter specified | ||
as language of the code block, in sub-processes. The output is captured | ||
and rendered as Markdown or HTML (see [Usage](../#html-vs-markdown)). | ||
|
||
## Bash | ||
|
||
````md exec="1" source="tabbed-left" tabs="Markdown|Rendered" | ||
```sh exec="1" source="material-block" | ||
pdm --version | ||
``` | ||
```` | ||
|
||
## Console | ||
|
||
````md exec="1" source="tabbed-left" tabs="Markdown|Rendered" | ||
```console exec="1" source="console" | ||
$ pdm --version | ||
``` | ||
```` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
"""Formatter for executing shell code.""" | ||
|
||
from __future__ import annotations | ||
|
||
import subprocess # noqa: S404 | ||
|
||
from markdown_exec.formatters.base import base_format | ||
from markdown_exec.rendering import code_block | ||
|
||
|
||
def _run_bash(code: str, **extra: str) -> str: | ||
try: | ||
output = subprocess.check_output(["bash", "-c", code]).decode() # noqa: S603,S607 | ||
except subprocess.CalledProcessError as error: | ||
return code_block("bash", error.output, **extra) | ||
return output | ||
|
||
|
||
def _format_bash(*args, **kwargs) -> str: | ||
return base_format("bash", _run_bash, *args, **kwargs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
"""Formatter for executing shell console code.""" | ||
|
||
from __future__ import annotations | ||
|
||
import textwrap | ||
from typing import Any | ||
from uuid import uuid4 | ||
|
||
from markdown.core import Markdown | ||
|
||
from markdown_exec.formatters.sh import _run_sh # noqa: WPS450 | ||
from markdown_exec.rendering import add_source, markdown | ||
|
||
|
||
def _format_console( # noqa: WPS231 | ||
code: str, | ||
md: Markdown, | ||
html: bool, | ||
source: str, | ||
tabs: tuple[str, str], | ||
**options: Any, | ||
) -> str: | ||
markdown.setup(md) | ||
|
||
sh_lines = [] | ||
for line in code.split("\n"): | ||
if line.startswith("$ "): | ||
sh_lines.append(line[2:]) | ||
sh_code = "\n".join(sh_lines) | ||
|
||
extra = options.get("extra", {}) | ||
output = _run_sh(sh_code, **extra) | ||
stash = {} | ||
if html: | ||
placeholder = str(uuid4()) | ||
stash[placeholder] = output | ||
output = placeholder | ||
if source: | ||
source_code = textwrap.indent(sh_code, "$ ") | ||
output = add_source(source=source_code, location=source, output=output, language="console", tabs=tabs, **extra) | ||
return markdown.convert(output, stash=stash) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
"""Formatter for executing shell code.""" | ||
|
||
from __future__ import annotations | ||
|
||
import subprocess # noqa: S404 | ||
|
||
from markdown_exec.formatters.base import base_format | ||
from markdown_exec.rendering import code_block | ||
|
||
|
||
def _run_sh(code: str, **extra: str) -> str: | ||
try: | ||
output = subprocess.check_output(["sh", "-c", code]).decode() # noqa: S603,S607 | ||
except subprocess.CalledProcessError as error: | ||
return code_block("sh", error.output, **extra) | ||
return output | ||
|
||
|
||
def _format_sh(*args, **kwargs) -> str: | ||
return base_format("sh", _run_sh, *args, **kwargs) |