Skip to content

mistune

Class info

Classes

Name Children Inherits
MistuneParser
mkconvert.parsers.mistune
Mistune: fast pure-Python parser (not fully CommonMark compliant).

    🛈 DocStrings

    Parser using mistune (pure Python, fast).

    MistuneParser

    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)