contaix.htmlmd¶
Light html -> markdown: no web-scraping stack, optional sanitising for untrusted html.
import contaix.htmlmd costs only scraped.html_markdown (markdownify and
bs4): no pandas, numpy, requests or notebook tooling. contaix.web re-exports
html_to_clean_markdown(), so existing imports keep working.
>>> html_to_clean_markdown("<h1>Hi</h1><p>a <b>b</b></p>")
'# Hi\n\na **b**'
For html you do not trust, sanitize=True keeps only structure and words:
>>> html_to_clean_markdown('<p onclick="x()">a</p><script>x()</script>', sanitize=True)
'a'
Functions
|
Convert HTML to clean markdown (via |
|
The content of an html page as a safe fragment: structure and words, nothing live. |
- contaix.htmlmd.html_to_clean_markdown(html, *, body_width=0, ignore_images=True, include_links=True, sanitize=False, **markdownify_options)[source]¶
Convert HTML to clean markdown (via
markdownify, MIT-licensed).A thin wrapper over
scraped.html_markdown.html_to_markdown_str()(with thelxmlparser and no trailing newline), which owns the cleaning passes.- Parameters:
html (
str) – Raw HTML content.body_width (
int) – Kept for backward compatibility. Only0(no wrapping, the default) is supported; any other value raisesValueError.ignore_images (
bool) – Whether to skip image tags.include_links (
bool) – Whether to include hyperlinks in output (Falsekeeps the link text only).sanitize (
bool) – Pass the html throughsanitize_html()first: use it for untrusted html. Remote images become their alt text, and onlyhttp(s)/mailtolinks stay.**markdownify_options – Options for
markdownify.MarkdownConverter, overridingHTML_TO_MARKDOWN_DEFAULTS. Unknown names raiseTypeError(MarkdownConverterwould silently ignore them), in particular thehtml2textoptions this function accepted before (ignore_links,unicode_snob, …).href='u'>c</a></p>") (>>> html_to_clean_markdown("<h1>Hi</h1><p>a <b>b</b> <a)
[c](u)' ('# Hinna b)
href='u'>c</a></p>" (>>> html_to_clean_markdown("<p>a <a)
include_links=False)
c' ('a)
href='javascript (>>> html_to_clean_markdown("<p>a <a)
c'
- Return type:
- contaix.htmlmd.sanitize_html(text, *, rewrite_src=<function <lambda>>, rewrite_href=None, max_depth=100, max_span=100)[source]¶
The content of an html page as a safe fragment: structure and words, nothing live.
Only
ALLOWED_TAGSsurvive, with a few plain attributes (KEEP_ATTRS): no script, style, frame, form, event handler,classorid. A link keeps only anhttp(s)ormailtoaddress, whichrewrite_href(href), when given, may change or refuse (None: the link keeps its words and loses its address).rewrite_src(src)decides each image: the url to show it from, orNonefor its alt text instead (by default every remote image becomes its alt text). Nesting deeper thanmax_depthis unwrapped, spans are capped atmax_span, and unbalanced tags are closed so the fragment can never swallow what is joined after it.>>> sanitize_html('<body><script>x()</script><h1 onclick="x()">Hi</h1><a href="javascript:x()">y</a></body>') '<h1>Hi</h1><a>y</a>' >>> sanitize_html("<div><p>unclosed") '<div><p>unclosed</p></div>' >>> sanitize_html('<img src="http://e/x.png" alt="a cat">') 'a cat'
- Return type: