Skip to content

MistuneParser

Base classes

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

⋔ Inheritance diagram

graph TD
  2980731078["mistune.MistuneParser"]
  2536207506["base.MarkdownParser"]
  1836221132["_core.Backend"]
  925645236["abc.ABC"]
  2751680898["builtins.object"]
  3481597255["typing.Generic"]
  2536207506 --> 2980731078
  1836221132 --> 2536207506
  925645236 --> 1836221132
  2751680898 --> 925645236
  3481597255 --> 1836221132
  2751680898 --> 3481597255

🛈 DocStrings

Bases: MarkdownParser

Mistune: fast pure-Python parser (not fully CommonMark compliant).

Not supported: alerts, smart_punctuation. autolinks links bare https:// URLs only, not www. ones.

Source code in src/mkconvert/parsers/mistune.py
class MistuneParser(MarkdownParser):
    """Mistune: fast pure-Python parser (not fully CommonMark compliant).

    Not supported: `alerts`, `smart_punctuation`. `autolinks` links bare `https://`
    URLs only, not `www.` ones.
    """

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

    def __init__(
        self,
        options: MarkdownOptions | None = None,
        *,
        on_unsupported: OnUnsupported = "warn",
        plugins: Sequence[str | Callable[..., None]] = (),
    ) -> None:
        """Create a mistune parser.

        Args:
            options: Common options.
            on_unsupported: See `Backend`.
            plugins: Further mistune plugins: names (e.g. `"abbr"`, `"mark"`,
                `"superscript"`) or plugin objects (e.g. a `mistune.directives.FencedDirective`
                for MyST-style admonitions).
        """
        super().__init__(options, on_unsupported=on_unsupported)
        import mistune
        from mistune.toc import add_toc_hook

        o = self.options
        selected = {
            "table": o.tables,
            "strikethrough": o.strikethrough,
            "url": o.autolinks,
            "task_lists": o.task_lists,
            "footnotes": o.footnotes,
            "math": o.math,
            "def_list": o.definition_lists,
        }
        names: list[str | Callable[..., None]] = [n for n, enabled in selected.items() if enabled]
        names += [p for p in plugins if p not in names]
        self._md = mistune.create_markdown(
            escape=not o.raw_html,
            hard_wrap=o.hard_breaks,
            plugins=names,  # type: ignore[arg-type]
        )
        if o.heading_ids:
            add_toc_hook(self._md, max_level=6, heading_id=_slugify)

    def _render(self, text: str) -> str:
        return str(self._md(text))

__init__

__init__(
    options: MarkdownOptions | None = None,
    *,
    on_unsupported: OnUnsupported = "warn",
    plugins: Sequence[str | Callable[..., None]] = (),
) -> None

Create a mistune parser.

Parameters:

Name Type Description Default
options MarkdownOptions | None

Common options.

None
on_unsupported OnUnsupported

See Backend.

'warn'
plugins Sequence[str | Callable[..., None]]

Further mistune plugins: names (e.g. "abbr", "mark", "superscript") or plugin objects (e.g. a mistune.directives.FencedDirective for MyST-style admonitions).

()
Source code in src/mkconvert/parsers/mistune.py
def __init__(
    self,
    options: MarkdownOptions | None = None,
    *,
    on_unsupported: OnUnsupported = "warn",
    plugins: Sequence[str | Callable[..., None]] = (),
) -> None:
    """Create a mistune parser.

    Args:
        options: Common options.
        on_unsupported: See `Backend`.
        plugins: Further mistune plugins: names (e.g. `"abbr"`, `"mark"`,
            `"superscript"`) or plugin objects (e.g. a `mistune.directives.FencedDirective`
            for MyST-style admonitions).
    """
    super().__init__(options, on_unsupported=on_unsupported)
    import mistune
    from mistune.toc import add_toc_hook

    o = self.options
    selected = {
        "table": o.tables,
        "strikethrough": o.strikethrough,
        "url": o.autolinks,
        "task_lists": o.task_lists,
        "footnotes": o.footnotes,
        "math": o.math,
        "def_list": o.definition_lists,
    }
    names: list[str | Callable[..., None]] = [n for n, enabled in selected.items() if enabled]
    names += [p for p in plugins if p not in names]
    self._md = mistune.create_markdown(
        escape=not o.raw_html,
        hard_wrap=o.hard_breaks,
        plugins=names,  # type: ignore[arg-type]
    )
    if o.heading_ids:
        add_toc_hook(self._md, max_level=6, heading_id=_slugify)

:fa-brands-github: Show source on GitHub