Skip to content

PythonMarkdownParser

Base classes

Name Children Inherits
MarkdownParser
mkconvert.parsers.base
Base class for Markdown -> HTML parsers.

⋔ Inheritance diagram

graph TD
  39780109["python_markdown.PythonMarkdownParser"]
  2536207506["base.MarkdownParser"]
  1836221132["_core.Backend"]
  925645236["abc.ABC"]
  2751680898["builtins.object"]
  3481597255["typing.Generic"]
  2536207506 --> 39780109
  1836221132 --> 2536207506
  925645236 --> 1836221132
  2751680898 --> 925645236
  3481597255 --> 1836221132
  2751680898 --> 3481597255

🛈 DocStrings

Bases: MarkdownParser

Python-Markdown with pymdown-extensions (not CommonMark compliant).

Fenced code blocks are always on. Strikethrough, task lists, autolinks and math come from pymdown-extensions. Not supported: alerts.

Source code in src/mkconvert/parsers/python_markdown.py
class PythonMarkdownParser(MarkdownParser):
    """Python-Markdown with pymdown-extensions (not CommonMark compliant).

    Fenced code blocks are always on. Strikethrough, task lists, autolinks and math
    come from pymdown-extensions. Not supported: `alerts`.
    """

    name = "python-markdown"
    requires = ("markdown", "pymdownx")
    extra = "markdown"
    supported_options: ClassVar[frozenset[str]] = frozenset({
        "tables",
        "strikethrough",
        "autolinks",
        "task_lists",
        "footnotes",
        "heading_ids",
        "math",
        "definition_lists",
        "hard_breaks",
        "smart_punctuation",
        "raw_html",
        "front_matter",
    })

    def __init__(
        self,
        options: MarkdownOptions | None = None,
        *,
        on_unsupported: OnUnsupported = "warn",
        extensions: Sequence[str | Extension] = (),
        extension_configs: Mapping[str, Mapping[str, Any]] | None = None,
        output_format: Literal["html", "xhtml"] = "html",
    ) -> None:
        """Create a Python-Markdown parser.

        Args:
            options: Common options.
            on_unsupported: See `Backend`.
            extensions: Further extensions, e.g. `"admonition"` or `"pymdownx.superfences"`.
            extension_configs: Settings per extension name; merged over the ones mkconvert
                uses for the common options.
            output_format: `"html"` or `"xhtml"`.
        """
        super().__init__(options, on_unsupported=on_unsupported)
        import markdown

        o = self.options
        selected = {
            "tables": o.tables,
            "pymdownx.tilde": o.strikethrough,
            "pymdownx.magiclink": o.autolinks,
            "pymdownx.tasklist": o.task_lists,
            "footnotes": o.footnotes,
            "toc": o.heading_ids,
            "pymdownx.arithmatex": o.math,
            "def_list": o.definition_lists,
            "nl2br": o.hard_breaks,
            "smarty": o.smart_punctuation,
        }
        exts: list[str | Extension] = ["fenced_code"]
        exts += [name for name, enabled in selected.items() if enabled]
        exts += list(extensions)
        configs: dict[str, Any] = {
            "pymdownx.tilde": {"subscript": False},
            "pymdownx.arithmatex": {"generic": True},
        }
        for name, cfg in (extension_configs or {}).items():
            configs[name] = {**configs.get(name, {}), **cfg}
        self._md = markdown.Markdown(
            extensions=exts, extension_configs=configs, output_format=output_format
        )
        if not o.raw_html:
            self._md.preprocessors.deregister("html_block")
            self._md.inlinePatterns.deregister("html")
        self._lock = threading.Lock()

    def _render(self, text: str) -> str:
        with self._lock:
            return self._md.reset().convert(text)

__init__

__init__(
    options: MarkdownOptions | None = None,
    *,
    on_unsupported: OnUnsupported = "warn",
    extensions: Sequence[str | Extension] = (),
    extension_configs: Mapping[str, Mapping[str, Any]] | None = None,
    output_format: Literal["html", "xhtml"] = "html",
) -> None

Create a Python-Markdown parser.

Parameters:

Name Type Description Default
options MarkdownOptions | None

Common options.

None
on_unsupported OnUnsupported

See Backend.

'warn'
extensions Sequence[str | Extension]

Further extensions, e.g. "admonition" or "pymdownx.superfences".

()
extension_configs Mapping[str, Mapping[str, Any]] | None

Settings per extension name; merged over the ones mkconvert uses for the common options.

None
output_format Literal['html', 'xhtml']

"html" or "xhtml".

'html'
Source code in src/mkconvert/parsers/python_markdown.py
def __init__(
    self,
    options: MarkdownOptions | None = None,
    *,
    on_unsupported: OnUnsupported = "warn",
    extensions: Sequence[str | Extension] = (),
    extension_configs: Mapping[str, Mapping[str, Any]] | None = None,
    output_format: Literal["html", "xhtml"] = "html",
) -> None:
    """Create a Python-Markdown parser.

    Args:
        options: Common options.
        on_unsupported: See `Backend`.
        extensions: Further extensions, e.g. `"admonition"` or `"pymdownx.superfences"`.
        extension_configs: Settings per extension name; merged over the ones mkconvert
            uses for the common options.
        output_format: `"html"` or `"xhtml"`.
    """
    super().__init__(options, on_unsupported=on_unsupported)
    import markdown

    o = self.options
    selected = {
        "tables": o.tables,
        "pymdownx.tilde": o.strikethrough,
        "pymdownx.magiclink": o.autolinks,
        "pymdownx.tasklist": o.task_lists,
        "footnotes": o.footnotes,
        "toc": o.heading_ids,
        "pymdownx.arithmatex": o.math,
        "def_list": o.definition_lists,
        "nl2br": o.hard_breaks,
        "smarty": o.smart_punctuation,
    }
    exts: list[str | Extension] = ["fenced_code"]
    exts += [name for name, enabled in selected.items() if enabled]
    exts += list(extensions)
    configs: dict[str, Any] = {
        "pymdownx.tilde": {"subscript": False},
        "pymdownx.arithmatex": {"generic": True},
    }
    for name, cfg in (extension_configs or {}).items():
        configs[name] = {**configs.get(name, {}), **cfg}
    self._md = markdown.Markdown(
        extensions=exts, extension_configs=configs, output_format=output_format
    )
    if not o.raw_html:
        self._md.preprocessors.deregister("html_block")
        self._md.inlinePatterns.deregister("html")
    self._lock = threading.Lock()

:fa-brands-github: Show source on GitHub