html-to-markdown 1.0.0__tar.gz → 1.2.0__tar.gz
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.
Potentially problematic release.
This version of html-to-markdown might be problematic. Click here for more details.
- html_to_markdown-1.2.0/PKG-INFO +102 -0
- html_to_markdown-1.2.0/README.md +75 -0
- html_to_markdown-1.2.0/html_to_markdown/__init__.py +5 -0
- html_to_markdown-1.2.0/html_to_markdown/__main__.py +11 -0
- html_to_markdown-1.2.0/html_to_markdown/cli.py +150 -0
- html_to_markdown-1.2.0/html_to_markdown/constants.py +18 -0
- {html_to_markdown-1.0.0 → html_to_markdown-1.2.0}/html_to_markdown/converters.py +12 -10
- html_to_markdown-1.2.0/html_to_markdown/legacy.py +89 -0
- {html_to_markdown-1.0.0 → html_to_markdown-1.2.0}/html_to_markdown/processing.py +70 -92
- html_to_markdown-1.2.0/pyproject.toml +113 -0
- html_to_markdown-1.0.0/PKG-INFO +0 -194
- html_to_markdown-1.0.0/README.md +0 -168
- html_to_markdown-1.0.0/html_to_markdown/__init__.py +0 -3
- html_to_markdown-1.0.0/html_to_markdown/__main__.py +0 -131
- html_to_markdown-1.0.0/html_to_markdown/constants.py +0 -18
- html_to_markdown-1.0.0/pyproject.toml +0 -137
- {html_to_markdown-1.0.0 → html_to_markdown-1.2.0}/.gitignore +0 -0
- {html_to_markdown-1.0.0 → html_to_markdown-1.2.0}/LICENSE +0 -0
- {html_to_markdown-1.0.0 → html_to_markdown-1.2.0}/html_to_markdown/py.typed +0 -0
- {html_to_markdown-1.0.0 → html_to_markdown-1.2.0}/html_to_markdown/utils.py +0 -0
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: html-to-markdown
|
|
3
|
+
Version: 1.2.0
|
|
4
|
+
Summary: Convert HTML to markdown
|
|
5
|
+
Author-email: Na'aman Hirschfeld <nhirschfeld@gmail.com>
|
|
6
|
+
License: MIT
|
|
7
|
+
License-File: LICENSE
|
|
8
|
+
Keywords: converter,html,markdown,text-extraction,text-processing
|
|
9
|
+
Classifier: Intended Audience :: Developers
|
|
10
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
11
|
+
Classifier: Operating System :: OS Independent
|
|
12
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
18
|
+
Classifier: Topic :: Text Processing
|
|
19
|
+
Classifier: Topic :: Text Processing :: Markup
|
|
20
|
+
Classifier: Topic :: Text Processing :: Markup :: HTML
|
|
21
|
+
Classifier: Topic :: Text Processing :: Markup :: Markdown
|
|
22
|
+
Classifier: Topic :: Utilities
|
|
23
|
+
Classifier: Typing :: Typed
|
|
24
|
+
Requires-Python: >=3.9
|
|
25
|
+
Requires-Dist: beautifulsoup4>=4.12.3
|
|
26
|
+
Description-Content-Type: text/markdown
|
|
27
|
+
|
|
28
|
+
# html_to_markdown
|
|
29
|
+
|
|
30
|
+
This library is a refactored and modernized fork of [markdownify](https://pypi.org/project/markdownify/), supporting
|
|
31
|
+
Python 3.9 and above.
|
|
32
|
+
|
|
33
|
+
### Differences with the Markdownify
|
|
34
|
+
|
|
35
|
+
- The refactored codebase uses a strict functional approach - no classes are involved.
|
|
36
|
+
- There is full typing with strict MyPy strict adherence and a py.typed file included.
|
|
37
|
+
- The `convert_to_markdown` function allows passing a pre-configured instance of `BeautifulSoup` instead of html.
|
|
38
|
+
- This library releases follows standard semver. Its version v1.0.0 was branched from markdownify's v0.13.1, at which
|
|
39
|
+
point versioning is no longer aligned.
|
|
40
|
+
|
|
41
|
+
## Installation
|
|
42
|
+
|
|
43
|
+
```shell
|
|
44
|
+
pip install html_to_markdown
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
## Usage
|
|
48
|
+
|
|
49
|
+
Convert an string HTML to Markdown:
|
|
50
|
+
|
|
51
|
+
```python
|
|
52
|
+
from html_to_markdown import convert_to_markdown
|
|
53
|
+
|
|
54
|
+
convert_to_markdown('<b>Yay</b> <a href="http://github.com">GitHub</a>') # > '**Yay** [GitHub](http://github.com)'
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
Or pass a pre-configured instance of `BeautifulSoup`:
|
|
58
|
+
|
|
59
|
+
```python
|
|
60
|
+
from bs4 import BeautifulSoup
|
|
61
|
+
from html_to_markdown import convert_to_markdown
|
|
62
|
+
|
|
63
|
+
soup = BeautifulSoup('<b>Yay</b> <a href="http://github.com">GitHub</a>', 'lxml') # lxml requires an extra dependency.
|
|
64
|
+
|
|
65
|
+
convert_to_markdown(soup) # > '**Yay** [GitHub](http://github.com)'
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
### Options
|
|
69
|
+
|
|
70
|
+
The `convert_to_markdown` function accepts the following kwargs:
|
|
71
|
+
|
|
72
|
+
- autolinks (bool): Automatically convert valid URLs into Markdown links. Defaults to True.
|
|
73
|
+
- bullets (str): A string of characters to use for bullet points in lists. Defaults to '\*+-'.
|
|
74
|
+
- code_language (str): Default language identifier for fenced code blocks. Defaults to an empty string.
|
|
75
|
+
- code_language_callback (Callable[[Any], str] | None): Function to dynamically determine the language for code blocks.
|
|
76
|
+
- convert (Iterable[str] | None): A list of tag names to convert to Markdown. If None, all supported tags are converted.
|
|
77
|
+
- default_title (bool): Use the default title when converting certain elements (e.g., links). Defaults to False.
|
|
78
|
+
- escape_asterisks (bool): Escape asterisks (\*) to prevent unintended Markdown formatting. Defaults to True.
|
|
79
|
+
- escape_misc (bool): Escape miscellaneous characters to prevent conflicts in Markdown. Defaults to True.
|
|
80
|
+
- escape*underscores (bool): Escape underscores (*) to prevent unintended italic formatting. Defaults to True.
|
|
81
|
+
- heading_style (Literal["underlined", "atx", "atx_closed"]): The style to use for Markdown headings. Defaults to "
|
|
82
|
+
underlined".
|
|
83
|
+
- keep_inline_images_in (Iterable[str] | None): Tags in which inline images should be preserved. Defaults to None.
|
|
84
|
+
- newline_style (Literal["spaces", "backslash"]): Style for handling newlines in text content. Defaults to "spaces".
|
|
85
|
+
- strip (Iterable[str] | None): Tags to strip from the output. Defaults to None.
|
|
86
|
+
- strong*em_symbol (Literal["\*", "*"]): Symbol to use for strong/emphasized text. Defaults to "\*".
|
|
87
|
+
- sub_symbol (str): Custom symbol for subscript text. Defaults to an empty string.
|
|
88
|
+
- sup_symbol (str): Custom symbol for superscript text. Defaults to an empty string.
|
|
89
|
+
- wrap (bool): Wrap text to the specified width. Defaults to False.
|
|
90
|
+
- wrap_width (int): The number of characters at which to wrap text. Defaults to 80.
|
|
91
|
+
- convert_as_inline (bool): Treat the content as inline elements (no block elements like paragraphs). Defaults to False.
|
|
92
|
+
|
|
93
|
+
## CLI
|
|
94
|
+
|
|
95
|
+
For compatibility with the original markdownify, a CLI is provided. Use `html_to_markdown example.html > example.md` or
|
|
96
|
+
pipe input from stdin:
|
|
97
|
+
|
|
98
|
+
```shell
|
|
99
|
+
cat example.html | html_to_markdown > example.md
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
Use `html_to_markdown -h` to see all available options. They are the same as listed above and take the same arguments.
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
# html_to_markdown
|
|
2
|
+
|
|
3
|
+
This library is a refactored and modernized fork of [markdownify](https://pypi.org/project/markdownify/), supporting
|
|
4
|
+
Python 3.9 and above.
|
|
5
|
+
|
|
6
|
+
### Differences with the Markdownify
|
|
7
|
+
|
|
8
|
+
- The refactored codebase uses a strict functional approach - no classes are involved.
|
|
9
|
+
- There is full typing with strict MyPy strict adherence and a py.typed file included.
|
|
10
|
+
- The `convert_to_markdown` function allows passing a pre-configured instance of `BeautifulSoup` instead of html.
|
|
11
|
+
- This library releases follows standard semver. Its version v1.0.0 was branched from markdownify's v0.13.1, at which
|
|
12
|
+
point versioning is no longer aligned.
|
|
13
|
+
|
|
14
|
+
## Installation
|
|
15
|
+
|
|
16
|
+
```shell
|
|
17
|
+
pip install html_to_markdown
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## Usage
|
|
21
|
+
|
|
22
|
+
Convert an string HTML to Markdown:
|
|
23
|
+
|
|
24
|
+
```python
|
|
25
|
+
from html_to_markdown import convert_to_markdown
|
|
26
|
+
|
|
27
|
+
convert_to_markdown('<b>Yay</b> <a href="http://github.com">GitHub</a>') # > '**Yay** [GitHub](http://github.com)'
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
Or pass a pre-configured instance of `BeautifulSoup`:
|
|
31
|
+
|
|
32
|
+
```python
|
|
33
|
+
from bs4 import BeautifulSoup
|
|
34
|
+
from html_to_markdown import convert_to_markdown
|
|
35
|
+
|
|
36
|
+
soup = BeautifulSoup('<b>Yay</b> <a href="http://github.com">GitHub</a>', 'lxml') # lxml requires an extra dependency.
|
|
37
|
+
|
|
38
|
+
convert_to_markdown(soup) # > '**Yay** [GitHub](http://github.com)'
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
### Options
|
|
42
|
+
|
|
43
|
+
The `convert_to_markdown` function accepts the following kwargs:
|
|
44
|
+
|
|
45
|
+
- autolinks (bool): Automatically convert valid URLs into Markdown links. Defaults to True.
|
|
46
|
+
- bullets (str): A string of characters to use for bullet points in lists. Defaults to '\*+-'.
|
|
47
|
+
- code_language (str): Default language identifier for fenced code blocks. Defaults to an empty string.
|
|
48
|
+
- code_language_callback (Callable[[Any], str] | None): Function to dynamically determine the language for code blocks.
|
|
49
|
+
- convert (Iterable[str] | None): A list of tag names to convert to Markdown. If None, all supported tags are converted.
|
|
50
|
+
- default_title (bool): Use the default title when converting certain elements (e.g., links). Defaults to False.
|
|
51
|
+
- escape_asterisks (bool): Escape asterisks (\*) to prevent unintended Markdown formatting. Defaults to True.
|
|
52
|
+
- escape_misc (bool): Escape miscellaneous characters to prevent conflicts in Markdown. Defaults to True.
|
|
53
|
+
- escape*underscores (bool): Escape underscores (*) to prevent unintended italic formatting. Defaults to True.
|
|
54
|
+
- heading_style (Literal["underlined", "atx", "atx_closed"]): The style to use for Markdown headings. Defaults to "
|
|
55
|
+
underlined".
|
|
56
|
+
- keep_inline_images_in (Iterable[str] | None): Tags in which inline images should be preserved. Defaults to None.
|
|
57
|
+
- newline_style (Literal["spaces", "backslash"]): Style for handling newlines in text content. Defaults to "spaces".
|
|
58
|
+
- strip (Iterable[str] | None): Tags to strip from the output. Defaults to None.
|
|
59
|
+
- strong*em_symbol (Literal["\*", "*"]): Symbol to use for strong/emphasized text. Defaults to "\*".
|
|
60
|
+
- sub_symbol (str): Custom symbol for subscript text. Defaults to an empty string.
|
|
61
|
+
- sup_symbol (str): Custom symbol for superscript text. Defaults to an empty string.
|
|
62
|
+
- wrap (bool): Wrap text to the specified width. Defaults to False.
|
|
63
|
+
- wrap_width (int): The number of characters at which to wrap text. Defaults to 80.
|
|
64
|
+
- convert_as_inline (bool): Treat the content as inline elements (no block elements like paragraphs). Defaults to False.
|
|
65
|
+
|
|
66
|
+
## CLI
|
|
67
|
+
|
|
68
|
+
For compatibility with the original markdownify, a CLI is provided. Use `html_to_markdown example.html > example.md` or
|
|
69
|
+
pipe input from stdin:
|
|
70
|
+
|
|
71
|
+
```shell
|
|
72
|
+
cat example.html | html_to_markdown > example.md
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
Use `html_to_markdown -h` to see all available options. They are the same as listed above and take the same arguments.
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
def main(argv: list[str]) -> str:
|
|
2
|
+
"""Command-line entry point."""
|
|
3
|
+
from argparse import ArgumentParser, FileType
|
|
4
|
+
from sys import stdin
|
|
5
|
+
|
|
6
|
+
from html_to_markdown.constants import ASTERISK, ATX, ATX_CLOSED, BACKSLASH, SPACES, UNDERLINED, UNDERSCORE
|
|
7
|
+
from html_to_markdown.processing import convert_to_markdown
|
|
8
|
+
|
|
9
|
+
parser = ArgumentParser(
|
|
10
|
+
prog="html_to_markdown",
|
|
11
|
+
description="Converts HTML to Markdown.",
|
|
12
|
+
)
|
|
13
|
+
|
|
14
|
+
parser.add_argument(
|
|
15
|
+
"html",
|
|
16
|
+
nargs="?",
|
|
17
|
+
type=FileType("r"),
|
|
18
|
+
default=stdin,
|
|
19
|
+
help="The HTML file to convert. Defaults to STDIN if not provided.",
|
|
20
|
+
)
|
|
21
|
+
|
|
22
|
+
parser.add_argument(
|
|
23
|
+
"-s",
|
|
24
|
+
"--strip",
|
|
25
|
+
nargs="*",
|
|
26
|
+
help="A list of tags to strip from the conversion. Incompatible with the --convert option.",
|
|
27
|
+
)
|
|
28
|
+
|
|
29
|
+
parser.add_argument(
|
|
30
|
+
"-c",
|
|
31
|
+
"--convert",
|
|
32
|
+
nargs="*",
|
|
33
|
+
help="A list of HTML tags to explicitly convert. Incompatible with the --strip option.",
|
|
34
|
+
)
|
|
35
|
+
|
|
36
|
+
parser.add_argument(
|
|
37
|
+
"-a",
|
|
38
|
+
"--autolinks",
|
|
39
|
+
action="store_true",
|
|
40
|
+
help="Automatically convert anchor links where the content matches the href.",
|
|
41
|
+
)
|
|
42
|
+
|
|
43
|
+
parser.add_argument(
|
|
44
|
+
"--default-title",
|
|
45
|
+
action="store_false",
|
|
46
|
+
help="Use this flag to disable setting the link title to its href when no title is provided.",
|
|
47
|
+
)
|
|
48
|
+
|
|
49
|
+
parser.add_argument(
|
|
50
|
+
"--heading-style",
|
|
51
|
+
default=UNDERLINED,
|
|
52
|
+
choices=(ATX, ATX_CLOSED, UNDERLINED),
|
|
53
|
+
help="Defines the heading conversion style: 'atx', 'atx_closed', or 'underlined'. Defaults to 'underlined'.",
|
|
54
|
+
)
|
|
55
|
+
|
|
56
|
+
parser.add_argument(
|
|
57
|
+
"-b",
|
|
58
|
+
"--bullets",
|
|
59
|
+
default="*+-",
|
|
60
|
+
help="A string of bullet styles to use for list items. The style alternates based on nesting level. Defaults to '*+-'.",
|
|
61
|
+
)
|
|
62
|
+
|
|
63
|
+
parser.add_argument(
|
|
64
|
+
"--strong-em-symbol",
|
|
65
|
+
default=ASTERISK,
|
|
66
|
+
choices=(ASTERISK, UNDERSCORE),
|
|
67
|
+
help="Choose between '*' or '_' for strong and emphasized text. Defaults to '*'.",
|
|
68
|
+
)
|
|
69
|
+
|
|
70
|
+
parser.add_argument(
|
|
71
|
+
"--sub-symbol",
|
|
72
|
+
default="",
|
|
73
|
+
help="Define the characters used to surround <sub> text. Defaults to empty.",
|
|
74
|
+
)
|
|
75
|
+
|
|
76
|
+
parser.add_argument(
|
|
77
|
+
"--sup-symbol",
|
|
78
|
+
default="",
|
|
79
|
+
help="Define the characters used to surround <sup> text. Defaults to empty.",
|
|
80
|
+
)
|
|
81
|
+
|
|
82
|
+
parser.add_argument(
|
|
83
|
+
"--newline-style",
|
|
84
|
+
default=SPACES,
|
|
85
|
+
choices=(SPACES, BACKSLASH),
|
|
86
|
+
help="Specify the <br> conversion style: two spaces (default) or a backslash at the end of the line.",
|
|
87
|
+
)
|
|
88
|
+
|
|
89
|
+
parser.add_argument(
|
|
90
|
+
"--code-language",
|
|
91
|
+
default="",
|
|
92
|
+
help="Specify the default language for code blocks inside <pre> tags. Defaults to empty.",
|
|
93
|
+
)
|
|
94
|
+
|
|
95
|
+
parser.add_argument(
|
|
96
|
+
"--no-escape-asterisks",
|
|
97
|
+
dest="escape_asterisks",
|
|
98
|
+
action="store_false",
|
|
99
|
+
help="Disable escaping of '*' characters in text to '\\*'.",
|
|
100
|
+
)
|
|
101
|
+
|
|
102
|
+
parser.add_argument(
|
|
103
|
+
"--no-escape-underscores",
|
|
104
|
+
dest="escape_underscores",
|
|
105
|
+
action="store_false",
|
|
106
|
+
help="Disable escaping of '_' characters in text to '\\_'.",
|
|
107
|
+
)
|
|
108
|
+
|
|
109
|
+
parser.add_argument(
|
|
110
|
+
"-i",
|
|
111
|
+
"--keep-inline-images-in",
|
|
112
|
+
nargs="*",
|
|
113
|
+
help="Specify parent tags where inline images should be preserved as images, rather than converted to alt-text. Defaults to None.",
|
|
114
|
+
)
|
|
115
|
+
|
|
116
|
+
parser.add_argument(
|
|
117
|
+
"-w",
|
|
118
|
+
"--wrap",
|
|
119
|
+
action="store_true",
|
|
120
|
+
help="Enable word wrapping for paragraphs at --wrap-width characters.",
|
|
121
|
+
)
|
|
122
|
+
|
|
123
|
+
parser.add_argument(
|
|
124
|
+
"--wrap-width",
|
|
125
|
+
type=int,
|
|
126
|
+
default=80,
|
|
127
|
+
help="The number of characters at which text paragraphs should wrap. Defaults to 80.",
|
|
128
|
+
)
|
|
129
|
+
|
|
130
|
+
args = parser.parse_args(argv)
|
|
131
|
+
|
|
132
|
+
return convert_to_markdown(
|
|
133
|
+
args.html.read(),
|
|
134
|
+
strip=args.strip,
|
|
135
|
+
convert=args.convert,
|
|
136
|
+
autolinks=args.autolinks,
|
|
137
|
+
default_title=args.default_title,
|
|
138
|
+
heading_style=args.heading_style,
|
|
139
|
+
bullets=args.bullets,
|
|
140
|
+
strong_em_symbol=args.strong_em_symbol,
|
|
141
|
+
sub_symbol=args.sub_symbol,
|
|
142
|
+
sup_symbol=args.sup_symbol,
|
|
143
|
+
newline_style=args.newline_style,
|
|
144
|
+
code_language=args.code_language,
|
|
145
|
+
escape_asterisks=args.escape_asterisks,
|
|
146
|
+
escape_underscores=args.escape_underscores,
|
|
147
|
+
keep_inline_images_in=args.keep_inline_images_in,
|
|
148
|
+
wrap=args.wrap,
|
|
149
|
+
wrap_width=args.wrap_width,
|
|
150
|
+
)
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import re
|
|
4
|
+
from re import Pattern
|
|
5
|
+
from typing import Final
|
|
6
|
+
|
|
7
|
+
convert_heading_re: Final[Pattern[str]] = re.compile(r"convert_h(\d+)")
|
|
8
|
+
line_beginning_re: Final[Pattern[str]] = re.compile(r"^", re.MULTILINE)
|
|
9
|
+
whitespace_re: Final[Pattern[str]] = re.compile(r"[\t ]+")
|
|
10
|
+
html_heading_re: Final[Pattern[str]] = re.compile(r"h[1-6]")
|
|
11
|
+
|
|
12
|
+
ASTERISK: Final = "*"
|
|
13
|
+
ATX: Final = "atx"
|
|
14
|
+
ATX_CLOSED: Final = "atx_closed"
|
|
15
|
+
BACKSLASH: Final = "backslash"
|
|
16
|
+
UNDERLINED: Final = "underlined"
|
|
17
|
+
SPACES: Final = "spaces"
|
|
18
|
+
UNDERSCORE: Final = "_"
|
|
@@ -55,17 +55,19 @@ SupportedElements = Literal[
|
|
|
55
55
|
"kbd",
|
|
56
56
|
]
|
|
57
57
|
|
|
58
|
-
|
|
58
|
+
ConvertersMap = Mapping[SupportedElements, Callable[[str, Tag], str]]
|
|
59
59
|
|
|
60
60
|
T = TypeVar("T")
|
|
61
61
|
|
|
62
62
|
|
|
63
63
|
def _create_inline_converter(markup_prefix: str) -> Callable[[Tag, str], str]:
|
|
64
|
-
"""
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
64
|
+
"""Create an inline converter for a markup pattern or tag.
|
|
65
|
+
|
|
66
|
+
Args:
|
|
67
|
+
markup_prefix: The markup prefix to insert.
|
|
68
|
+
|
|
69
|
+
Returns:
|
|
70
|
+
A function that can be used to convert HTML to Markdown.
|
|
69
71
|
"""
|
|
70
72
|
|
|
71
73
|
def implementation(*, tag: Tag, text: str) -> str:
|
|
@@ -147,9 +149,9 @@ def _convert_hn(
|
|
|
147
149
|
|
|
148
150
|
|
|
149
151
|
def _convert_img(*, tag: Tag, convert_as_inline: bool, keep_inline_images_in: Iterable[str] | None) -> str:
|
|
150
|
-
alt = tag.attrs.get("alt",
|
|
151
|
-
src = tag.attrs.get("src",
|
|
152
|
-
title = tag.attrs.get("title",
|
|
152
|
+
alt = tag.attrs.get("alt", "")
|
|
153
|
+
src = tag.attrs.get("src", "")
|
|
154
|
+
title = tag.attrs.get("title", "")
|
|
153
155
|
title_part = ' "{}"'.format(title.replace('"', r"\"")) if title else ""
|
|
154
156
|
parent_name = tag.parent.name if tag.parent else ""
|
|
155
157
|
if convert_as_inline and parent_name not in (keep_inline_images_in or []):
|
|
@@ -295,7 +297,7 @@ def create_converters_map(
|
|
|
295
297
|
sup_symbol: str,
|
|
296
298
|
wrap: bool,
|
|
297
299
|
wrap_width: int,
|
|
298
|
-
) ->
|
|
300
|
+
) -> ConvertersMap:
|
|
299
301
|
"""Create a mapping of HTML elements to their corresponding conversion functions.
|
|
300
302
|
|
|
301
303
|
Args:
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from typing import TYPE_CHECKING, Literal
|
|
4
|
+
|
|
5
|
+
from html_to_markdown.constants import ASTERISK, SPACES, UNDERLINED
|
|
6
|
+
from html_to_markdown.converters import create_converters_map
|
|
7
|
+
|
|
8
|
+
if TYPE_CHECKING:
|
|
9
|
+
from collections.abc import Callable, Iterable
|
|
10
|
+
|
|
11
|
+
from bs4 import Tag
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def _create_legacy_class(
|
|
15
|
+
autolinks: bool,
|
|
16
|
+
bullets: str,
|
|
17
|
+
code_language: str,
|
|
18
|
+
code_language_callback: Callable[[Tag], str] | None,
|
|
19
|
+
default_title: bool,
|
|
20
|
+
heading_style: Literal["atx", "atx_closed", "underlined"],
|
|
21
|
+
keep_inline_images_in: Iterable[str] | None,
|
|
22
|
+
newline_style: str,
|
|
23
|
+
strong_em_symbol: str,
|
|
24
|
+
sub_symbol: str,
|
|
25
|
+
sup_symbol: str,
|
|
26
|
+
wrap: bool,
|
|
27
|
+
wrap_width: int,
|
|
28
|
+
) -> type:
|
|
29
|
+
"""Create a legacy class for Markdownify.
|
|
30
|
+
|
|
31
|
+
Deprecated: Use the new hooks api instead.
|
|
32
|
+
|
|
33
|
+
Args:
|
|
34
|
+
autolinks: Whether to convert URLs into links.
|
|
35
|
+
bullets: The bullet characters to use for unordered lists.
|
|
36
|
+
code_language: The default code language to use.
|
|
37
|
+
code_language_callback: A callback to get the code language.
|
|
38
|
+
default_title: Whether to use the URL as the title for links.
|
|
39
|
+
heading_style: The style of headings.
|
|
40
|
+
keep_inline_images_in: The tags to keep inline images in.
|
|
41
|
+
newline_style: The style of newlines.
|
|
42
|
+
strong_em_symbol: The symbol to use for strong and emphasis text.
|
|
43
|
+
sub_symbol: The symbol to use for subscript text.
|
|
44
|
+
sup_symbol: The symbol to use for superscript text.
|
|
45
|
+
wrap: Whether to wrap text.
|
|
46
|
+
wrap_width: The width to wrap text at.
|
|
47
|
+
|
|
48
|
+
Returns:
|
|
49
|
+
A class that can be used to convert HTML to Markdown.
|
|
50
|
+
"""
|
|
51
|
+
return type(
|
|
52
|
+
"Markdownify",
|
|
53
|
+
(),
|
|
54
|
+
{
|
|
55
|
+
k.removeprefix("_"): v
|
|
56
|
+
for k, v in create_converters_map(
|
|
57
|
+
autolinks=autolinks,
|
|
58
|
+
bullets=bullets,
|
|
59
|
+
code_language=code_language,
|
|
60
|
+
code_language_callback=code_language_callback,
|
|
61
|
+
default_title=default_title,
|
|
62
|
+
heading_style=heading_style,
|
|
63
|
+
keep_inline_images_in=keep_inline_images_in,
|
|
64
|
+
newline_style=newline_style,
|
|
65
|
+
strong_em_symbol=strong_em_symbol,
|
|
66
|
+
sub_symbol=sub_symbol,
|
|
67
|
+
sup_symbol=sup_symbol,
|
|
68
|
+
wrap=wrap,
|
|
69
|
+
wrap_width=wrap_width,
|
|
70
|
+
).items()
|
|
71
|
+
},
|
|
72
|
+
)
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
Markdownify = _create_legacy_class(
|
|
76
|
+
autolinks=True,
|
|
77
|
+
bullets="*+-",
|
|
78
|
+
code_language="",
|
|
79
|
+
code_language_callback=None,
|
|
80
|
+
default_title=False,
|
|
81
|
+
heading_style=UNDERLINED,
|
|
82
|
+
keep_inline_images_in=None,
|
|
83
|
+
newline_style=SPACES,
|
|
84
|
+
strong_em_symbol=ASTERISK,
|
|
85
|
+
sub_symbol="",
|
|
86
|
+
sup_symbol="",
|
|
87
|
+
wrap=False,
|
|
88
|
+
wrap_width=80,
|
|
89
|
+
)
|