-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Add reusable base formatter
- Loading branch information
Showing
5 changed files
with
63 additions
and
111 deletions.
There are no files selected for viewing
Empty file.
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,49 @@ | ||
"""Generic formatter for executing code.""" | ||
|
||
from __future__ import annotations | ||
|
||
from typing import Any, Callable | ||
from uuid import uuid4 | ||
|
||
from markdown.core import Markdown | ||
from markupsafe import Markup | ||
|
||
from markdown_exec.rendering import add_source, markdown | ||
|
||
|
||
def base_format( # noqa: WPS231 | ||
language: str, | ||
run: Callable, | ||
code: str, | ||
md: Markdown, | ||
html: bool, | ||
source: str, | ||
tabs: tuple[str, str], | ||
**options: Any, | ||
) -> Markup: | ||
"""Execute code and return HTML. | ||
Parameters: | ||
language: The code language. | ||
run: Function that runs code and returns output. | ||
code: The code to execute. | ||
md: The Markdown instance. | ||
html: Whether to inject output as HTML directly, without rendering. | ||
source: Whether to show source as well, and where. | ||
tabs: Titles of tabs (if used). | ||
**options: Additional options passed from the formatter. | ||
Returns: | ||
HTML contents. | ||
""" | ||
markdown.setup(md) | ||
extra = options.get("extra", {}) | ||
output = run(code, **extra) | ||
stash = {} | ||
if html: | ||
placeholder = str(uuid4()) | ||
stash[placeholder] = output | ||
output = placeholder | ||
if source: | ||
output = add_source(source=code, location=source, output=output, language=language, 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 |
---|---|---|
@@ -1,44 +1,9 @@ | ||
"""Formatter for reapply literate Markdown.""" | ||
"""Formatter for literate Markdown.""" | ||
|
||
from __future__ import annotations | ||
|
||
from typing import Any | ||
from uuid import uuid4 | ||
from markdown_exec.formatters.base import base_format | ||
|
||
from markdown.core import Markdown | ||
|
||
from markdown_exec.rendering import add_source, markdown | ||
|
||
|
||
def format_markdown( # noqa: WPS231 | ||
code: str, | ||
md: Markdown, | ||
html: bool, | ||
source: str, | ||
tabs: tuple[str, str], | ||
**options: Any, | ||
) -> str: | ||
"""Reapply Markdown and return HTML. | ||
Parameters: | ||
code: The code to execute. | ||
md: The Markdown instance. | ||
html: Whether to inject output as HTML directly, without rendering. | ||
source: Whether to show source as well, and where. | ||
tabs: Titles of tabs (if used). | ||
**options: Additional options passed from the formatter. | ||
Returns: | ||
HTML contents. | ||
""" | ||
markdown.setup(md) | ||
extra = options.get("extra", {}) | ||
output = code | ||
stash = {} | ||
if html: | ||
placeholder = str(uuid4()) | ||
stash[placeholder] = output | ||
output = placeholder | ||
if source: | ||
output = add_source(source=code, location=source, output=output, language="md", tabs=tabs, **extra) | ||
return markdown.convert(output, stash=stash) | ||
def _format_markdown(*args, **kwargs) -> str: | ||
return base_format("md", lambda code, **_: code, *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
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