Skip to content

Commit b132e78

Browse files
committed
refactor: Move code under internal folder
1 parent ec83f48 commit b132e78

File tree

24 files changed

+253
-218
lines changed

24 files changed

+253
-218
lines changed

docs/reference/api.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@ hide:
55
---
66

77
# ::: markdown_exec
8+
options:
9+
show_submodules: true

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ Gitter = "https://gitter.im/markdown-exec/community"
4747
Funding = "https://github.com/sponsors/pawamoy"
4848

4949
[project.entry-points."mkdocs.plugins"]
50-
markdown-exec = "markdown_exec.mkdocs_plugin:MarkdownExecPlugin"
50+
markdown-exec = "markdown_exec:MarkdownExecPlugin"
5151

5252
[tool.pdm.version]
5353
source = "call"

src/markdown_exec/__init__.py

Lines changed: 50 additions & 138 deletions
Original file line numberDiff line numberDiff line change
@@ -3,141 +3,53 @@
33
Utilities to execute code blocks in Markdown files.
44
"""
55

6-
# https://facelessuser.github.io/pymdown-extensions/extensions/superfences/#custom-fences
7-
# https://squidfunk.github.io/mkdocs-material/setup/extensions/python-markdown-extensions/#snippets
8-
9-
from __future__ import annotations
10-
11-
import os
12-
import re
13-
from typing import TYPE_CHECKING, Any
14-
15-
if TYPE_CHECKING:
16-
from markdown import Markdown
17-
18-
from markdown_exec.formatters.base import default_tabs
19-
from markdown_exec.formatters.bash import _format_bash
20-
from markdown_exec.formatters.console import _format_console
21-
from markdown_exec.formatters.markdown import _format_markdown
22-
from markdown_exec.formatters.pycon import _format_pycon
23-
from markdown_exec.formatters.pyodide import _format_pyodide
24-
from markdown_exec.formatters.python import _format_python
25-
from markdown_exec.formatters.sh import _format_sh
26-
from markdown_exec.formatters.tree import _format_tree
27-
28-
__all__: list[str] = ["formatter", "validator"]
29-
30-
MARKDOWN_EXEC_AUTO = [lang.strip() for lang in os.getenv("MARKDOWN_EXEC_AUTO", "").split(",")]
31-
32-
formatters = {
33-
"bash": _format_bash,
34-
"console": _format_console,
35-
"md": _format_markdown,
36-
"markdown": _format_markdown,
37-
"py": _format_python,
38-
"python": _format_python,
39-
"pycon": _format_pycon,
40-
"pyodide": _format_pyodide,
41-
"sh": _format_sh,
42-
"tree": _format_tree,
43-
}
44-
45-
# negative look behind: matches only if | (pipe) if not preceded by \ (backslash)
46-
_tabs_re = re.compile(r"(?<!\\)\|")
47-
48-
49-
def validator(
50-
language: str,
51-
inputs: dict[str, str],
52-
options: dict[str, Any],
53-
attrs: dict[str, Any], # noqa: ARG001
54-
md: Markdown, # noqa: ARG001
55-
) -> bool:
56-
"""Validate code blocks inputs.
57-
58-
Parameters:
59-
language: The code language, like python or bash.
60-
inputs: The code block inputs, to be sorted into options and attrs.
61-
options: The container for options.
62-
attrs: The container for attrs:
63-
md: The Markdown instance.
64-
65-
Returns:
66-
Success or not.
67-
"""
68-
exec_value = language in MARKDOWN_EXEC_AUTO or _to_bool(inputs.pop("exec", "no"))
69-
if language not in {"tree", "pyodide"} and not exec_value:
70-
return False
71-
id_value = inputs.pop("id", "")
72-
id_prefix_value = inputs.pop("idprefix", None)
73-
html_value = _to_bool(inputs.pop("html", "no"))
74-
source_value = inputs.pop("source", "")
75-
result_value = inputs.pop("result", "")
76-
returncode_value = int(inputs.pop("returncode", "0"))
77-
session_value = inputs.pop("session", "")
78-
update_toc_value = _to_bool(inputs.pop("updatetoc", "yes"))
79-
tabs_value = inputs.pop("tabs", "|".join(default_tabs))
80-
tabs = tuple(_tabs_re.split(tabs_value, maxsplit=1))
81-
workdir_value = inputs.pop("workdir", None)
82-
width_value = int(inputs.pop("width", "0"))
83-
options["id"] = id_value
84-
options["id_prefix"] = id_prefix_value
85-
options["html"] = html_value
86-
options["source"] = source_value
87-
options["result"] = result_value
88-
options["returncode"] = returncode_value
89-
options["session"] = session_value
90-
options["update_toc"] = update_toc_value
91-
options["tabs"] = tabs
92-
options["workdir"] = workdir_value
93-
options["width"] = width_value
94-
options["extra"] = inputs
95-
return True
96-
97-
98-
def formatter(
99-
source: str,
100-
language: str,
101-
css_class: str, # noqa: ARG001
102-
options: dict[str, Any],
103-
md: Markdown,
104-
classes: list[str] | None = None, # noqa: ARG001
105-
id_value: str = "", # noqa: ARG001
106-
attrs: dict[str, Any] | None = None, # noqa: ARG001
107-
**kwargs: Any, # noqa: ARG001
108-
) -> str:
109-
"""Execute code and return HTML.
110-
111-
Parameters:
112-
source: The code to execute.
113-
language: The code language, like python or bash.
114-
css_class: The CSS class to add to the HTML element.
115-
options: The container for options.
116-
attrs: The container for attrs:
117-
md: The Markdown instance.
118-
classes: Additional CSS classes.
119-
id_value: An optional HTML id.
120-
attrs: Additional attributes
121-
**kwargs: Additional arguments passed to SuperFences default formatters.
122-
123-
Returns:
124-
HTML contents.
125-
"""
126-
fmt = formatters.get(language, lambda source, **kwargs: source)
127-
return fmt(code=source, md=md, **options) # type: ignore[operator]
128-
129-
130-
falsy_values = {"", "no", "off", "false", "0"}
131-
truthy_values = {"yes", "on", "true", "1"}
132-
133-
134-
def _to_bool(value: str) -> bool:
135-
return value.lower() not in falsy_values
136-
137-
138-
def _to_bool_or_value(value: str) -> bool | str:
139-
if value.lower() in falsy_values:
140-
return False
141-
if value.lower() in truthy_values:
142-
return True
143-
return value
6+
from markdown_exec._internal.formatters.base import (
7+
ExecutionError,
8+
base_format,
9+
console_width,
10+
default_tabs,
11+
working_directory,
12+
)
13+
from markdown_exec._internal.logger import get_logger, patch_loggers
14+
from markdown_exec._internal.main import MARKDOWN_EXEC_AUTO, formatter, formatters, validator
15+
from markdown_exec._internal.mkdocs_plugin import MarkdownExecPlugin, MarkdownExecPluginConfig
16+
from markdown_exec._internal.processors import (
17+
HeadingReportingTreeprocessor,
18+
IdPrependingTreeprocessor,
19+
InsertHeadings,
20+
RemoveHeadings,
21+
)
22+
from markdown_exec._internal.rendering import (
23+
MarkdownConfig,
24+
MarkdownConverter,
25+
add_source,
26+
code_block,
27+
markdown_config,
28+
tabbed,
29+
)
30+
31+
__all__ = [
32+
"MARKDOWN_EXEC_AUTO",
33+
"ExecutionError",
34+
"HeadingReportingTreeprocessor",
35+
"IdPrependingTreeprocessor",
36+
"InsertHeadings",
37+
"MarkdownConfig",
38+
"MarkdownConverter",
39+
"MarkdownExecPlugin",
40+
"MarkdownExecPluginConfig",
41+
"RemoveHeadings",
42+
"add_source",
43+
"base_format",
44+
"code_block",
45+
"console_width",
46+
"default_tabs",
47+
"formatter",
48+
"formatters",
49+
"get_logger",
50+
"markdown_config",
51+
"patch_loggers",
52+
"tabbed",
53+
"validator",
54+
"working_directory",
55+
]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# This subpackage contains all the formatters.

src/markdown_exec/formatters/_exec_python.py renamed to src/markdown_exec/_internal/formatters/_exec_python.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
"""Special module without future annotations for executing Python code."""
1+
# Special module without future annotations for executing Python code.
22

33
from typing import Any, Optional
44

src/markdown_exec/formatters/base.py renamed to src/markdown_exec/_internal/formatters/base.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
"""Generic formatter for executing code."""
1+
# Generic formatter for executing code.
22

33
from __future__ import annotations
44

@@ -10,8 +10,8 @@
1010

1111
from markupsafe import Markup
1212

13-
from markdown_exec.logger import get_logger
14-
from markdown_exec.rendering import MarkdownConverter, add_source, code_block
13+
from markdown_exec._internal.logger import get_logger
14+
from markdown_exec._internal.rendering import MarkdownConverter, add_source, code_block
1515

1616
if TYPE_CHECKING:
1717
from collections.abc import Iterator
@@ -71,7 +71,7 @@ class ExecutionError(Exception):
7171
returncode: The code returned by the execution of the code block.
7272
"""
7373

74-
def __init__(self, message: str, returncode: int | None = None) -> None: # noqa: D107
74+
def __init__(self, message: str, returncode: int | None = None) -> None:
7575
super().__init__(message)
7676
self.returncode = returncode
7777

src/markdown_exec/formatters/bash.py renamed to src/markdown_exec/_internal/formatters/bash.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
"""Formatter for executing shell code."""
1+
# Formatter for executing shell code.
22

33
from __future__ import annotations
44

55
import subprocess
66
from typing import Any
77

8-
from markdown_exec.formatters.base import ExecutionError, base_format
9-
from markdown_exec.rendering import code_block
8+
from markdown_exec._internal.formatters.base import ExecutionError, base_format
9+
from markdown_exec._internal.rendering import code_block
1010

1111

1212
def _run_bash(

src/markdown_exec/formatters/console.py renamed to src/markdown_exec/_internal/formatters/console.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
"""Formatter for executing shell console code."""
1+
# Formatter for executing shell console code.
22

33
from __future__ import annotations
44

55
import textwrap
66
from typing import TYPE_CHECKING, Any
77

8-
from markdown_exec.formatters.base import base_format
9-
from markdown_exec.formatters.sh import _run_sh
10-
from markdown_exec.logger import get_logger
8+
from markdown_exec._internal.formatters.base import base_format
9+
from markdown_exec._internal.formatters.sh import _run_sh
10+
from markdown_exec._internal.logger import get_logger
1111

1212
if TYPE_CHECKING:
1313
from markupsafe import Markup

src/markdown_exec/formatters/markdown.py renamed to src/markdown_exec/_internal/formatters/markdown.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
"""Formatter for literate Markdown."""
1+
# Formatter for literate Markdown.
22

33
from __future__ import annotations
44

55
from typing import Any
66

7-
from markdown_exec.formatters.base import base_format
7+
from markdown_exec._internal.formatters.base import base_format
88

99

1010
def _format_markdown(**kwargs: Any) -> str:

src/markdown_exec/formatters/pycon.py renamed to src/markdown_exec/_internal/formatters/pycon.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
"""Formatter for executing `pycon` code."""
1+
# Formatter for executing `pycon` code.
22

33
from __future__ import annotations
44

55
from typing import TYPE_CHECKING, Any
66

7-
from markdown_exec.formatters.base import base_format
8-
from markdown_exec.formatters.python import _run_python
9-
from markdown_exec.logger import get_logger
7+
from markdown_exec._internal.formatters.base import base_format
8+
from markdown_exec._internal.formatters.python import _run_python
9+
from markdown_exec._internal.logger import get_logger
1010

1111
if TYPE_CHECKING:
1212
from markupsafe import Markup

src/markdown_exec/formatters/pyodide.py renamed to src/markdown_exec/_internal/formatters/pyodide.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
"""Formatter for creating a Pyodide interactive editor."""
1+
# Formatter for creating a Pyodide interactive editor.
22

33
from __future__ import annotations
44

src/markdown_exec/formatters/python.py renamed to src/markdown_exec/_internal/formatters/python.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
"""Formatter for executing Python code."""
1+
# Formatter for executing Python code.
22

33
from __future__ import annotations
44

@@ -11,9 +11,9 @@
1111
from types import ModuleType
1212
from typing import Any
1313

14-
from markdown_exec.formatters._exec_python import exec_python
15-
from markdown_exec.formatters.base import ExecutionError, base_format
16-
from markdown_exec.rendering import code_block
14+
from markdown_exec._internal.formatters._exec_python import exec_python
15+
from markdown_exec._internal.formatters.base import ExecutionError, base_format
16+
from markdown_exec._internal.rendering import code_block
1717

1818
_sessions_globals: dict[str, dict] = defaultdict(dict)
1919
_sessions_counter: dict[str | None, int] = defaultdict(int)

src/markdown_exec/formatters/sh.py renamed to src/markdown_exec/_internal/formatters/sh.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
"""Formatter for executing shell code."""
1+
# Formatter for executing shell code.
22

33
from __future__ import annotations
44

55
import subprocess
66
from typing import Any
77

8-
from markdown_exec.formatters.base import ExecutionError, base_format
9-
from markdown_exec.rendering import code_block
8+
from markdown_exec._internal.formatters.base import ExecutionError, base_format
9+
from markdown_exec._internal.rendering import code_block
1010

1111

1212
def _run_sh(

src/markdown_exec/formatters/tree.py renamed to src/markdown_exec/_internal/formatters/tree.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
"""Formatter for file-system trees."""
1+
# Formatter for file-system trees.
22

33
from __future__ import annotations
44

55
from textwrap import dedent
66
from typing import TYPE_CHECKING, Any
77

8-
from markdown_exec.rendering import MarkdownConverter, code_block
8+
from markdown_exec._internal.rendering import MarkdownConverter, code_block
99

1010
if TYPE_CHECKING:
1111
from markdown import Markdown

0 commit comments

Comments
 (0)