Skip to content
Merged
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
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "hatchling.build"

[project]
name = "polytypo"
version = "1.0.1"
version = "1.1.0"
description = "Normalize typography across languages: locale-correct quotes, dashes, ellipses and no-break spaces."
readme = "README.md"
license = "MIT"
Expand Down
75 changes: 36 additions & 39 deletions src/polytypo/_engine/rules/_quote_ambiguity.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
"""Shared ambiguous-medial-span predicate (quotes.md 3.2 "General ambiguous-medial-span veto"
and "Listed elision veto"), consumed identically by `quotes` and `apostrophe` so the two rules
cannot drift apart on what counts as ambiguous (mirrors polytypo-js's
src/rules/quote-ambiguity.ts)."""
"""The two decline-only predicates `quotes` reads (quotes.md 3.2, "Listed elision veto" and
"Universal medial-`n` elision veto"). Both have the same outcome: the marks survive pass 2
unmatched and `apostrophe` converts each by its own case ladder, giving `rock 'n' roll` ->
`rock ’n’ roll`. Only `quotes` consumes this module -- spec 0.5.0 had `apostrophe` consume it
too, through a preserve set withdrawn in 1.1.0 (apostrophe.md 3.4). Mirrors polytypo-js's
src/rules/quote-ambiguity.ts."""

from __future__ import annotations

Expand All @@ -10,9 +12,14 @@
from polytypo._engine.sentinels import NONE
from polytypo._engine.unicode import is_letter

NARROW_APOSTROPHE = 0x27 # U+0027 -- the only glyph the GENERAL ambiguous-shape veto considers
NARROW = frozenset({0x27, 0x2018, 0x2019, 0x201A, 0x201B, 0x2039, 0x203A}) # the LISTED elision
# veto's own trigger class -- ANY NARROW mark, curly or straight (unlike the general veto above)
NARROW = frozenset({0x27, 0x2018, 0x2019, 0x201A, 0x201B, 0x2039, 0x203A}) # both vetoes' trigger
# class -- ANY NARROW mark, curly or straight. For the universal medial-n veto this is an
# IDEMPOTENCY obligation, not a preference: its marks are converted to U+2019 by `apostrophe`, so
# a straight-ASCII-only predicate would not recognise its own output and pass 2 would pair
# `rock ’n’ roll` as an ordinary NARROW quotation on the next run -- measured as `rock «n» roll`
# in ru and `rock ”n” roll` in fi.
LOWER_N = 0x6E # quotes.md 3.2 -- the one code point the universal veto's span may enclose,
UPPER_N = 0x4E # in either case
DIGIT = frozenset(range(0x30, 0x3A))
INLINE_SPACE = frozenset({0x20, 0x09, 0xA0, 0x202F, 0x2007, 0x2009, 0x200A})

Expand Down Expand Up @@ -72,17 +79,12 @@ def _word_matches(cp: list[int], start: int, end: int, pattern: str) -> bool:
return True


def compute_ambiguous_indices(
cp: list[int], locale_data: dict[str, Any]
) -> tuple[set[int], set[int]]:
"""Returns (veto_indices, preserve_indices).
def compute_ambiguous_indices(cp: list[int], locale_data: dict[str, Any]) -> set[int]:
"""Every index that must have both quote capabilities forced false in `quotes`' own pass 1 --
the union of the listed-idiom matches and the universal medial-n matches.

veto_indices: every index that must have both quote capabilities forced false in `quotes`'
own pass 1 -- the union of listed-idiom matches and the general ambiguous-shape matches.

preserve_indices: veto_indices MINUS the listed-idiom matches -- positions `apostrophe` must
skip entirely rather than curling via its own case ladder (general-shape-but-uncited
positions only; idiom-matched positions get apostrophe's normal elision treatment).
The union is computed rather than assumed: an idiom's `elided` field is not required to be
the single `n` the universal veto matches.
"""
n = len(cp)
idiom_indices: set[int] = set()
Expand Down Expand Up @@ -119,25 +121,20 @@ def compute_ambiguous_indices(
idiom_indices.add(i)
idiom_indices.add(j)

general_indices: set[int] = set()
i = 0
while i < n:
if cp[i] == NARROW_APOSTROPHE:
for enclosed_len in (1, 2, 3):
j = i + 1 + enclosed_len
if j >= n or cp[j] != NARROW_APOSTROPHE:
continue
enclosed = cp[i + 1 : i + 1 + enclosed_len]
if not all(is_letter(c) for c in enclosed):
continue
if _at(cp, i - 1) not in INLINE_SPACE:
continue
if _at(cp, j + 1) not in INLINE_SPACE:
continue
general_indices.add(i)
general_indices.add(j)
i += 1

veto_indices = idiom_indices | general_indices
preserve_indices = general_indices - idiom_indices
return veto_indices, preserve_indices
medial_n_indices: set[int] = set()
for i in range(n):
if cp[i] not in NARROW:
continue
if _at(cp, i + 1) not in (LOWER_N, UPPER_N):
continue
j = i + 2
if _at(cp, j) not in NARROW:
continue
if _at(cp, i - 1) not in INLINE_SPACE:
continue
if _at(cp, j + 1) not in INLINE_SPACE:
continue
medial_n_indices.add(i)
medial_n_indices.add(j)

return idiom_indices | medial_n_indices
11 changes: 5 additions & 6 deletions src/polytypo/_engine/rules/apostrophe.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
"""spec/rules/apostrophe.md -- order 50, default on. Locale data: quotes.elisionIdioms
(indirectly, via the shared ambiguity predicate's preserve set)."""
"""spec/rules/apostrophe.md -- order 50, default on. Reads no locale data and skips no position
as of spec 1.1.0: 0.5.0's preserve set (apostrophe.md 3.4) existed to stop the case ladder from
converting the marks `quotes` had vetoed, and conversion is now the specified outcome for exactly
those marks -- cases 4 and 3 are what turn `rock 'n' roll` into `rock ’n’ roll`."""

from __future__ import annotations

from typing import Any

from polytypo._engine.edits import Edit
from polytypo._engine.registry import RuleContext
from polytypo._engine.rules import _quote_ambiguity
from polytypo._engine.sentinels import LINE_MARKER, MARKER, NONE
from polytypo._engine.unicode import is_letter

Expand Down Expand Up @@ -75,11 +76,9 @@ def _is_alnum(v: int) -> bool:


def scan(cp: list[int], locale_data: dict[str, Any], ctx: RuleContext) -> list[Edit]:
_veto, preserve = _quote_ambiguity.compute_ambiguous_indices(cp, locale_data)

edits: list[Edit] = []
for i, g in enumerate(cp):
if g != SQ or i in preserve:
if g != SQ:
continue
left = _at(cp, i - 1)
right = _at(cp, i + 1)
Expand Down
2 changes: 1 addition & 1 deletion src/polytypo/_engine/rules/quotes.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def _skip_right(cp: list[int], i: int) -> int:

def _compute_candidates(cp: list[int], locale_data: dict[str, Any]) -> list[Candidate]:
space_right, space_left = _locale_skip_sets(locale_data)
veto_indices, _preserve = _quote_ambiguity.compute_ambiguous_indices(cp, locale_data)
veto_indices = _quote_ambiguity.compute_ambiguous_indices(cp, locale_data)

candidates: list[Candidate] = []
for i, g in enumerate(cp):
Expand Down
2 changes: 1 addition & 1 deletion vendor/polytypo-spec/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.0.0
1.1.0
6 changes: 3 additions & 3 deletions vendor/polytypo-spec/fixtures/de-CH.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"spec": "1.0.0",
"spec": "1.1.0",
"locale": "de-CH",
"cases": [
{
Expand Down Expand Up @@ -494,8 +494,8 @@
"rule": "quotes",
"mode": "text",
"in": "rock 'n' roll",
"out": "rock 'n' roll",
"note": "spec 0.5.0: de-CH has no cited quotes.elisionIdioms entry for this idiom (quotes.md §7 item 8 — only en-US does, via Chicago Manual of Style Online and the American Heritage Dictionary). The general ambiguous-medial-span veto (quotes.md 3.2) preserves both marks as literal U+0027 rather than pairing them as an ordinary quotation, closing the false positive AUDIT_REMEDIATION_AND_RELEASE_PLAN.md 3.1 named for \"equivalent cases in several locales.\""
"out": "rock ’n’ roll",
"note": "spec 1.1.0, quotes.md §3.2's universal medial-n elision veto: by operator decision the idiom is international, so `quotes` declines the pairing in every locale rather than only where a cited quotes.elisionIdioms entry exists, and `apostrophe` (order 50) converts each mark by its own case ladder — leading elision (apostrophe.md §3.3 case 4), then trailing elision (case 3). Same output and same two case decisions as en-us-quotes-rock-n-roll, which reaches them through the cited idiom instead. History: spec 0.5.0's general ambiguous-medial-span veto preserved both marks here as literal U+0027; before 0.5.0 `quotes` paired them as an ordinary depth-1 quotation in this locale's primary glyphs."
}
]
}
6 changes: 3 additions & 3 deletions vendor/polytypo-spec/fixtures/de-DE.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"spec": "1.0.0",
"spec": "1.1.0",
"locale": "de-DE",
"cases": [
{
Expand Down Expand Up @@ -540,8 +540,8 @@
"rule": "quotes",
"mode": "text",
"in": "rock 'n' roll",
"out": "rock 'n' roll",
"note": "spec 0.5.0: de-DE has no cited quotes.elisionIdioms entry for this idiom (quotes.md §7 item 8 — only en-US does, via Chicago Manual of Style Online and the American Heritage Dictionary). The general ambiguous-medial-span veto (quotes.md 3.2) preserves both marks as literal U+0027 rather than pairing them as an ordinary quotation, closing the false positive AUDIT_REMEDIATION_AND_RELEASE_PLAN.md 3.1 named for \"equivalent cases in several locales.\""
"out": "rock ’n’ roll",
"note": "spec 1.1.0, quotes.md §3.2's universal medial-n elision veto: by operator decision the idiom is international, so `quotes` declines the pairing in every locale rather than only where a cited quotes.elisionIdioms entry exists, and `apostrophe` (order 50) converts each mark by its own case ladder — leading elision (apostrophe.md §3.3 case 4), then trailing elision (case 3). Same output and same two case decisions as en-us-quotes-rock-n-roll, which reaches them through the cited idiom instead. History: spec 0.5.0's general ambiguous-medial-span veto preserved both marks here as literal U+0027; before 0.5.0 `quotes` paired them as an ordinary depth-1 quotation in this locale's primary glyphs."
}
]
}
14 changes: 7 additions & 7 deletions vendor/polytypo-spec/fixtures/el.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"spec": "1.0.0",
"spec": "1.1.0",
"locale": "el",
"cases": [
{
Expand Down Expand Up @@ -133,8 +133,8 @@
"rule": "quotes",
"mode": "text",
"in": "\"Είπε 'όχι' σε μένα\", σημείωσε.",
"out": "«Είπε 'όχι' σε μένα», σημείωσε.",
"note": "quotes.md §6: one stack, inner pair at depth 2 takes the secondary pair U+201C/U+201D. Greek combines guillemets with curly double quotes, which no other v1 locale does. spec 0.5.0: the inner 'όχι' (3 Greek letters, space-flanked) is preserved by the general ambiguous-medial-span veto — el has no elisionIdioms entry for this shape."
"out": "«Είπε “όχι” σε μένα», σημείωσε.",
"note": "quotes.md §6: one stack, inner pair at depth 2 takes the secondary pair U+201C/U+201D. Greek combines guillemets with curly double quotes, which no other v1 locale does. spec 1.1.0 restores the nesting this case was written for: 0.5.0's general ambiguous-medial-span veto had preserved the inner 'όχι' as literal U+0027 because it was 3 letters, space-flanked; the veto that replaced it matches only a medial n (quotes.md §3.2), so an ordinary short quotation pairs again."
},
{
"id": "el-quotes-already-guillemets",
Expand Down Expand Up @@ -205,8 +205,8 @@
"rule": "quotes",
"mode": "text",
"in": "\"Είπε 'όχι' σε μένα\", σημείωσε.",
"out": "«Είπε 'όχι' σε μένα», σημείωσε.",
"note": "quotes.md §6 row 16: width drift with no gate intervention — el's secondary pair is WIDE (“ ”), so the inner NARROW pair drifts to WIDE on render and all four marks land on one stack in the gate's re-scan; the certification gate certifies without declining anything, evidence the gate's cost is near zero for the common case. spec 0.5.0: same as el-quotes-nested — 'όχι' is preserved by the general ambiguous-medial-span veto rather than pairing as an ordinary same-width quotation."
"out": "«Είπε “όχι” σε μένα», σημείωσε.",
"note": "quotes.md §6 row 16: width drift with no gate intervention — el's secondary pair is WIDE (“ ”), so the inner NARROW pair drifts to WIDE on render and all four marks land on one stack in the gate's re-scan; the certification gate certifies without declining anything, evidence the gate's cost is near zero for the common case. spec 1.1.0: same as el-quotes-nested — the width-drift path this case exists to exercise is reachable again now that a 3-letter medial span is no longer vetoed (quotes.md §3.2). Under 0.5.0 the inner marks never paired, so nothing drifted and the case proved nothing about the gate."
},
{
"id": "el-quotes-030-adversarial-interleaved",
Expand All @@ -221,8 +221,8 @@
"rule": "quotes",
"mode": "text",
"in": "rock 'n' roll",
"out": "rock 'n' roll",
"note": "spec 0.5.0: el has no cited quotes.elisionIdioms entry for this idiom (quotes.md §7 item 8 — only en-US does, via Chicago Manual of Style Online and the American Heritage Dictionary). The general ambiguous-medial-span veto (quotes.md 3.2) preserves both marks as literal U+0027 rather than pairing them as an ordinary quotation, closing the false positive AUDIT_REMEDIATION_AND_RELEASE_PLAN.md 3.1 named for \"equivalent cases in several locales.\""
"out": "rock ’n’ roll",
"note": "spec 1.1.0, quotes.md §3.2's universal medial-n elision veto: by operator decision the idiom is international, so `quotes` declines the pairing in every locale rather than only where a cited quotes.elisionIdioms entry exists, and `apostrophe` (order 50) converts each mark by its own case ladder — leading elision (apostrophe.md §3.3 case 4), then trailing elision (case 3). Same output and same two case decisions as en-us-quotes-rock-n-roll, which reaches them through the cited idiom instead. History: spec 0.5.0's general ambiguous-medial-span veto preserved both marks here as literal U+0027; before 0.5.0 `quotes` paired them as an ordinary depth-1 quotation in this locale's primary glyphs."
},
{
"id": "el-ranges-none",
Expand Down
10 changes: 5 additions & 5 deletions vendor/polytypo-spec/fixtures/en-GB.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"spec": "1.0.0",
"spec": "1.1.0",
"locale": "en-GB",
"cases": [
{
Expand Down Expand Up @@ -632,8 +632,8 @@
"rule": "quotes",
"mode": "text",
"in": "\"He said 'no' to me,\" she noted.",
"out": "‘He said 'no' to me,’ she noted.",
"note": "quotes.md §6 case 2, inverted: depth 1 → single, depth 2 → double. Compare en-US-quotes-nested, same input. The inner 'no' (2 letters, space-flanked, no matching elisionIdioms entry — en-GB has none) is preserved by the general ambiguous-medial-span veto (quotes.md §3.2) rather than paired as a nested quotation: a documented false negative, not an unresolved gap (History: this was open per quotes.md §7 item 8 through spec 0.4.1, closed by the general veto in spec 0.5.0)."
"out": "‘He said “no” to me,’ she noted.",
"note": "quotes.md §6 case 2, inverted: depth 1 → single, depth 2 → double. Compare en-us-quotes-nested, same input. spec 1.1.0: the inner 'no' pairs as a nested quotation again — 0.5.0's general ambiguous-medial-span veto had preserved it as literal U+0027, and the universal medial-n veto that replaced it does not match a two-letter span (quotes.md §3.2)."
},
{
"id": "en-gb-quotes-nested-inverted-input",
Expand Down Expand Up @@ -712,8 +712,8 @@
"rule": "quotes",
"mode": "text",
"in": "rock 'n' roll",
"out": "rock 'n' roll",
"note": "quotes.md §7.1. The output is accidentally right here — not because a secondary level was selected, but because en-GB declares the single pair as PRIMARY and the pair is at depth 1. spec 0.5.0: en-GB has no cited elisionIdioms entry for this idiom (quotes.md §7 item 8 — elevated ambiguity risk, since en-GB's primary pair is itself the single quote). The general ambiguous-medial-span veto now preserves the marks as literal U+0027 instead of pairing them as an ordinary quotation, which is the pre-0.5.0 behaviour this fixture used to assert. AUDIT_REMEDIATION_AND_RELEASE_PLAN.md §3.1 named exactly this locale as an affected case."
"out": "rock ’n’ roll",
"note": "spec 1.1.0, quotes.md §3.2's universal medial-n elision veto: by operator decision the idiom is international, so `quotes` declines the pairing in every locale rather than only where a cited quotes.elisionIdioms entry exists, and `apostrophe` (order 50) converts each mark by its own case ladder — leading elision (apostrophe.md §3.3 case 4), then trailing elision (case 3). Same output and same two case decisions as en-us-quotes-rock-n-roll, which reaches them through the cited idiom instead. History: spec 0.5.0's general ambiguous-medial-span veto preserved both marks here as literal U+0027; before 0.5.0 `quotes` paired them as an ordinary depth-1 quotation in this locale's primary glyphs. en-GB is the locale quotes.md §7 item 8 flags as carrying the highest genuine-dialogue collision risk, because its primary pair is itself the single quote — that risk is why no elisionIdioms entry was ever cited here, and 1.1.0's veto deliberately does not depend on one."
},
{
"id": "en-gb-quotes-elision-inside-quotation",
Expand Down
Loading