Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,20 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).

## [Unreleased]

### Fixed

- 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.

## [0.9.2] - 2026-11-24

A speed and links release. On a CUDA machine the default local plan gets
Expand Down
17 changes: 15 additions & 2 deletions bibr/structure/table_links.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
84 changes: 84 additions & 0 deletions tests/test_table_links.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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.")],
Expand Down