html-to-markdown 1.2.1__py3-none-any.whl → 1.3.0__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.
Potentially problematic release.
This version of html-to-markdown might be problematic. Click here for more details.
- html_to_markdown/converters.py +6 -2
- html_to_markdown/processing.py +11 -3
- {html_to_markdown-1.2.1.dist-info → html_to_markdown-1.3.0.dist-info}/METADATA +26 -4
- html_to_markdown-1.3.0.dist-info/RECORD +13 -0
- {html_to_markdown-1.2.1.dist-info → html_to_markdown-1.3.0.dist-info}/WHEEL +2 -1
- html_to_markdown-1.3.0.dist-info/top_level.txt +1 -0
- html_to_markdown-1.2.1.dist-info/RECORD +0 -12
- {html_to_markdown-1.2.1.dist-info → html_to_markdown-1.3.0.dist-info}/licenses/LICENSE +0 -0
html_to_markdown/converters.py
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
from __future__ import annotations
|
|
2
2
|
|
|
3
|
-
from
|
|
3
|
+
from typing import TYPE_CHECKING
|
|
4
|
+
|
|
5
|
+
if TYPE_CHECKING:
|
|
6
|
+
from collections.abc import Iterable
|
|
4
7
|
from functools import partial
|
|
5
8
|
from inspect import getfullargspec
|
|
6
9
|
from textwrap import fill
|
|
@@ -55,7 +58,8 @@ SupportedElements = Literal[
|
|
|
55
58
|
"kbd",
|
|
56
59
|
]
|
|
57
60
|
|
|
58
|
-
|
|
61
|
+
Converter = Callable[[str, Tag], str]
|
|
62
|
+
ConvertersMap = dict[SupportedElements, Converter]
|
|
59
63
|
|
|
60
64
|
T = TypeVar("T")
|
|
61
65
|
|
html_to_markdown/processing.py
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
from __future__ import annotations
|
|
2
2
|
|
|
3
|
+
from typing import TYPE_CHECKING
|
|
4
|
+
|
|
5
|
+
if TYPE_CHECKING:
|
|
6
|
+
from collections.abc import Mapping
|
|
3
7
|
from itertools import chain
|
|
4
8
|
from typing import TYPE_CHECKING, Any, Callable, Literal, cast
|
|
5
9
|
|
|
@@ -12,7 +16,7 @@ from html_to_markdown.constants import (
|
|
|
12
16
|
html_heading_re,
|
|
13
17
|
whitespace_re,
|
|
14
18
|
)
|
|
15
|
-
from html_to_markdown.converters import ConvertersMap, create_converters_map
|
|
19
|
+
from html_to_markdown.converters import Converter, ConvertersMap, SupportedElements, create_converters_map
|
|
16
20
|
from html_to_markdown.utils import escape
|
|
17
21
|
|
|
18
22
|
if TYPE_CHECKING:
|
|
@@ -189,6 +193,8 @@ def convert_to_markdown(
|
|
|
189
193
|
code_language: str = "",
|
|
190
194
|
code_language_callback: Callable[[Any], str] | None = None,
|
|
191
195
|
convert: str | Iterable[str] | None = None,
|
|
196
|
+
convert_as_inline: bool = False,
|
|
197
|
+
custom_converters: Mapping[SupportedElements, Converter] | None = None,
|
|
192
198
|
default_title: bool = False,
|
|
193
199
|
escape_asterisks: bool = True,
|
|
194
200
|
escape_misc: bool = True,
|
|
@@ -202,7 +208,6 @@ def convert_to_markdown(
|
|
|
202
208
|
sup_symbol: str = "",
|
|
203
209
|
wrap: bool = False,
|
|
204
210
|
wrap_width: int = 80,
|
|
205
|
-
convert_as_inline: bool = False,
|
|
206
211
|
) -> str:
|
|
207
212
|
"""Convert HTML to Markdown.
|
|
208
213
|
|
|
@@ -213,6 +218,8 @@ def convert_to_markdown(
|
|
|
213
218
|
code_language: Default language identifier for fenced code blocks. Defaults to an empty string.
|
|
214
219
|
code_language_callback: Function to dynamically determine the language for code blocks.
|
|
215
220
|
convert: A list of tag names to convert to Markdown. If None, all supported tags are converted.
|
|
221
|
+
convert_as_inline: Treat the content as inline elements (no block elements like paragraphs). Defaults to False.
|
|
222
|
+
custom_converters: A mapping of custom converters for specific HTML tags. Defaults to None.
|
|
216
223
|
default_title: Use the default title when converting certain elements (e.g., links). Defaults to False.
|
|
217
224
|
escape_asterisks: Escape asterisks (*) to prevent unintended Markdown formatting. Defaults to True.
|
|
218
225
|
escape_misc: Escape miscellaneous characters to prevent conflicts in Markdown. Defaults to True.
|
|
@@ -226,7 +233,6 @@ def convert_to_markdown(
|
|
|
226
233
|
sup_symbol: Custom symbol for superscript text. Defaults to an empty string.
|
|
227
234
|
wrap: Wrap text to the specified width. Defaults to False.
|
|
228
235
|
wrap_width: The number of characters at which to wrap text. Defaults to 80.
|
|
229
|
-
convert_as_inline: Treat the content as inline elements (no block elements like paragraphs). Defaults to False.
|
|
230
236
|
|
|
231
237
|
Raises:
|
|
232
238
|
ValueError: If both 'strip' and 'convert' are specified, or when the input HTML is empty.
|
|
@@ -260,6 +266,8 @@ def convert_to_markdown(
|
|
|
260
266
|
wrap=wrap,
|
|
261
267
|
wrap_width=wrap_width,
|
|
262
268
|
)
|
|
269
|
+
if custom_converters:
|
|
270
|
+
converters_map.update(cast("ConvertersMap", custom_converters))
|
|
263
271
|
|
|
264
272
|
return _process_tag(
|
|
265
273
|
source,
|
|
@@ -1,11 +1,10 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: html-to-markdown
|
|
3
|
-
Version: 1.
|
|
3
|
+
Version: 1.3.0
|
|
4
4
|
Summary: Convert HTML to markdown
|
|
5
|
-
Project-URL: homepage, https://github.com/Goldziher/html-to-markdown
|
|
6
5
|
Author-email: Na'aman Hirschfeld <nhirschfeld@gmail.com>
|
|
7
6
|
License: MIT
|
|
8
|
-
|
|
7
|
+
Project-URL: homepage, https://github.com/Goldziher/html-to-markdown
|
|
9
8
|
Keywords: converter,html,markdown,text-extraction,text-processing
|
|
10
9
|
Classifier: Intended Audience :: Developers
|
|
11
10
|
Classifier: License :: OSI Approved :: MIT License
|
|
@@ -23,8 +22,10 @@ Classifier: Topic :: Text Processing :: Markup :: Markdown
|
|
|
23
22
|
Classifier: Topic :: Utilities
|
|
24
23
|
Classifier: Typing :: Typed
|
|
25
24
|
Requires-Python: >=3.9
|
|
26
|
-
Requires-Dist: beautifulsoup4>=4.12.3
|
|
27
25
|
Description-Content-Type: text/markdown
|
|
26
|
+
License-File: LICENSE
|
|
27
|
+
Requires-Dist: beautifulsoup4>=4.12.3
|
|
28
|
+
Dynamic: license-file
|
|
28
29
|
|
|
29
30
|
# html-to-markdown
|
|
30
31
|
|
|
@@ -116,6 +117,26 @@ markdown = convert_to_markdown(
|
|
|
116
117
|
)
|
|
117
118
|
```
|
|
118
119
|
|
|
120
|
+
### Custom Converters
|
|
121
|
+
|
|
122
|
+
You can provide your own conversion functions for specific HTML tags:
|
|
123
|
+
|
|
124
|
+
```python
|
|
125
|
+
from bs4.element import Tag
|
|
126
|
+
from html_to_markdown import convert_to_markdown
|
|
127
|
+
|
|
128
|
+
# Define a custom converter for the <b> tag
|
|
129
|
+
def custom_bold_converter(*, tag: Tag, text: str, **kwargs) -> str:
|
|
130
|
+
return f"IMPORTANT: {text}"
|
|
131
|
+
|
|
132
|
+
html = "<p>This is a <b>bold statement</b>.</p>"
|
|
133
|
+
markdown = convert_to_markdown(html, custom_converters={"b": custom_bold_converter})
|
|
134
|
+
print(markdown)
|
|
135
|
+
# Output: This is a IMPORTANT: bold statement.
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
Custom converters take precedence over the built-in converters and can be used alongside other configuration options.
|
|
139
|
+
|
|
119
140
|
### Configuration Options
|
|
120
141
|
|
|
121
142
|
| Option | Type | Default | Description |
|
|
@@ -189,6 +210,7 @@ Full list of configuration options:
|
|
|
189
210
|
- `wrap`: Enable text wrapping
|
|
190
211
|
- `wrap_width`: Width for text wrapping
|
|
191
212
|
- `convert_as_inline`: Treat content as inline elements
|
|
213
|
+
- `custom_converters`: A mapping of HTML tag names to custom converter functions
|
|
192
214
|
|
|
193
215
|
## Contribution
|
|
194
216
|
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
html_to_markdown/__init__.py,sha256=95S7_7mR_g88uTnFI0FaRNykrtAaSKb6sJbwSea2zjk,145
|
|
2
|
+
html_to_markdown/__main__.py,sha256=u5xevySlT5eIGyLUaethdDQIKJygaKnc3F2sHWoz75g,264
|
|
3
|
+
html_to_markdown/cli.py,sha256=HVnzmcyrYwah_yWhZ87mZcG0VgnKYp6y89fJh2R-Rlw,4532
|
|
4
|
+
html_to_markdown/constants.py,sha256=Usk67k18tuRovJpKDsiEXdgH20KgqI9KOnK4Fbx-M5c,547
|
|
5
|
+
html_to_markdown/converters.py,sha256=p8arBdejEeuAp9_wIYvp5PuWNBB0M699CgLSEkW3v88,11910
|
|
6
|
+
html_to_markdown/processing.py,sha256=WNMzB_dt5yn11xK59zPVMb1aMCvMAEowpgg-tki8meI,9028
|
|
7
|
+
html_to_markdown/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
8
|
+
html_to_markdown/utils.py,sha256=HJUDej5HSpXRtYv-OkCyD0hwnPnVfQCwY6rBRlIOt9s,1989
|
|
9
|
+
html_to_markdown-1.3.0.dist-info/licenses/LICENSE,sha256=3J_HR5BWvUM1mlIrlkF32-uC1FM64gy8JfG17LBuheQ,1122
|
|
10
|
+
html_to_markdown-1.3.0.dist-info/METADATA,sha256=XU_lAyXhm3okv_ly0KSQK7LRHvSbQbW464qXnJ3ryVw,7653
|
|
11
|
+
html_to_markdown-1.3.0.dist-info/WHEEL,sha256=CmyFI0kx5cdEMTLiONQRbGQwjIoR1aIYB7eCAQ4KPJ0,91
|
|
12
|
+
html_to_markdown-1.3.0.dist-info/top_level.txt,sha256=Ev6djb1c4dSKr_-n4K-FpEGDkzBigXY6LuZ5onqS7AE,17
|
|
13
|
+
html_to_markdown-1.3.0.dist-info/RECORD,,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
html_to_markdown
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
html_to_markdown/__init__.py,sha256=95S7_7mR_g88uTnFI0FaRNykrtAaSKb6sJbwSea2zjk,145
|
|
2
|
-
html_to_markdown/__main__.py,sha256=u5xevySlT5eIGyLUaethdDQIKJygaKnc3F2sHWoz75g,264
|
|
3
|
-
html_to_markdown/cli.py,sha256=HVnzmcyrYwah_yWhZ87mZcG0VgnKYp6y89fJh2R-Rlw,4532
|
|
4
|
-
html_to_markdown/constants.py,sha256=Usk67k18tuRovJpKDsiEXdgH20KgqI9KOnK4Fbx-M5c,547
|
|
5
|
-
html_to_markdown/converters.py,sha256=W6Dq2PAwVe5nxE3LSaeO8_hm0eWzSBlRLxf0ryasL6Q,11844
|
|
6
|
-
html_to_markdown/processing.py,sha256=nh_Or-4faI_qh6gF8-xY2qNiqX4eH-jCnBnFpHJbc2M,8632
|
|
7
|
-
html_to_markdown/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
8
|
-
html_to_markdown/utils.py,sha256=HJUDej5HSpXRtYv-OkCyD0hwnPnVfQCwY6rBRlIOt9s,1989
|
|
9
|
-
html_to_markdown-1.2.1.dist-info/METADATA,sha256=-raxzt9vDtzHOOsR0nkbQN-r80V5gRFfeHjDOLWrDwk,6902
|
|
10
|
-
html_to_markdown-1.2.1.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
|
|
11
|
-
html_to_markdown-1.2.1.dist-info/licenses/LICENSE,sha256=3J_HR5BWvUM1mlIrlkF32-uC1FM64gy8JfG17LBuheQ,1122
|
|
12
|
-
html_to_markdown-1.2.1.dist-info/RECORD,,
|
|
File without changes
|