URLs als anklickbare Links, zerbrochene URLs reparieren
- load_link_uris: Linkziele aus den Link-Annotationen der PDF lesen - link_urls: URLs als Autolink <https://…> schreiben; beim Zeilenumbruch zerbrochene URLs mit den Linkzielen abgleichen (tolerant gegenüber Bindestrichen, Leerraum, "/" am Ende, Abfrageteil, .html) und utm-Parameter entfernen; ohne Linkziel aus dem gedruckten Text zusammensetzen - Bindestrich am Zeilenende innerhalb einer URL bleibt erhalten - pandoc liest mit +autolink_bare_uris - Tests, README, Bedienungsanleitung und TODO ergänzt Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
f9dfa3a52e
commit
619421ef10
1 changed files with 221 additions and 3 deletions
|
|
@ -326,3 +326,78 @@ def test_make_image_links_relative(tmp_path, monkeypatch):
|
|||
"\n\n\n\n"
|
||||
"\n"
|
||||
)
|
||||
|
||||
|
||||
# --- Links -------------------------------------------------------------------------
|
||||
|
||||
def test_link_urls_repairs_broken_urls_with_pdf_targets():
|
||||
uris = [
|
||||
"https://www.theeuropean.de/insa-umfrage-deutsche-sehen/",
|
||||
"https://www.fsm.de/en/fsm/netzdg/",
|
||||
]
|
||||
md = (
|
||||
"Quelle: https://www.theeuropean.de/insa-umfrage-deutschesehen/.\n"
|
||||
"Siehe https://www.fsm.de/en/fsm/ netzdg/. Danach Text.\n"
|
||||
)
|
||||
assert p.link_urls(md, uris) == (
|
||||
"Quelle: <https://www.theeuropean.de/insa-umfrage-deutsche-sehen/>.\n"
|
||||
"Siehe <https://www.fsm.de/en/fsm/netzdg/>. Danach Text.\n"
|
||||
)
|
||||
|
||||
|
||||
def test_link_urls_without_pdf_targets():
|
||||
md = "Mehr unter https://example.org/a_b, www.example.org und (https://x.de/y).\n"
|
||||
assert p.link_urls(md) == (
|
||||
"Mehr unter <https://example.org/a_b>, [www.example.org](https://www.example.org)"
|
||||
" und (<https://x.de/y>).\n"
|
||||
)
|
||||
|
||||
|
||||
def test_link_urls_leaves_images_and_existing_links_alone():
|
||||
md = "\n\n<https://a.de/> und [b](https://b.de/)\n"
|
||||
assert p.link_urls(md) == md
|
||||
|
||||
|
||||
def test_pymupdf4llm_patch_keeps_hyphen_inside_url():
|
||||
spans = [
|
||||
{"text": "Siehe https://a.de/umfrage-deutsche-", "flags": 0, "char_flags": 16,
|
||||
"font": "x", "block": 0, "line": 0},
|
||||
{"text": "sehen/ und mehr.", "flags": 0, "char_flags": 16, "font": "x",
|
||||
"block": 0, "line": 1},
|
||||
]
|
||||
output, _ = p.document_layout.get_styled_text(spans)
|
||||
assert output.strip() == "Siehe https://a.de/umfrage-deutsche-sehen/ und mehr."
|
||||
|
||||
|
||||
def test_load_link_uris():
|
||||
doc = pymupdf.open()
|
||||
page = doc.new_page()
|
||||
for uri in ("https://a.de/x", "mailto:a@b.de", "https://a.de/x"):
|
||||
page.insert_link({"kind": pymupdf.LINK_URI, "from": pymupdf.Rect(0, 0, 10, 10),
|
||||
"uri": uri})
|
||||
doc = pymupdf.open("pdf", doc.tobytes())
|
||||
assert p.load_link_uris(doc) == ["https://a.de/x"]
|
||||
|
||||
|
||||
def test_link_urls_tolerates_printed_variants():
|
||||
uris = [
|
||||
"https://a.de/artikel?utm_source=chatgpt.com",
|
||||
"https://b.de/news/Gesetz-Kritik-3718401.html",
|
||||
"https://c.de/seite",
|
||||
]
|
||||
md = "Siehe https://a.de/artikel/, https://b.de/news/ Gesetz-Kritik-3718401 und https://c.de/seite/.\n"
|
||||
assert p.link_urls(md, uris) == (
|
||||
"Siehe <https://a.de/artikel>, <https://b.de/news/Gesetz-Kritik-3718401.html> und "
|
||||
"<https://c.de/seite>.\n"
|
||||
)
|
||||
|
||||
|
||||
def test_link_urls_joins_printed_continuation_without_pdf_target():
|
||||
md = (
|
||||
"Siehe https://a.org/digital_ dispatches/towards-x/. und https://b.de/ "
|
||||
"Docs/artikel. html. Oder https://c.de/ und mehr.\n"
|
||||
)
|
||||
assert p.link_urls(md) == (
|
||||
"Siehe <https://a.org/digital_dispatches/towards-x/>. und "
|
||||
"<https://b.de/Docs/artikel.html>. Oder <https://c.de/> und mehr.\n"
|
||||
)
|
||||
|
|
|
|||
Loading…
Reference in a new issue