Skip to content

CmarkgfmParser

Base classes

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

⋔ Inheritance diagram

graph TD
  3245185810["cmarkgfm.CmarkgfmParser"]
  2536207506["base.MarkdownParser"]
  1836221132["_core.Backend"]
  925645236["abc.ABC"]
  2751680898["builtins.object"]
  3481597255["typing.Generic"]
  2536207506 --> 3245185810
  1836221132 --> 2536207506
  925645236 --> 1836221132
  2751680898 --> 925645236
  3481597255 --> 1836221132
  2751680898 --> 3481597255

🛈 DocStrings

Bases: MarkdownParser

cmarkgfm: bindings to cmark-gfm, the reference GFM implementation.

Not supported: heading_ids, alerts, math, definition_lists. With raw_html=False, raw HTML is replaced by <!-- raw HTML omitted -->.

Source code in src/mkconvert/parsers/cmarkgfm.py
class CmarkgfmParser(MarkdownParser):
    """cmarkgfm: bindings to cmark-gfm, the reference GFM implementation.

    Not supported: `heading_ids`, `alerts`, `math`, `definition_lists`.
    With `raw_html=False`, raw HTML is replaced by `<!-- raw HTML omitted -->`.
    """

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

    def __init__(
        self,
        options: MarkdownOptions | None = None,
        *,
        on_unsupported: OnUnsupported = "warn",
        tagfilter: bool = False,
        github_pre_lang: bool = False,
        sourcepos: bool = False,
    ) -> None:
        """Create a cmarkgfm parser.

        Args:
            options: Common options.
            on_unsupported: See `Backend`.
            tagfilter: GFM tag filter: escape `<script>`, `<iframe>`, ... in raw HTML.
            github_pre_lang: GitHub-style `<pre lang="...">` code blocks.
            sourcepos: Add `data-sourcepos` attributes.
        """
        super().__init__(options, on_unsupported=on_unsupported)
        from cmarkgfm.cmark import Options as Flags

        o = self.options
        selected = {
            "table": o.tables,
            "strikethrough": o.strikethrough,
            "autolink": o.autolinks,
            "tasklist": o.task_lists,
            "tagfilter": tagfilter,
        }
        self._extensions = [ext for ext, enabled in selected.items() if enabled]
        flags = {
            Flags.CMARK_OPT_FOOTNOTES: o.footnotes,
            Flags.CMARK_OPT_HARDBREAKS: o.hard_breaks,
            Flags.CMARK_OPT_SMART: o.smart_punctuation,
            Flags.CMARK_OPT_UNSAFE: o.raw_html,
            Flags.CMARK_OPT_GITHUB_PRE_LANG: github_pre_lang,
            Flags.CMARK_OPT_SOURCEPOS: sourcepos,
        }
        self._flags = sum(flag for flag, enabled in flags.items() if enabled)

    def _render(self, text: str) -> str:
        from cmarkgfm.cmark import markdown_to_html_with_extensions

        with _LOCK:
            return markdown_to_html_with_extensions(
                text, options=self._flags, extensions=self._extensions
            )

__init__

__init__(
    options: MarkdownOptions | None = None,
    *,
    on_unsupported: OnUnsupported = "warn",
    tagfilter: bool = False,
    github_pre_lang: bool = False,
    sourcepos: bool = False,
) -> None

Create a cmarkgfm parser.

Parameters:

Name Type Description Default
options MarkdownOptions | None

Common options.

None
on_unsupported OnUnsupported

See Backend.

'warn'
tagfilter bool

GFM tag filter: escape <script>, <iframe>, ... in raw HTML.

False
github_pre_lang bool

GitHub-style <pre lang="..."> code blocks.

False
sourcepos bool

Add data-sourcepos attributes.

False
Source code in src/mkconvert/parsers/cmarkgfm.py
def __init__(
    self,
    options: MarkdownOptions | None = None,
    *,
    on_unsupported: OnUnsupported = "warn",
    tagfilter: bool = False,
    github_pre_lang: bool = False,
    sourcepos: bool = False,
) -> None:
    """Create a cmarkgfm parser.

    Args:
        options: Common options.
        on_unsupported: See `Backend`.
        tagfilter: GFM tag filter: escape `<script>`, `<iframe>`, ... in raw HTML.
        github_pre_lang: GitHub-style `<pre lang="...">` code blocks.
        sourcepos: Add `data-sourcepos` attributes.
    """
    super().__init__(options, on_unsupported=on_unsupported)
    from cmarkgfm.cmark import Options as Flags

    o = self.options
    selected = {
        "table": o.tables,
        "strikethrough": o.strikethrough,
        "autolink": o.autolinks,
        "tasklist": o.task_lists,
        "tagfilter": tagfilter,
    }
    self._extensions = [ext for ext, enabled in selected.items() if enabled]
    flags = {
        Flags.CMARK_OPT_FOOTNOTES: o.footnotes,
        Flags.CMARK_OPT_HARDBREAKS: o.hard_breaks,
        Flags.CMARK_OPT_SMART: o.smart_punctuation,
        Flags.CMARK_OPT_UNSAFE: o.raw_html,
        Flags.CMARK_OPT_GITHUB_PRE_LANG: github_pre_lang,
        Flags.CMARK_OPT_SOURCEPOS: sourcepos,
    }
    self._flags = sum(flag for flag, enabled in flags.items() if enabled)

:fa-brands-github: Show source on GitHub