mkconvert 0.11.5__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- mkconvert/__init__.py +50 -0
- mkconvert/extensions.py +126 -0
- mkconvert/html_to_md/__init__.py +0 -0
- mkconvert/html_to_md/base.py +96 -0
- mkconvert/html_to_md/factory.py +317 -0
- mkconvert/html_to_md/htmd_converter.py +193 -0
- mkconvert/html_to_md/markdownify_converter.py +258 -0
- mkconvert/parsers/__init__.py +0 -0
- mkconvert/parsers/base_parser.py +31 -0
- mkconvert/parsers/comrak_parser/__init__.py +5 -0
- mkconvert/parsers/comrak_parser/parser.py +176 -0
- mkconvert/parsers/github_api_parser/__init__.py +5 -0
- mkconvert/parsers/github_api_parser/parser.py +62 -0
- mkconvert/parsers/markdown2_parser/__init__.py +5 -0
- mkconvert/parsers/markdown2_parser/parser.py +116 -0
- mkconvert/parsers/markdown_it_pyrs_parser/__init__.py +5 -0
- mkconvert/parsers/markdown_it_pyrs_parser/parser.py +194 -0
- mkconvert/parsers/marko_parser/__init__.py +5 -0
- mkconvert/parsers/marko_parser/parser.py +134 -0
- mkconvert/parsers/mistletoe_parser/__init__.py +1 -0
- mkconvert/parsers/mistune_parser/__init__.py +5 -0
- mkconvert/parsers/mistune_parser/parser.py +120 -0
- mkconvert/parsers/parser.py +150 -0
- mkconvert/parsers/pyromark_parser/__init__.py +5 -0
- mkconvert/parsers/pyromark_parser/parser.py +181 -0
- mkconvert/parsers/python_markdown_parser/__init__.py +5 -0
- mkconvert/parsers/python_markdown_parser/parser.py +92 -0
- mkconvert/post_processors/__init__.py +9 -0
- mkconvert/post_processors/base.py +39 -0
- mkconvert/post_processors/prettify.py +67 -0
- mkconvert/post_processors/registry.py +44 -0
- mkconvert/post_processors/sanitizer.py +65 -0
- mkconvert/pre_processors/__init__.py +7 -0
- mkconvert/pre_processors/admonition_converter.py +104 -0
- mkconvert/pre_processors/base.py +39 -0
- mkconvert/pre_processors/registry.py +44 -0
- mkconvert/py.typed +0 -0
- mkconvert/sanitizers/__init__.py +6 -0
- mkconvert/sanitizers/base.py +41 -0
- mkconvert/sanitizers/bleach_sanitizer.py +64 -0
- mkconvert/sanitizers/factory.py +96 -0
- mkconvert/sanitizers/nh3_sanitizer.py +63 -0
- mkconvert/tree_processors/__init__.py +28 -0
- mkconvert/tree_processors/base.py +216 -0
- mkconvert/tree_processors/extract_title.py +59 -0
- mkconvert/tree_processors/extract_title_lxml.py +64 -0
- mkconvert/tree_processors/registry.py +66 -0
- mkconvert-0.11.5.dist-info/METADATA +131 -0
- mkconvert-0.11.5.dist-info/RECORD +51 -0
- mkconvert-0.11.5.dist-info/WHEEL +4 -0
- mkconvert-0.11.5.dist-info/licenses/LICENSE +22 -0
mkconvert/__init__.py
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
"""A Python wrapper for Rust-based markdown parsers with processor support."""
|
|
2
|
+
|
|
3
|
+
__version__ = "0.11.5"
|
|
4
|
+
|
|
5
|
+
from mkconvert.parsers.base_parser import BaseParser
|
|
6
|
+
from mkconvert.parsers.parser import MarkdownParser
|
|
7
|
+
from mkconvert.parsers.comrak_parser import ComrakParser
|
|
8
|
+
from mkconvert.parsers.github_api_parser import GithubApiParser
|
|
9
|
+
from mkconvert.parsers.markdown2_parser import Markdown2Parser
|
|
10
|
+
from mkconvert.parsers.markdown_it_pyrs_parser import MarkdownItPyRSParser
|
|
11
|
+
from mkconvert.parsers.marko_parser import MarkoParser
|
|
12
|
+
from mkconvert.parsers.mistune_parser import MistuneParser
|
|
13
|
+
from mkconvert.parsers.pyromark_parser import PyroMarkParser
|
|
14
|
+
from mkconvert.parsers.python_markdown_parser import PythonMarkdownParser
|
|
15
|
+
|
|
16
|
+
from mkconvert.pre_processors.base import PreProcessor
|
|
17
|
+
from mkconvert.pre_processors.admonition_converter import MkDocsToGFMAdmonitionProcessor
|
|
18
|
+
|
|
19
|
+
from mkconvert.tree_processors.base import (
|
|
20
|
+
ETTreeProcessor,
|
|
21
|
+
LXMLTreeProcessor,
|
|
22
|
+
TreeProcessor,
|
|
23
|
+
)
|
|
24
|
+
from mkconvert.tree_processors.extract_title import ExtractTitleETProcessor
|
|
25
|
+
from mkconvert.tree_processors.extract_title_lxml import ExtractTitleLXMLProcessor
|
|
26
|
+
|
|
27
|
+
from mkconvert.post_processors.base import PostProcessor
|
|
28
|
+
from mkconvert.post_processors.sanitizer import SanitizeHTMLProcessor
|
|
29
|
+
|
|
30
|
+
__all__ = [
|
|
31
|
+
"BaseParser",
|
|
32
|
+
"ComrakParser",
|
|
33
|
+
"ETTreeProcessor",
|
|
34
|
+
"ExtractTitleETProcessor",
|
|
35
|
+
"ExtractTitleLXMLProcessor",
|
|
36
|
+
"GithubApiParser",
|
|
37
|
+
"LXMLTreeProcessor",
|
|
38
|
+
"Markdown2Parser",
|
|
39
|
+
"MarkdownItPyRSParser",
|
|
40
|
+
"MarkdownParser",
|
|
41
|
+
"MarkoParser",
|
|
42
|
+
"MistuneParser",
|
|
43
|
+
"MkDocsToGFMAdmonitionProcessor",
|
|
44
|
+
"PostProcessor",
|
|
45
|
+
"PreProcessor",
|
|
46
|
+
"PyroMarkParser",
|
|
47
|
+
"PythonMarkdownParser",
|
|
48
|
+
"SanitizeHTMLProcessor",
|
|
49
|
+
"TreeProcessor",
|
|
50
|
+
]
|
mkconvert/extensions.py
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from dataclasses import dataclass
|
|
4
|
+
from typing import TYPE_CHECKING, Literal
|
|
5
|
+
|
|
6
|
+
import epregistry
|
|
7
|
+
import markdown
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
# from mkconvert.tree_processors.python_markdown import PythonMarkdownExtension
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
if TYPE_CHECKING:
|
|
14
|
+
from collections.abc import Callable
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
# https://python-markdown.github.io/extensions/api/
|
|
18
|
+
# FrontMatter / Meta
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
@dataclass
|
|
22
|
+
class TocConfig:
|
|
23
|
+
"""Default configuration options."""
|
|
24
|
+
|
|
25
|
+
marker: str = "[TOC]"
|
|
26
|
+
"""Text to find and replace with Table of Contents. Set to empty string to disable."""
|
|
27
|
+
|
|
28
|
+
title: str = ""
|
|
29
|
+
"""Title to insert into TOC `<div>`."""
|
|
30
|
+
|
|
31
|
+
title_class: str = "toctitle"
|
|
32
|
+
"""CSS class used for the title."""
|
|
33
|
+
|
|
34
|
+
toc_class: str = "toc"
|
|
35
|
+
"""CSS class(es) used for the TOC."""
|
|
36
|
+
|
|
37
|
+
anchorlink: bool = False
|
|
38
|
+
"""True if header should be a self link."""
|
|
39
|
+
|
|
40
|
+
anchorlink_class: str = "toclink"
|
|
41
|
+
"""CSS class(es) used for the anchor link."""
|
|
42
|
+
|
|
43
|
+
permalink: bool | str = False
|
|
44
|
+
"""True or link text if a Sphinx-style permalink should be added."""
|
|
45
|
+
|
|
46
|
+
permalink_class: str = "headerlink"
|
|
47
|
+
"""CSS class(es) used for the permalink."""
|
|
48
|
+
|
|
49
|
+
permalink_title: str = "Permanent link"
|
|
50
|
+
"""Title attribute of the permalink."""
|
|
51
|
+
|
|
52
|
+
permalink_leading: bool = False
|
|
53
|
+
"""True if permalinks should be placed at start of the header, rather than end."""
|
|
54
|
+
|
|
55
|
+
baselevel: str = "1"
|
|
56
|
+
"""Base level for headers."""
|
|
57
|
+
|
|
58
|
+
slugify: Callable | None = None # Will be set in __init__
|
|
59
|
+
"""Function to generate anchors based on header text."""
|
|
60
|
+
|
|
61
|
+
separator: str = "-"
|
|
62
|
+
"""Word separator."""
|
|
63
|
+
|
|
64
|
+
toc_depth: int | str = 6
|
|
65
|
+
"""Define the range of section levels to include in the Table of Contents.
|
|
66
|
+
A single integer (b) defines the bottom section level (<h1>..<hb>) only.
|
|
67
|
+
A string consisting of two digits separated by a hyphen ('2-5') defines
|
|
68
|
+
the top (t) and the bottom (b) (<ht>..<hb>)."""
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
@dataclass
|
|
72
|
+
class CodeHiliteConfig:
|
|
73
|
+
"""Configuration for code highlighting functionality."""
|
|
74
|
+
|
|
75
|
+
linenums: bool | Literal["table", "inline"] | None = None
|
|
76
|
+
"""Use lines numbers. True|table|inline=yes, False=no, None=auto."""
|
|
77
|
+
|
|
78
|
+
guess_lang: bool = True
|
|
79
|
+
"""Automatic language detection."""
|
|
80
|
+
|
|
81
|
+
css_class: str = "codehilite"
|
|
82
|
+
"""Set class name for wrapper <div>."""
|
|
83
|
+
|
|
84
|
+
pygments_style: str = "default"
|
|
85
|
+
"""Pygments HTML Formatter Style (Colorscheme)."""
|
|
86
|
+
|
|
87
|
+
noclasses: bool = False
|
|
88
|
+
"""Use inline styles instead of CSS classes."""
|
|
89
|
+
|
|
90
|
+
use_pygments: bool = True
|
|
91
|
+
"""Highlight code blocks with pygments. Disable if using a JavaScript library."""
|
|
92
|
+
|
|
93
|
+
lang_prefix: str = "language-"
|
|
94
|
+
"""Prefix prepended to the language when `use_pygments` is false."""
|
|
95
|
+
|
|
96
|
+
pygments_formatter: str = "html"
|
|
97
|
+
"""Use a specific formatter for Pygments highlighting."""
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
@dataclass
|
|
101
|
+
class FencedCodeConfig:
|
|
102
|
+
"""Default configuration options."""
|
|
103
|
+
|
|
104
|
+
lang_prefix: str = "language-"
|
|
105
|
+
"""Prefix prepended to the language."""
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
# attr_list = PythonMarkdownExtension(
|
|
109
|
+
# AttrListTreeprocessor, "fenced_code_block", 25, FencedCodeConfig
|
|
110
|
+
# )
|
|
111
|
+
# hilite = PythonMarkdownExtension(CodeHilite, "hilite", 8, CodeHiliteConfig)
|
|
112
|
+
# attr_list = PythonMarkdownExtension(AttrListTreeprocessor, "attr_list", 8)
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
def get_installed_extensions():
|
|
116
|
+
"""Return all entry_points in the `markdown.extensions` group."""
|
|
117
|
+
registry = epregistry.EntryPointRegistry[markdown.extensions.Extension](
|
|
118
|
+
group="markdown.extensions"
|
|
119
|
+
)
|
|
120
|
+
return registry.load_all()
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
if __name__ == "__main__":
|
|
124
|
+
extensions = get_installed_extensions()
|
|
125
|
+
# for extension in extensions.values():
|
|
126
|
+
# print(extension())
|
|
File without changes
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
"""Base classes for HTML to Markdown converters."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from abc import ABC, abstractmethod
|
|
6
|
+
from typing import Any, ClassVar, Literal
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
HeadingStyle = Literal["atx", "setext", "atx_closed"]
|
|
10
|
+
LinkStyle = Literal["inline", "reference"]
|
|
11
|
+
CodeBlockStyle = Literal["indented", "fenced"]
|
|
12
|
+
CodeFenceStyle = Literal["backticks", "tildes"]
|
|
13
|
+
ListMarkerStyle = Literal["asterisk", "dash", "plus"]
|
|
14
|
+
LineBreakStyle = Literal["spaces", "backslash"]
|
|
15
|
+
HorizontalRuleStyle = Literal["dashes", "asterisks", "underscores"]
|
|
16
|
+
EmphasisStyle = Literal["asterisk", "underscore"]
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class BaseHtmlToMarkdown(ABC):
|
|
20
|
+
"""Base class for HTML to Markdown converters with common options."""
|
|
21
|
+
|
|
22
|
+
# Default values
|
|
23
|
+
DEFAULT_HEADING_STYLE: ClassVar[HeadingStyle] = "atx"
|
|
24
|
+
DEFAULT_LINK_STYLE: ClassVar[LinkStyle] = "inline"
|
|
25
|
+
DEFAULT_CODE_BLOCK_STYLE: ClassVar[CodeBlockStyle] = "fenced"
|
|
26
|
+
DEFAULT_CODE_FENCE_STYLE: ClassVar[CodeFenceStyle] = "backticks"
|
|
27
|
+
DEFAULT_LIST_MARKER_STYLE: ClassVar[ListMarkerStyle] = "asterisk"
|
|
28
|
+
DEFAULT_LINE_BREAK_STYLE: ClassVar[LineBreakStyle] = "spaces"
|
|
29
|
+
DEFAULT_HR_STYLE: ClassVar[HorizontalRuleStyle] = "asterisks"
|
|
30
|
+
DEFAULT_EMPHASIS_STYLE: ClassVar[EmphasisStyle] = "asterisk"
|
|
31
|
+
|
|
32
|
+
def __init__(
|
|
33
|
+
self,
|
|
34
|
+
# Common options shared across implementations
|
|
35
|
+
heading_style: HeadingStyle | None = None,
|
|
36
|
+
link_style: LinkStyle | None = None,
|
|
37
|
+
code_block_style: CodeBlockStyle | None = None,
|
|
38
|
+
code_fence_style: CodeFenceStyle | None = None,
|
|
39
|
+
list_marker_style: ListMarkerStyle | None = None,
|
|
40
|
+
line_break_style: LineBreakStyle | None = None,
|
|
41
|
+
hr_style: HorizontalRuleStyle | None = None,
|
|
42
|
+
emphasis_style: EmphasisStyle | None = None,
|
|
43
|
+
# HTML tags handling
|
|
44
|
+
skip_tags: list[str] | None = None,
|
|
45
|
+
# Implementation-specific options
|
|
46
|
+
**options: Any,
|
|
47
|
+
) -> None:
|
|
48
|
+
"""Initialize HTML to Markdown converter with common options.
|
|
49
|
+
|
|
50
|
+
Args:
|
|
51
|
+
heading_style: Style for headings
|
|
52
|
+
link_style: Style for links
|
|
53
|
+
code_block_style: Style for code blocks
|
|
54
|
+
code_fence_style: Style for code fences
|
|
55
|
+
list_marker_style: Style for bullet list markers
|
|
56
|
+
line_break_style: Style for line breaks
|
|
57
|
+
hr_style: Style for horizontal rules
|
|
58
|
+
emphasis_style: Style for emphasis
|
|
59
|
+
skip_tags: HTML tags to skip during conversion
|
|
60
|
+
**options: Additional implementation-specific options
|
|
61
|
+
"""
|
|
62
|
+
self._heading_style = heading_style or self.DEFAULT_HEADING_STYLE
|
|
63
|
+
self._link_style = link_style or self.DEFAULT_LINK_STYLE
|
|
64
|
+
self._code_block_style = code_block_style or self.DEFAULT_CODE_BLOCK_STYLE
|
|
65
|
+
self._code_fence_style = code_fence_style or self.DEFAULT_CODE_FENCE_STYLE
|
|
66
|
+
self._list_marker_style = list_marker_style or self.DEFAULT_LIST_MARKER_STYLE
|
|
67
|
+
self._line_break_style = line_break_style or self.DEFAULT_LINE_BREAK_STYLE
|
|
68
|
+
self._hr_style = hr_style or self.DEFAULT_HR_STYLE
|
|
69
|
+
self._emphasis_style = emphasis_style or self.DEFAULT_EMPHASIS_STYLE
|
|
70
|
+
self._skip_tags = skip_tags
|
|
71
|
+
self._additional_options = options
|
|
72
|
+
|
|
73
|
+
# Initialize implementation-specific options
|
|
74
|
+
self._initialize_options()
|
|
75
|
+
|
|
76
|
+
@abstractmethod
|
|
77
|
+
def _initialize_options(self) -> None:
|
|
78
|
+
"""Initialize implementation-specific options.
|
|
79
|
+
|
|
80
|
+
This method should be overridden by subclasses to set up
|
|
81
|
+
their implementation-specific options based on the common options
|
|
82
|
+
and any additional options provided.
|
|
83
|
+
"""
|
|
84
|
+
...
|
|
85
|
+
|
|
86
|
+
@abstractmethod
|
|
87
|
+
def convert(self, html: str) -> str:
|
|
88
|
+
"""Convert HTML to Markdown.
|
|
89
|
+
|
|
90
|
+
Args:
|
|
91
|
+
html: HTML content to convert
|
|
92
|
+
|
|
93
|
+
Returns:
|
|
94
|
+
Markdown representation of the HTML
|
|
95
|
+
"""
|
|
96
|
+
...
|
|
@@ -0,0 +1,317 @@
|
|
|
1
|
+
"""Factory for creating HTML to Markdown converters."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import importlib.util
|
|
6
|
+
from typing import TYPE_CHECKING, Any, Literal
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
if TYPE_CHECKING:
|
|
10
|
+
from collections.abc import Callable
|
|
11
|
+
|
|
12
|
+
from mkconvert.html_to_md.base import (
|
|
13
|
+
BaseHtmlToMarkdown,
|
|
14
|
+
CodeBlockStyle,
|
|
15
|
+
CodeFenceStyle,
|
|
16
|
+
EmphasisStyle,
|
|
17
|
+
HeadingStyle,
|
|
18
|
+
HorizontalRuleStyle,
|
|
19
|
+
LineBreakStyle,
|
|
20
|
+
LinkStyle,
|
|
21
|
+
ListMarkerStyle,
|
|
22
|
+
)
|
|
23
|
+
from mkconvert.html_to_md.htmd_converter import HtmdConverter
|
|
24
|
+
from mkconvert.html_to_md.markdownify_converter import MarkdownifyConverter
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
# Check for library availability
|
|
28
|
+
HTMD_AVAILABLE = importlib.util.find_spec("htmd") is not None
|
|
29
|
+
MARKDOWNIFY_AVAILABLE = importlib.util.find_spec("markdownify") is not None
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
class HtmlToMarkdownFactory:
|
|
33
|
+
"""Factory for creating HTML to Markdown converters with consistent options."""
|
|
34
|
+
|
|
35
|
+
@staticmethod
|
|
36
|
+
def create(
|
|
37
|
+
backend: Literal["auto", "htmd", "markdownify"] = "auto",
|
|
38
|
+
# Common shared options
|
|
39
|
+
heading_style: HeadingStyle | None = None,
|
|
40
|
+
link_style: LinkStyle | None = None,
|
|
41
|
+
code_block_style: CodeBlockStyle | None = None,
|
|
42
|
+
code_fence_style: CodeFenceStyle | None = None,
|
|
43
|
+
list_marker_style: ListMarkerStyle | None = None,
|
|
44
|
+
line_break_style: LineBreakStyle | None = None,
|
|
45
|
+
hr_style: HorizontalRuleStyle | None = None,
|
|
46
|
+
emphasis_style: EmphasisStyle | None = None,
|
|
47
|
+
skip_tags: list[str] | None = None,
|
|
48
|
+
# Backend-specific options can be passed as additional kwargs
|
|
49
|
+
**options: Any,
|
|
50
|
+
) -> BaseHtmlToMarkdown:
|
|
51
|
+
"""Create an HTML to Markdown converter with consistent options.
|
|
52
|
+
|
|
53
|
+
Args:
|
|
54
|
+
backend: Which backend to use ('auto', 'htmd', or 'markdownify')
|
|
55
|
+
heading_style: Style for headings
|
|
56
|
+
link_style: Style for links
|
|
57
|
+
code_block_style: Style for code blocks
|
|
58
|
+
code_fence_style: Style for code fences
|
|
59
|
+
list_marker_style: Style for bullet list markers
|
|
60
|
+
line_break_style: Style for line breaks
|
|
61
|
+
hr_style: Style for horizontal rules
|
|
62
|
+
emphasis_style: Style for emphasis
|
|
63
|
+
skip_tags: HTML tags to skip during conversion
|
|
64
|
+
**options: Backend-specific options
|
|
65
|
+
|
|
66
|
+
Returns:
|
|
67
|
+
An HTML to Markdown converter
|
|
68
|
+
|
|
69
|
+
Raises:
|
|
70
|
+
ImportError: If the requested backend is not available
|
|
71
|
+
ValueError: If no suitable backend is available
|
|
72
|
+
"""
|
|
73
|
+
# Combine standard options and backend-specific options
|
|
74
|
+
combined_options = {
|
|
75
|
+
"heading_style": heading_style,
|
|
76
|
+
"link_style": link_style,
|
|
77
|
+
"code_block_style": code_block_style,
|
|
78
|
+
"code_fence_style": code_fence_style,
|
|
79
|
+
"list_marker_style": list_marker_style,
|
|
80
|
+
"line_break_style": line_break_style,
|
|
81
|
+
"hr_style": hr_style,
|
|
82
|
+
"emphasis_style": emphasis_style,
|
|
83
|
+
"skip_tags": skip_tags,
|
|
84
|
+
**options,
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
# Clean None values
|
|
88
|
+
clean_options = {k: v for k, v in combined_options.items() if v is not None}
|
|
89
|
+
|
|
90
|
+
# Try htmd (Rust-based, faster)
|
|
91
|
+
if backend == "htmd" or (backend == "auto" and HTMD_AVAILABLE):
|
|
92
|
+
from mkconvert.html_to_md.htmd_converter import HtmdConverter
|
|
93
|
+
|
|
94
|
+
return HtmdConverter(**clean_options)
|
|
95
|
+
|
|
96
|
+
# Try markdownify (pure Python)
|
|
97
|
+
if backend == "markdownify" or (backend == "auto" and MARKDOWNIFY_AVAILABLE):
|
|
98
|
+
from mkconvert.html_to_md.markdownify_converter import MarkdownifyConverter
|
|
99
|
+
|
|
100
|
+
return MarkdownifyConverter(**clean_options)
|
|
101
|
+
|
|
102
|
+
# No suitable backend found
|
|
103
|
+
if backend == "auto":
|
|
104
|
+
msg = (
|
|
105
|
+
"No HTML to Markdown converter is available. "
|
|
106
|
+
"Install either 'htmd-py' (recommended, Rust-based) or "
|
|
107
|
+
"'markdownify' (pure Python)."
|
|
108
|
+
)
|
|
109
|
+
raise ValueError(msg)
|
|
110
|
+
|
|
111
|
+
msg = (
|
|
112
|
+
f"The requested backend '{backend}' is not available. "
|
|
113
|
+
f"Install the corresponding package: "
|
|
114
|
+
f"{'htmd-py' if backend == 'htmd' else 'markdownify'}."
|
|
115
|
+
)
|
|
116
|
+
raise ImportError(msg)
|
|
117
|
+
|
|
118
|
+
@staticmethod
|
|
119
|
+
def create_htmd(
|
|
120
|
+
# Common shared options
|
|
121
|
+
heading_style: HeadingStyle | None = None,
|
|
122
|
+
link_style: LinkStyle | None = None,
|
|
123
|
+
code_block_style: CodeBlockStyle | None = None,
|
|
124
|
+
code_fence_style: CodeFenceStyle | None = None,
|
|
125
|
+
list_marker_style: ListMarkerStyle | None = None,
|
|
126
|
+
line_break_style: LineBreakStyle | None = None,
|
|
127
|
+
hr_style: HorizontalRuleStyle | None = None,
|
|
128
|
+
emphasis_style: EmphasisStyle | None = None,
|
|
129
|
+
skip_tags: list[str] | None = None,
|
|
130
|
+
# htmd-specific options
|
|
131
|
+
preformatted_code: bool = False,
|
|
132
|
+
**options: Any,
|
|
133
|
+
) -> HtmdConverter:
|
|
134
|
+
"""Create an HTML to Markdown converter using htmd.
|
|
135
|
+
|
|
136
|
+
Args:
|
|
137
|
+
heading_style: Style for headings
|
|
138
|
+
link_style: Style for links
|
|
139
|
+
code_block_style: Style for code blocks
|
|
140
|
+
code_fence_style: Style for code fences
|
|
141
|
+
list_marker_style: Style for bullet list markers
|
|
142
|
+
line_break_style: Style for line breaks
|
|
143
|
+
hr_style: Style for horizontal rules
|
|
144
|
+
emphasis_style: Style for emphasis (not used by htmd)
|
|
145
|
+
skip_tags: HTML tags to skip during conversion
|
|
146
|
+
preformatted_code: Whether to preserve whitespace in code
|
|
147
|
+
**options: Additional htmd-specific options
|
|
148
|
+
|
|
149
|
+
Returns:
|
|
150
|
+
An HTML to Markdown converter using htmd
|
|
151
|
+
|
|
152
|
+
Raises:
|
|
153
|
+
ImportError: If htmd is not installed
|
|
154
|
+
"""
|
|
155
|
+
from mkconvert.html_to_md.htmd_converter import HtmdConverter
|
|
156
|
+
|
|
157
|
+
return HtmdConverter(
|
|
158
|
+
heading_style=heading_style,
|
|
159
|
+
link_style=link_style,
|
|
160
|
+
code_block_style=code_block_style,
|
|
161
|
+
code_fence_style=code_fence_style,
|
|
162
|
+
list_marker_style=list_marker_style,
|
|
163
|
+
line_break_style=line_break_style,
|
|
164
|
+
hr_style=hr_style,
|
|
165
|
+
emphasis_style=emphasis_style,
|
|
166
|
+
skip_tags=skip_tags,
|
|
167
|
+
preformatted_code=preformatted_code,
|
|
168
|
+
**options,
|
|
169
|
+
)
|
|
170
|
+
|
|
171
|
+
@staticmethod
|
|
172
|
+
def create_markdownify(
|
|
173
|
+
# Common shared options
|
|
174
|
+
heading_style: HeadingStyle | None = None,
|
|
175
|
+
link_style: LinkStyle | None = None,
|
|
176
|
+
code_block_style: CodeBlockStyle | None = None,
|
|
177
|
+
code_fence_style: CodeFenceStyle | None = None,
|
|
178
|
+
list_marker_style: ListMarkerStyle | None = None,
|
|
179
|
+
line_break_style: LineBreakStyle | None = None,
|
|
180
|
+
hr_style: HorizontalRuleStyle | None = None,
|
|
181
|
+
emphasis_style: EmphasisStyle | None = None,
|
|
182
|
+
skip_tags: list[str] | None = None,
|
|
183
|
+
# Markdownify-specific options
|
|
184
|
+
convert: list[str] | None = None,
|
|
185
|
+
autolinks: bool = True,
|
|
186
|
+
default_title: bool = False,
|
|
187
|
+
sub_symbol: str = "",
|
|
188
|
+
sup_symbol: str = "",
|
|
189
|
+
code_language: str = "",
|
|
190
|
+
code_language_callback: Callable | None = None,
|
|
191
|
+
escape_asterisks: bool = True,
|
|
192
|
+
escape_underscores: bool = True,
|
|
193
|
+
escape_misc: bool = False,
|
|
194
|
+
keep_inline_images_in: list[str] | None = None,
|
|
195
|
+
table_infer_header: bool = False,
|
|
196
|
+
wrap: bool = False,
|
|
197
|
+
wrap_width: int | None = 80,
|
|
198
|
+
strip_document: str | None = "STRIP",
|
|
199
|
+
**options: Any,
|
|
200
|
+
) -> MarkdownifyConverter:
|
|
201
|
+
"""Create an HTML to Markdown converter using markdownify.
|
|
202
|
+
|
|
203
|
+
Args:
|
|
204
|
+
heading_style: Style for headings
|
|
205
|
+
link_style: Style for links (not directly used by markdownify)
|
|
206
|
+
code_block_style: Style for code blocks (not directly used by markdownify)
|
|
207
|
+
code_fence_style: Style for code fences (not directly used by markdownify)
|
|
208
|
+
list_marker_style: Style for bullet list markers
|
|
209
|
+
line_break_style: Style for line breaks
|
|
210
|
+
hr_style: Style for horizontal rules (not directly used by markdownify)
|
|
211
|
+
emphasis_style: Style for emphasis
|
|
212
|
+
skip_tags: HTML tags to skip during conversion
|
|
213
|
+
convert: HTML tags to convert (alternative to skip_tags)
|
|
214
|
+
autolinks: Use automatic link style for matching URLs
|
|
215
|
+
default_title: Set title of links to href when no title is given
|
|
216
|
+
sub_symbol: Symbol for subscripts
|
|
217
|
+
sup_symbol: Symbol for superscripts
|
|
218
|
+
code_language: Default language for code blocks
|
|
219
|
+
code_language_callback: Callback to extract code language
|
|
220
|
+
escape_asterisks: Whether to escape asterisks in text
|
|
221
|
+
escape_underscores: Whether to escape underscores in text
|
|
222
|
+
escape_misc: Whether to escape miscellaneous punctuation
|
|
223
|
+
keep_inline_images_in: Tags that can contain inline images
|
|
224
|
+
table_infer_header: Infer table headers from first row
|
|
225
|
+
wrap: Whether to wrap text
|
|
226
|
+
wrap_width: Width to wrap text at
|
|
227
|
+
strip_document: How to strip document (LSTRIP, RSTRIP, STRIP, None)
|
|
228
|
+
**options: Additional markdownify-specific options
|
|
229
|
+
|
|
230
|
+
Returns:
|
|
231
|
+
An HTML to Markdown converter using markdownify
|
|
232
|
+
|
|
233
|
+
Raises:
|
|
234
|
+
ImportError: If markdownify is not installed
|
|
235
|
+
"""
|
|
236
|
+
from mkconvert.html_to_md.markdownify_converter import MarkdownifyConverter
|
|
237
|
+
|
|
238
|
+
return MarkdownifyConverter(
|
|
239
|
+
heading_style=heading_style,
|
|
240
|
+
link_style=link_style,
|
|
241
|
+
code_block_style=code_block_style,
|
|
242
|
+
code_fence_style=code_fence_style,
|
|
243
|
+
list_marker_style=list_marker_style,
|
|
244
|
+
line_break_style=line_break_style,
|
|
245
|
+
hr_style=hr_style,
|
|
246
|
+
emphasis_style=emphasis_style,
|
|
247
|
+
skip_tags=skip_tags,
|
|
248
|
+
convert=convert,
|
|
249
|
+
autolinks=autolinks,
|
|
250
|
+
default_title=default_title,
|
|
251
|
+
sub_symbol=sub_symbol,
|
|
252
|
+
sup_symbol=sup_symbol,
|
|
253
|
+
code_language=code_language,
|
|
254
|
+
code_language_callback=code_language_callback,
|
|
255
|
+
escape_asterisks=escape_asterisks,
|
|
256
|
+
escape_underscores=escape_underscores,
|
|
257
|
+
escape_misc=escape_misc,
|
|
258
|
+
keep_inline_images_in=keep_inline_images_in,
|
|
259
|
+
table_infer_header=table_infer_header,
|
|
260
|
+
wrap=wrap,
|
|
261
|
+
wrap_width=wrap_width,
|
|
262
|
+
strip_document=strip_document,
|
|
263
|
+
**options,
|
|
264
|
+
)
|
|
265
|
+
|
|
266
|
+
|
|
267
|
+
# Convenience function
|
|
268
|
+
def create_html_to_markdown(
|
|
269
|
+
backend: Literal["auto", "htmd", "markdownify"] = "auto",
|
|
270
|
+
# Common shared options
|
|
271
|
+
heading_style: HeadingStyle | None = None,
|
|
272
|
+
link_style: LinkStyle | None = None,
|
|
273
|
+
code_block_style: CodeBlockStyle | None = None,
|
|
274
|
+
code_fence_style: CodeFenceStyle | None = None,
|
|
275
|
+
list_marker_style: ListMarkerStyle | None = None,
|
|
276
|
+
line_break_style: LineBreakStyle | None = None,
|
|
277
|
+
hr_style: HorizontalRuleStyle | None = None,
|
|
278
|
+
emphasis_style: EmphasisStyle | None = None,
|
|
279
|
+
skip_tags: list[str] | None = None,
|
|
280
|
+
# Backend-specific options can be passed as additional kwargs
|
|
281
|
+
**options: Any,
|
|
282
|
+
) -> BaseHtmlToMarkdown:
|
|
283
|
+
"""Create an HTML to Markdown converter with consistent options.
|
|
284
|
+
|
|
285
|
+
Args:
|
|
286
|
+
backend: Which backend to use ('auto', 'htmd', or 'markdownify')
|
|
287
|
+
heading_style: Style for headings
|
|
288
|
+
link_style: Style for links
|
|
289
|
+
code_block_style: Style for code blocks
|
|
290
|
+
code_fence_style: Style for code fences
|
|
291
|
+
list_marker_style: Style for bullet list markers
|
|
292
|
+
line_break_style: Style for line breaks
|
|
293
|
+
hr_style: Style for horizontal rules
|
|
294
|
+
emphasis_style: Style for emphasis
|
|
295
|
+
skip_tags: HTML tags to skip during conversion
|
|
296
|
+
**options: Backend-specific options
|
|
297
|
+
|
|
298
|
+
Returns:
|
|
299
|
+
An HTML to Markdown converter
|
|
300
|
+
|
|
301
|
+
Raises:
|
|
302
|
+
ImportError: If the requested backend is not available
|
|
303
|
+
ValueError: If no suitable backend is available
|
|
304
|
+
"""
|
|
305
|
+
return HtmlToMarkdownFactory.create(
|
|
306
|
+
backend,
|
|
307
|
+
heading_style=heading_style,
|
|
308
|
+
link_style=link_style,
|
|
309
|
+
code_block_style=code_block_style,
|
|
310
|
+
code_fence_style=code_fence_style,
|
|
311
|
+
list_marker_style=list_marker_style,
|
|
312
|
+
line_break_style=line_break_style,
|
|
313
|
+
hr_style=hr_style,
|
|
314
|
+
emphasis_style=emphasis_style,
|
|
315
|
+
skip_tags=skip_tags,
|
|
316
|
+
**options,
|
|
317
|
+
)
|