From 7a7dafd8502807ad93b32a3479aecc20eb9bb917 Mon Sep 17 00:00:00 2001 From: Jakub Werner Date: Fri, 27 Nov 2026 00:13:17 +0100 Subject: [PATCH] Read a typographic hyphen in a PDF link target as "-" Some publishers' PDFs write a link annotation's target with the hyphen U+2010 where the address has "-". repair_links_from_targets compares a printed link with the targets after squeezing out hyphens, so the printed "https://doi.org/10.13020/1f35-rn20" matched the target "https://doi.org/10.13020/1f35%E2%80%90rn20" and took its spelling: the url table's href did not resolve, and the printed address became link_text. _target now reads U+2010 and U+2011 in a target as "-", as the character or percent-encoded, and drops a soft hyphen. The printed link is then the target itself and is left alone. A dash or minus sign is kept, since an address can mean one. The same reading applies to the targets of the annotations on a table. On the 24 BES papers of the link comparison (--fast, CPU) this changes four url rows of 10.1111/2041-210X.13821 and nothing else. --- CHANGELOG.md | 9 ++++ bibr/structure/table_links.py | 17 ++++++- tests/test_table_links.py | 84 +++++++++++++++++++++++++++++++++++ 3 files changed, 108 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f1ead076..e1bee90f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -106,6 +106,15 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/). `href` is then the annotation's target and `link_text` the printed form. It happens only when exactly one of the PDF's link targets matches, and for a cut address only when the sentence goes on to print the rest. +- A typographic hyphen in a PDF's link target no longer ends up in `href`. Some + publishers write the target with the hyphen U+2010 where the address has + "-", so the DOI printed as "10.13020/1f35-rn20" came out as + "https://doi.org/10.13020/1f35%E2%80%90rn20", which does not resolve, with + the printed address as `link_text`. A hyphen (U+2010) or non-breaking hyphen + (U+2011) in a link target, as the character or percent-encoded, is now read + as "-", and a soft hyphen is dropped. `href` is then the address as printed + and `link_text` is null, as for any link whose target is what the page + prints. A dash in a target is kept: an address can mean one. - Two URLs printed with no space between them ("…/repo):https://doi.org/10.5281/…") are two `url` entries, in PDF, HTML, DOCX and JATS input. They were one entry with both addresses in its `href`. diff --git a/bibr/structure/table_links.py b/bibr/structure/table_links.py index 9ee4c617..4de04dbb 100644 --- a/bibr/structure/table_links.py +++ b/bibr/structure/table_links.py @@ -48,16 +48,29 @@ _BREAK_CHARS_RE = re.compile(r"[\s\-_­‐‑]+") # A link shorter than this, squeezed, is too short to be told apart by spelling. _MIN_SQUEEZED = 12 +# Hyphens a typesetter sets and no web address holds, as the character or +# percent-encoded: the hyphen U+2010 and the non-breaking hyphen U+2011, which +# stand for "-", and the soft hyphen U+00AD, which stands for nothing. A PDF +# writer that builds the target from the printed link copies them +# ("doi.org/10.13020/1f35%E2%80%90rn20"), and that target resolves nowhere. A +# dash (U+2012 to U+2015) or minus sign is left as it is: an address can mean +# one ("/wiki/Michelson%E2%80%93Morley_experiment"). +_TYPESET_HYPHEN_RE = re.compile(r"[\u2010\u2011]|%E2%80%9[01]", re.IGNORECASE) +_SOFT_HYPHEN_RE = re.compile(r"\u00ad|%C2%AD", re.IGNORECASE) def _target(uri: object) -> str | None: - """An http(s) link annotation's target, or None for any other annotation.""" + """An http(s) link annotation's target, or None for any other annotation. + + A typeset hyphen in it is read as "-" and a soft hyphen dropped (see + ``_TYPESET_HYPHEN_RE``), so the target is the address the link means. + """ if not isinstance(uri, str): return None text = html.unescape(uri.strip()) if not _HTTP_RE.match(text) or any(ch.isspace() for ch in text): return None - return text + return _TYPESET_HYPHEN_RE.sub("-", _SOFT_HYPHEN_RE.sub("", text)) def _key(url: str) -> str: diff --git a/tests/test_table_links.py b/tests/test_table_links.py index 8a6fb593..d5afbc97 100644 --- a/tests/test_table_links.py +++ b/tests/test_table_links.py @@ -2,7 +2,10 @@ from __future__ import annotations +from urllib.parse import unquote + import pandas as pd +import pytest from bibr.paper_contents import ( PaperContents, @@ -214,6 +217,87 @@ def test_link_that_matches_a_target_is_left_alone(): ) +@pytest.mark.parametrize( + "uri", + [ + "https://doi.org/10.5555/1f35%E2%80%90rn20", # U+2010, as the PDF writer encoded it + "https://doi.org/10.5555/1f35%e2%80%90rn20", + "https://doi.org/10.5555/1f35\u2010rn20", + "https://doi.org/10.5555/1f35%E2%80%91rn20", # U+2011, non-breaking + "https://doi.org/10.5555/1f35\u2011rn20", + "https://doi.org/10.5555/1f35‐rn20", + "https://doi.org/10.5555/1f%C2%AD35-rn\u00ad20", # soft hyphens + ], +) +def test_typeset_hyphen_in_a_target_does_not_replace_the_printed_hyphen(uri): + printed = "https://doi.org/10.5555/1f35-rn20" + contents = _contents( + sentences=[_sentence(1, f"Data are at {printed}.")], + links=[_link(printed)], + uri_links=_uris(uri), + ) + + repair_links_from_targets(contents) + + (link,) = contents.links + assert (link.url, link.link_text) == (printed, None) + + +def test_repaired_link_takes_the_target_with_its_typeset_hyphens_as_ascii(): + # The line break added the hyphen in "long-itudinal"; the target's own two + # hyphens are U+2010 and U+2011, percent-encoded. + printed = "https://example.org/long-itudinal-study/wave-2" + contents = _contents( + sentences=[_sentence(1, f"See {printed} for the data.")], + links=[_link(printed)], + uri_links=_uris("https://example.org/longitudinal%E2%80%90study/wave%e2%80%912"), + ) + + repair_links_from_targets(contents) + + (link,) = contents.links + assert link.url == "https://example.org/longitudinal-study/wave-2" + assert link.link_text == printed + + +def test_typeset_hyphen_in_a_target_on_a_table_is_ascii_in_the_link(): + table = _table( + 1, [["Source"], ["Archive of the survey"]], page=2, bbox=(0.0, 0.0, 500.0, 500.0) + ) + uri_links = [ + { + "page": 2, + "bbox": [10, 10, 90, 20], + "uri": "https://example.org/data%E2%80%90set\u2011one", + } + ] + contents = _contents(tables=[table], uri_links=uri_links) + + add_table_links(contents) + + assert _table_links(contents) == [(1, "https://example.org/data-set-one")] + + +@pytest.mark.parametrize( + "dash", + ["%E2%80%92", "%E2%80%93", "\u2013", "%E2%80%94", "\u2014", "%E2%80%95", "%E2%88%92", "\u2212"], +) +def test_dash_in_a_target_is_kept(dash): + # An address can mean an en dash (a wiki page title); only hyphens are read as "-". + target = f"https://example.org/wiki/Alpha{dash}Beta_experiment/results" + printed = f"https://example.org/wiki/Alpha{unquote(dash)}Beta_experi-ment/results" + contents = _contents( + sentences=[_sentence(1, f"See {printed} for an account.")], + links=[_link(printed)], + uri_links=_uris(target), + ) + + repair_links_from_targets(contents) + + (link,) = contents.links + assert (link.url, link.link_text) == (target, printed) + + def test_shorter_link_is_not_completed_unless_the_sentence_prints_the_rest(): contents = _contents( sentences=[_sentence(1, "See https://github.com/org-name/repo and the wiki.")],