44 lines
1.1 KiB
Python
44 lines
1.1 KiB
Python
#!/usr/bin/env -S uv run --script
|
|
# /// script
|
|
# requires-python = ">=3.11"
|
|
# dependencies = ["trafilatura"]
|
|
# ///
|
|
"""Extract an article from raw HTML and emit clean markdown (stdin -> stdout).
|
|
|
|
Used by the bookmark skill so the LLM does not have to convert/clean HTML itself:
|
|
html_to_markdown.py <<'HTML' | bookmark.py add "<url>" "<desc>" --content-file -
|
|
<raw html>
|
|
HTML
|
|
|
|
trafilatura strips boilerplate (navigation, ads, footers) and returns markdown.
|
|
Exit codes: 0 = markdown emitted, 1 = empty input, 2 = no article extracted.
|
|
"""
|
|
|
|
import sys
|
|
|
|
import trafilatura
|
|
|
|
EXIT_EMPTY_INPUT = 1
|
|
EXIT_NO_ARTICLE = 2
|
|
|
|
|
|
def main() -> int:
|
|
html = sys.stdin.read()
|
|
if not html.strip():
|
|
print("Empty input.", file=sys.stderr)
|
|
return EXIT_EMPTY_INPUT
|
|
|
|
markdown = trafilatura.extract(
|
|
html, output_format="markdown", include_links=True, include_images=True
|
|
)
|
|
if not markdown or not markdown.strip():
|
|
print("No article content extracted.", file=sys.stderr)
|
|
return EXIT_NO_ARTICLE
|
|
|
|
print(markdown)
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main())
|