wordlift-sdk 2.8.0__py3-none-any.whl → 2.9.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.
- wordlift_sdk/utils/__init__.py +6 -2
- wordlift_sdk/utils/html_converter.py +56 -0
- {wordlift_sdk-2.8.0.dist-info → wordlift_sdk-2.9.0.dist-info}/METADATA +1 -1
- {wordlift_sdk-2.8.0.dist-info → wordlift_sdk-2.9.0.dist-info}/RECORD +5 -4
- {wordlift_sdk-2.8.0.dist-info → wordlift_sdk-2.9.0.dist-info}/WHEEL +0 -0
wordlift_sdk/utils/__init__.py
CHANGED
|
@@ -1,10 +1,13 @@
|
|
|
1
1
|
from .create_dataframe_from_google_sheets import create_dataframe_from_google_sheets
|
|
2
2
|
from .create_dataframe_of_entities_by_types import create_dataframe_of_entities_by_types
|
|
3
|
-
from .create_dataframe_of_entities_with_embedding_vectors import
|
|
3
|
+
from .create_dataframe_of_entities_with_embedding_vectors import (
|
|
4
|
+
create_dataframe_of_entities_with_embedding_vectors,
|
|
5
|
+
)
|
|
4
6
|
from .create_dataframe_of_url_iri import create_dataframe_of_url_iri
|
|
5
7
|
from .create_entity_patch_request import create_entity_patch_request
|
|
6
8
|
from .delayed import create_delayed
|
|
7
9
|
from .get_me import get_me
|
|
10
|
+
from .html_converter import HtmlConverter
|
|
8
11
|
|
|
9
12
|
__all__ = [
|
|
10
13
|
"create_dataframe_from_google_sheets",
|
|
@@ -13,5 +16,6 @@ __all__ = [
|
|
|
13
16
|
"create_dataframe_of_url_iri",
|
|
14
17
|
"create_entity_patch_request",
|
|
15
18
|
"create_delayed",
|
|
16
|
-
"get_me"
|
|
19
|
+
"get_me",
|
|
20
|
+
"HtmlConverter",
|
|
17
21
|
]
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
"""HTML to XHTML conversion utility."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import re
|
|
6
|
+
from typing import Any
|
|
7
|
+
|
|
8
|
+
_INVALID_XML_CHARS_RE = re.compile(r"[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]")
|
|
9
|
+
_XML_NAME_RE = re.compile(r"^[A-Za-z_][A-Za-z0-9_.:-]*$")
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class HtmlConverter:
|
|
13
|
+
"""Converts HTML to XHTML."""
|
|
14
|
+
|
|
15
|
+
def convert(self, html: str) -> str:
|
|
16
|
+
"""
|
|
17
|
+
Convert an HTML string to a valid XHTML string.
|
|
18
|
+
|
|
19
|
+
Args:
|
|
20
|
+
html: The raw HTML string.
|
|
21
|
+
|
|
22
|
+
Returns:
|
|
23
|
+
A sanitized XHTML string.
|
|
24
|
+
"""
|
|
25
|
+
html = re.sub(r"<!DOCTYPE[^>]*>", "", html, flags=re.IGNORECASE)
|
|
26
|
+
html = self._strip_invalid_xml_chars(html)
|
|
27
|
+
try:
|
|
28
|
+
from lxml import html as lxml_html
|
|
29
|
+
except ImportError as exc:
|
|
30
|
+
raise ImportError(
|
|
31
|
+
"lxml is required for XHTML output. Install with: pip install lxml"
|
|
32
|
+
) from exc
|
|
33
|
+
|
|
34
|
+
try:
|
|
35
|
+
parser = lxml_html.HTMLParser(encoding="utf-8", recover=True)
|
|
36
|
+
doc = lxml_html.document_fromstring(html, parser=parser)
|
|
37
|
+
self._sanitize_xhtml_tree(doc)
|
|
38
|
+
xhtml = lxml_html.tostring(doc, encoding="unicode", method="xml")
|
|
39
|
+
return self._strip_invalid_xml_chars(xhtml)
|
|
40
|
+
except Exception as exc:
|
|
41
|
+
raise RuntimeError("Failed to convert HTML to XHTML.") from exc
|
|
42
|
+
|
|
43
|
+
def _strip_invalid_xml_chars(self, value: str) -> str:
|
|
44
|
+
return _INVALID_XML_CHARS_RE.sub("", value)
|
|
45
|
+
|
|
46
|
+
def _sanitize_xhtml_tree(self, doc: Any) -> None:
|
|
47
|
+
for element in doc.iter():
|
|
48
|
+
if not hasattr(element, "attrib"):
|
|
49
|
+
continue
|
|
50
|
+
for attr in list(element.attrib):
|
|
51
|
+
if not _XML_NAME_RE.match(attr):
|
|
52
|
+
del element.attrib[attr]
|
|
53
|
+
continue
|
|
54
|
+
value = element.attrib.get(attr)
|
|
55
|
+
if isinstance(value, str):
|
|
56
|
+
element.attrib[attr] = self._strip_invalid_xml_chars(value)
|
|
@@ -67,7 +67,7 @@ wordlift_sdk/url_source/new_or_changed_url_source.py,sha256=81ZEitfxxMof5-fcKuvT
|
|
|
67
67
|
wordlift_sdk/url_source/sitemap_url_source.py,sha256=JMIteakIFIWcHoQ3Uf0fyMh8pYz4YM_rnJvdm7NXYak,1198
|
|
68
68
|
wordlift_sdk/url_source/url_source.py,sha256=ZmGOiDlPVzoOTt2Qz69onpnFzQYZlrwWaogOkZMEqGc,403
|
|
69
69
|
wordlift_sdk/url_source/url_source_input.py,sha256=GvRAjBb103DS--rR7M8oIxIjWhQbYUTldwbPx2BctaU,152
|
|
70
|
-
wordlift_sdk/utils/__init__.py,sha256=
|
|
70
|
+
wordlift_sdk/utils/__init__.py,sha256=3l2N8t7LlaEScp08rRApx6fKvx3bo9hnUdGAcZRZG8w,832
|
|
71
71
|
wordlift_sdk/utils/create_dataframe_from_google_sheets.py,sha256=7j0oCwUwSqoo2505_QfcUCx4XNqhw94Y_UTL5tyIk5U,1117
|
|
72
72
|
wordlift_sdk/utils/create_dataframe_of_entities_by_types.py,sha256=5U5k_OheifPJyod4PB0HEdYCGwZEMgkdiwckAR8LFJY,770
|
|
73
73
|
wordlift_sdk/utils/create_dataframe_of_entities_with_embedding_vectors.py,sha256=A_JMVoYS4pcWCsKYGH_sHyc9MJvg5EuoKELp1BCI2Ko,679
|
|
@@ -75,6 +75,7 @@ wordlift_sdk/utils/create_dataframe_of_url_iri.py,sha256=xTxNyHb1Vo7YdfoYoaiCqp6
|
|
|
75
75
|
wordlift_sdk/utils/create_entity_patch_request.py,sha256=-fEZrflVpTBtk7c7ME8-BWn6-r86cQJp7fvQJznj_aU,497
|
|
76
76
|
wordlift_sdk/utils/delayed.py,sha256=uy-cQsLMprakx8CiQl6ZLL49bfYB1C6oQt4UObr5TwU,323
|
|
77
77
|
wordlift_sdk/utils/get_me.py,sha256=-mr0DscVWEDFvgBYtmgND3_gIuh2q7kfD-OiqrYwLJU,297
|
|
78
|
+
wordlift_sdk/utils/html_converter.py,sha256=MGdXQg2EAhI2K7CWtbIzZip08clVPUgL84Xt_xxa-h4,1945
|
|
78
79
|
wordlift_sdk/utils/import_url.py,sha256=KiUFkU4mRfd7YWFlLoz5bB13AgPURV8Km8H05LxevCI,1299
|
|
79
80
|
wordlift_sdk/wordlift/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
80
81
|
wordlift_sdk/wordlift/entity_gaps/__init__.py,sha256=PMtdyBoUD-oiTE-7Izr6fuod7OxGOQIhFSasZG_SafM,109
|
|
@@ -97,6 +98,6 @@ wordlift_sdk/workflow/url_handler/default_url_handler.py,sha256=irMoJftx9Gyq-8HQ
|
|
|
97
98
|
wordlift_sdk/workflow/url_handler/search_console_url_handler.py,sha256=uOwD4t009-cA9JI7ZtbYhCGRwjhLzDpBNTWHERPnNqI,2698
|
|
98
99
|
wordlift_sdk/workflow/url_handler/url_handler.py,sha256=meyOpWVhLk2NcbtUOO_V0z6_T9q-D3pD7kWQCRioYh0,129
|
|
99
100
|
wordlift_sdk/workflow/url_handler/web_page_import_url_handler.py,sha256=xQiy-fgFj_dWKFFBNWeodAGhIMJQereSPdgnjK-Crmo,3529
|
|
100
|
-
wordlift_sdk-2.
|
|
101
|
-
wordlift_sdk-2.
|
|
102
|
-
wordlift_sdk-2.
|
|
101
|
+
wordlift_sdk-2.9.0.dist-info/METADATA,sha256=NF2Kp2Z4kFdv2TXA7Tf3zJjzebs_EuH8MG0deldHtx4,5098
|
|
102
|
+
wordlift_sdk-2.9.0.dist-info/WHEEL,sha256=zp0Cn7JsFoX2ATtOhtaFYIiE2rmFAD4OcMhtUki8W3U,88
|
|
103
|
+
wordlift_sdk-2.9.0.dist-info/RECORD,,
|
|
File without changes
|