Skip to content

HtmdConverter

Base classes

Name Children Inherits
HtmlToMarkdownConverter
mkconvert.converters.base
Base class for HTML -> Markdown converters.

⋔ Inheritance diagram

graph TD
  2349700106["htmd.HtmdConverter"]
  1426344930["base.HtmlToMarkdownConverter"]
  1836221132["_core.Backend"]
  925645236["abc.ABC"]
  2751680898["builtins.object"]
  3481597255["typing.Generic"]
  1426344930 --> 2349700106
  1836221132 --> 1426344930
  925645236 --> 1836221132
  2751680898 --> 925645236
  3481597255 --> 1836221132
  2751680898 --> 3481597255

🛈 DocStrings

Bases: HtmlToMarkdownConverter

htmd: fast Rust converter.

Not supported: emphasis_marker="_".

Source code in src/mkconvert/converters/htmd.py
class HtmdConverter(HtmlToMarkdownConverter):
    """htmd: fast Rust converter.

    Not supported: `emphasis_marker="_"`.
    """

    name = "htmd"
    requires = ("htmd",)
    extra = "htmd-py"
    supported_options: ClassVar[frozenset[str]] = frozenset({
        "heading_style",
        "bullet_marker",
        "code_block_style",
        "code_fence",
        "line_break",
        "link_style",
        "skip_tags",
    })

    def __init__(
        self,
        options: HtmlToMarkdownOptions | None = None,
        *,
        on_unsupported: OnUnsupported = "warn",
        hr_style: Literal["dashes", "asterisks", "underscores"] = "dashes",
        link_reference_style: Literal["full", "collapsed", "shortcut"] = "full",
        preformatted_code: bool = False,
        ul_bullet_spacing: int = 1,
        ol_number_spacing: int = 1,
        translation_mode: Literal["pure", "faithful"] = "pure",
    ) -> None:
        """Create an htmd converter.

        Args:
            options: Common options.
            on_unsupported: See `Backend`.
            hr_style: `- - -`, `* * *` or `_ _ _` for `<hr>`.
            link_reference_style: Reference link form (with `link_style="reference"`).
            preformatted_code: Keep whitespace of inline `<code>` as is.
            ul_bullet_spacing: Spaces after a bullet list marker.
            ol_number_spacing: Spaces after an ordered list number.
            translation_mode: `"faithful"` keeps HTML that has no Markdown equivalent.
        """
        super().__init__(options, on_unsupported=on_unsupported)
        import htmd

        o = self.options
        opts = htmd.Options()  # ty: ignore[missing-attribute]
        opts.heading_style = "setex" if o.heading_style == "setext" else "atx"
        opts.bullet_list_marker = "dash" if o.bullet_marker == "-" else "asterisk"
        opts.code_block_style = o.code_block_style
        opts.code_block_fence = "tildes" if o.code_fence == "~~~" else "backticks"
        opts.br_style = "backslash" if o.line_break == "backslash" else "two_spaces"
        opts.link_style = "referenced" if o.link_style == "reference" else "inlined"
        opts.skip_tags = [tag.lower() for tag in o.skip_tags]
        opts.hr_style = hr_style
        opts.link_reference_style = link_reference_style
        opts.preformatted_code = preformatted_code
        opts.ul_bullet_spacing = ul_bullet_spacing
        opts.ol_number_spacing = ol_number_spacing
        opts.translation_mode = translation_mode
        self._opts = opts

    def convert(self, text: str) -> str:
        """Convert HTML `text` to Markdown."""
        import htmd

        return htmd.convert_html(text, self._opts)  # ty: ignore[missing-attribute]

__init__

__init__(
    options: HtmlToMarkdownOptions | None = None,
    *,
    on_unsupported: OnUnsupported = "warn",
    hr_style: Literal["dashes", "asterisks", "underscores"] = "dashes",
    link_reference_style: Literal["full", "collapsed", "shortcut"] = "full",
    preformatted_code: bool = False,
    ul_bullet_spacing: int = 1,
    ol_number_spacing: int = 1,
    translation_mode: Literal["pure", "faithful"] = "pure",
) -> None

Create an htmd converter.

Parameters:

Name Type Description Default
options HtmlToMarkdownOptions | None

Common options.

None
on_unsupported OnUnsupported

See Backend.

'warn'
hr_style Literal['dashes', 'asterisks', 'underscores']

- - -, * * * or _ _ _ for <hr>.

'dashes'
link_reference_style Literal['full', 'collapsed', 'shortcut']

Reference link form (with link_style="reference").

'full'
preformatted_code bool

Keep whitespace of inline <code> as is.

False
ul_bullet_spacing int

Spaces after a bullet list marker.

1
ol_number_spacing int

Spaces after an ordered list number.

1
translation_mode Literal['pure', 'faithful']

"faithful" keeps HTML that has no Markdown equivalent.

'pure'
Source code in src/mkconvert/converters/htmd.py
def __init__(
    self,
    options: HtmlToMarkdownOptions | None = None,
    *,
    on_unsupported: OnUnsupported = "warn",
    hr_style: Literal["dashes", "asterisks", "underscores"] = "dashes",
    link_reference_style: Literal["full", "collapsed", "shortcut"] = "full",
    preformatted_code: bool = False,
    ul_bullet_spacing: int = 1,
    ol_number_spacing: int = 1,
    translation_mode: Literal["pure", "faithful"] = "pure",
) -> None:
    """Create an htmd converter.

    Args:
        options: Common options.
        on_unsupported: See `Backend`.
        hr_style: `- - -`, `* * *` or `_ _ _` for `<hr>`.
        link_reference_style: Reference link form (with `link_style="reference"`).
        preformatted_code: Keep whitespace of inline `<code>` as is.
        ul_bullet_spacing: Spaces after a bullet list marker.
        ol_number_spacing: Spaces after an ordered list number.
        translation_mode: `"faithful"` keeps HTML that has no Markdown equivalent.
    """
    super().__init__(options, on_unsupported=on_unsupported)
    import htmd

    o = self.options
    opts = htmd.Options()  # ty: ignore[missing-attribute]
    opts.heading_style = "setex" if o.heading_style == "setext" else "atx"
    opts.bullet_list_marker = "dash" if o.bullet_marker == "-" else "asterisk"
    opts.code_block_style = o.code_block_style
    opts.code_block_fence = "tildes" if o.code_fence == "~~~" else "backticks"
    opts.br_style = "backslash" if o.line_break == "backslash" else "two_spaces"
    opts.link_style = "referenced" if o.link_style == "reference" else "inlined"
    opts.skip_tags = [tag.lower() for tag in o.skip_tags]
    opts.hr_style = hr_style
    opts.link_reference_style = link_reference_style
    opts.preformatted_code = preformatted_code
    opts.ul_bullet_spacing = ul_bullet_spacing
    opts.ol_number_spacing = ol_number_spacing
    opts.translation_mode = translation_mode
    self._opts = opts

convert

convert(text: str) -> str

Convert HTML text to Markdown.

Source code in src/mkconvert/converters/htmd.py
def convert(self, text: str) -> str:
    """Convert HTML `text` to Markdown."""
    import htmd

    return htmd.convert_html(text, self._opts)  # ty: ignore[missing-attribute]

:fa-brands-github: Show source on GitHub