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.1.0"
version = "1.2.0"
description = "Normalize typography across languages: locale-correct quotes, dashes, ellipses and no-break spaces."
readme = "README.md"
license = "MIT"
Expand Down
9 changes: 8 additions & 1 deletion src/polytypo/_engine/rules/apostrophe.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
"""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`."""
those marks -- cases 4 and 3 are what turn `rock 'n' roll` into `rock ’n’ roll`. Case 3a (spec
1.2.0) reads a fixed OPENQUOTE set, not locale data."""

from __future__ import annotations

Expand Down Expand Up @@ -63,6 +64,9 @@
0x2014,
}
)
# The quotation glyphs of OPENISH, without its brackets, dashes and MARKER (apostrophe.md 3.1,
# spec 1.2.0): `f'(x)` must stay a prime, and a letter + U+0027 + dash is already case 3.
OPENQUOTE = frozenset({0xAB, 0x2018, 0x201A, 0x201B, 0x201C, 0x201E, 0x201F, 0x2039})


def _at(cp: list[int], i: int) -> int:
Expand Down Expand Up @@ -95,6 +99,9 @@ def scan(cp: list[int], locale_data: dict[str, Any], ctx: RuleContext) -> list[E
):
edits.append(Edit(i, i + 1, [0x2019], RULE_ID)) # case 3: trailing
continue
if left != NONE and is_letter(left) and right in OPENQUOTE:
edits.append(Edit(i, i + 1, [0x2019], RULE_ID)) # case 3a: before a quotation
continue
if (left == NONE or left in SPACELIKE or left in OPENISH) and _is_alnum(right):
edits.append(Edit(i, i + 1, [0x2019], RULE_ID)) # case 4: leading
continue
Expand Down
14 changes: 7 additions & 7 deletions src/polytypo/_engine/rules/nbsp.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from polytypo._engine.edits import Edit
from polytypo._engine.registry import RuleContext
from polytypo._engine.sentinels import LINE_MARKER, NONE
from polytypo._engine.sentinels import LINE_MARKER, MARKER, NONE
from polytypo._engine.unicode import is_letter, is_upper, simple_uppercase
from polytypo.errors import POLYTYPO_MALFORMED_LOCALE_DATA, PolytypoError

Expand All @@ -35,9 +35,9 @@
0x3000,
}
)
# Includes LINE_MARKER: a member of BREAK for every rule everywhere (modes.md 3.2). nbsp's own
# OPENISH/CLOSEISH (brackets + locale quote glyphs, see _openish_closeish) do NOT get MARKER --
# unlike quotes/apostrophe, matching the reference implementation exactly.
# Includes LINE_MARKER: a member of BREAK for every rule everywhere (modes.md 3.2). As of spec
# 1.2.0 the inline MARKER is a member of nbsp's own CLOSEISH (read only by N1/N2's right-context
# guard) and NOT of its OPENISH (nbsp.md 3.1, 7 item 12; modes.md 3.3) -- see _openish_closeish.
BREAK = frozenset({0x0A, 0x0D, 0x0B, 0x0C, 0x85, 0x2028, 0x2029, LINE_MARKER})
SPACELIKE = frozenset({SP, 0x09}) | NOBREAK | OTHER_SPACE | BREAK
DIGIT = frozenset(range(0x30, 0x3A))
Expand All @@ -59,7 +59,7 @@ def _is_alnum(v: int) -> bool:
def _openish_closeish(locale_data: dict[str, Any]) -> tuple[frozenset[int], frozenset[int]]:
quotes = locale_data["quotes"]
openish = {0x28, 0x5B, 0x7B}
closeish = {0x29, 0x5D, 0x7D}
closeish = {MARKER, 0x29, 0x5D, 0x7D}
for pair in (quotes["primary"], quotes["secondary"]):
openish.add(ord(pair["open"]))
closeish.add(ord(pair["close"]))
Expand Down Expand Up @@ -124,8 +124,8 @@ def _before_punctuation_common(
prev = _at(cp, i - 1)
if prev in target_set or prev in other_set:
continue
# Right-context guard. nbsp.md's own CLOSEISH (brackets + locale close glyphs), NOT
# quotes.md's broader CLOSEISH -- the two documents define the class name differently.
# Right-context guard. nbsp.md's own CLOSEISH (brackets + locale close glyphs + MARKER),
# NOT quotes.md's broader CLOSEISH -- the two documents define the class name differently.
after = _at(cp, i + 1)
if (
after != NONE
Expand Down
9 changes: 7 additions & 2 deletions src/polytypo/_engine/rules/spaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,9 @@ def scan(cp: list[int], locale_data: dict[str, Any], ctx: RuleContext) -> list[E

def _lone_dot_condition_holds(cp: list[int], e: int, right: int) -> bool:
"""3.4: if right is U+002E, replacement length may be 0 only if the maximal DOTLIKE run
starting at e has length exactly 1. Any other STRIP-BEFORE member is unaffected."""
starting at e has length exactly 1 and cp[e+1] is neither a LETTER nor an ASCII digit (the
word-start clause, spec 1.2.0: `.NET`, `.5`). A span boundary marker after the dot is neither,
so it still strips. Any other STRIP-BEFORE member is unaffected."""
if right != 0x2E:
return True
j = e
Expand All @@ -121,4 +123,7 @@ def _lone_dot_condition_holds(cp: list[int], e: int, right: int) -> bool:
while j < n and cp[j] in DOTLIKE:
run_len += 1
j += 1
return run_len == 1
if run_len != 1:
return False
after = _at(cp, e + 1)
return after == NONE or not (is_letter(after) or _is_ascii_digit(after))
9 changes: 9 additions & 0 deletions tests/test_rules/test_apostrophe.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,12 @@ def test_prime_guard_leaves_feet_and_inches_alone() -> None:

def test_case_5_nothing_inferable_is_left_alone() -> None:
assert polytypo.transform("a ' b", locale="en-US") == "a ' b"


def test_case_3a_elision_before_opening_quotation_glyph() -> None:
# apostrophe.md 3.3 case 3a (spec 1.2.0).
assert polytypo.transform("l'“idea”", locale="en-US") == "l’“idea”"


def test_case_3a_excludes_brackets() -> None:
assert polytypo.transform("f'(x) = 2", locale="en-US") == "f'(x) = 2"
7 changes: 7 additions & 0 deletions tests/test_rules/test_nbsp.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from __future__ import annotations

import polytypo
import polytypo.html

NBSP = " "
NNBSP = " "
Expand All @@ -27,3 +28,9 @@ def test_n5_before_units() -> None:

def test_n3_after_short_words_does_not_fire_mid_abbreviation() -> None:
assert polytypo.transform("p. 12", locale="fr") == "p. 12"


def test_n1_right_context_accepts_span_boundary_marker() -> None:
# nbsp.md 3.3 step 2 (spec 1.2.0): the inline marker is in nbsp's CLOSEISH.
out = polytypo.html.transform("<strong>gel :</strong> il", locale="fr")
assert out == f"<strong>gel{NBSP}:</strong> il"
6 changes: 6 additions & 0 deletions tests/test_rules/test_spaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,9 @@ def test_empty_bracket_guard_collapses_but_does_not_delete() -> None:

def test_open_bracket_strips_trailing_space() -> None:
assert polytypo.transform("( a)", locale="en-US") == "(a)"


def test_word_start_clause_keeps_space_before_dot_initial_token() -> None:
# spaces.md 3.4 (spec 1.2.0): a dot followed by a letter or digit starts a word.
assert polytypo.transform("Use .NET, .NET Core", locale="en-US") == "Use .NET, .NET Core"
assert polytypo.transform("a .5 b", locale="en-US") == "a .5 b"
2 changes: 1 addition & 1 deletion vendor/polytypo-spec/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.1.0
1.2.0
10 changes: 9 additions & 1 deletion vendor/polytypo-spec/fixtures/de-CH.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"spec": "1.1.0",
"spec": "1.2.0",
"locale": "de-CH",
"cases": [
{
Expand Down Expand Up @@ -496,6 +496,14 @@
"in": "rock 'n' roll",
"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": "de-ch-apostrophe-elision-before-guillemet",
"rule": "apostrophe",
"mode": "text",
"in": "die Zeitung l'\"Express\"",
"out": "die Zeitung l’«Express»",
"note": "apostrophe.md §3.3 case 3a (spec 1.2.0): `quotes` gives de-CH's « », and the elision before « converts."
}
]
}
34 changes: 33 additions & 1 deletion vendor/polytypo-spec/fixtures/de-DE.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"spec": "1.1.0",
"spec": "1.2.0",
"locale": "de-DE",
"cases": [
{
Expand Down Expand Up @@ -542,6 +542,38 @@
"in": "rock 'n' roll",
"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": "de-de-spaces-dot-word-start-dotfile",
"rule": "spaces",
"mode": "text",
"in": "Die Datei .gitignore ist leer .",
"out": "Die Datei .gitignore ist leer.",
"note": "spaces.md §3.4 word-start clause (spec 1.2.0): the space before `.gitignore` survives because a letter follows the dot; the space before the final dot is deleted because nothing follows it."
},
{
"id": "de-de-apostrophe-possessive-before-closing-quote",
"rule": "apostrophe",
"mode": "text",
"in": "Er sagte „Hans'“ und ging.",
"out": "Er sagte „Hans’“ und ging.",
"note": "apostrophe.md §3.3 case 3a (spec 1.2.0), §6 row 17: U+201C closes a de-DE quotation but is not in CLOSEISH, so case 3 missed the possessive; case 3a reaches the same U+2019 without reading locale data."
},
{
"id": "de-de-apostrophe-closing-single-before-closing-double-not-elision",
"rule": "apostrophe",
"mode": "text",
"in": "\"Er sagte 'Hans'\" und ging.",
"out": "„Er sagte ‚Hans‘“ und ging.",
"note": "apostrophe.md §3.3 case 3a (spec 1.2.0), §7 item 7: in de-DE the closing double quote U+201C is in OPENQUOTE, so a closing single quote directly before it has exactly case 3a's shape. `quotes` (order 40) pairs both levels first, so no U+0027 reaches this rule and the nested quotation keeps its de-DE glyphs."
},
{
"id": "de-de-apostrophe-mixed-curly-and-straight-closing-single",
"rule": "apostrophe",
"mode": "text",
"in": "„Er sagte ‚Hans'“ und ging.",
"out": "„Er sagte ‚Hans‘“ und ging.",
"note": "apostrophe.md §3.3 case 3a (spec 1.2.0): the same shape with the outer pair and the opening single quote already curly. `quotes` still pairs the straight closing single quote with ‚, so case 3a never sees it."
}
]
}
10 changes: 9 additions & 1 deletion vendor/polytypo-spec/fixtures/el.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"spec": "1.1.0",
"spec": "1.2.0",
"locale": "el",
"cases": [
{
Expand Down Expand Up @@ -234,6 +234,14 @@
"ranges": true
},
"note": "spec 0.5.0: el's dash.range is \"none\" (no verified convention), so `ranges` substitutes nothing even when explicitly enabled (ranges.md §2, §3.3) — the same invariant the pre-0.5.0 `dashes`-tagged no-op fixtures for this locale already proved for the combined rule."
},
{
"id": "el-apostrophe-elision-before-guillemet",
"rule": "apostrophe",
"mode": "text",
"in": "η εφημερίδα l'\"Humanité\"",
"out": "η εφημερίδα l’«Humanité»",
"note": "apostrophe.md §3.3 case 3a (spec 1.2.0): `quotes` gives el's « », and the elision before « converts."
}
]
}
18 changes: 17 additions & 1 deletion vendor/polytypo-spec/fixtures/en-GB.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"spec": "1.1.0",
"spec": "1.2.0",
"locale": "en-GB",
"cases": [
{
Expand Down Expand Up @@ -1269,6 +1269,22 @@
"in": "'90s were fun,' he said",
"out": "‘90s were fun,’ he said",
"note": "quotes.md §6 row E2s: the straight-input control, confirming the corruption in E2 predates mandate 1."
},
{
"id": "en-gb-spaces-dot-word-start-file-extensions",
"rule": "spaces",
"mode": "text",
"in": "CAD files (.DWG, .STEP) on request",
"out": "CAD files (.DWG, .STEP) on request",
"note": "spaces.md §3.4 word-start clause (spec 1.2.0), §6 row 10g: `.STEP` starts a token, so the space after the comma survives. Spec 1.1.0 produced `(.DWG,.STEP)`."
},
{
"id": "en-gb-apostrophe-elision-before-curly-quote",
"rule": "apostrophe",
"mode": "text",
"in": "dell'“arte” moderna",
"out": "dell’‘arte’ moderna",
"note": "apostrophe.md §3.3 case 3a (spec 1.2.0), §6 row 16: `quotes` re-typesets the curly pair as en-GB's primary single quotes, and the elision before the opening glyph converts to U+2019."
}
]
}
66 changes: 65 additions & 1 deletion vendor/polytypo-spec/fixtures/en-US.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"spec": "1.1.0",
"spec": "1.2.0",
"locale": "en-US",
"cases": [
{
Expand Down Expand Up @@ -1802,6 +1802,70 @@
"ranges": true
},
"note": "ranges.md §3.2, G3 — not part of a decimal or a path. `before` is U+002E (.) for the first candidate and U+002F (/) for the second; `after` is U+002F (/) for the second. This is currently the only canonical fixture exercising G3 — previously untested by any canonical case even before the spec 0.5.0 ownership correction (dashes.test.ts's engine-level 'must not touch' list covered it, but no fixture did)."
},
{
"id": "en-us-spaces-dot-word-start-dotnet",
"rule": "spaces",
"mode": "text",
"in": "Use .NET, .NET Core or .NET Framework",
"out": "Use .NET, .NET Core or .NET Framework",
"note": "spaces.md §3.4 word-start clause (spec 1.2.0), §6 row 10f: each dot is followed by a letter, so it starts a token and the space before it is not deleted. Spec 1.1.0 produced `Use.NET,.NET Core or.NET Framework`."
},
{
"id": "en-us-spaces-dot-word-start-decimal",
"rule": "spaces",
"mode": "text",
"in": "the values .5 and .9 apply",
"out": "the values .5 and .9 apply",
"note": "spaces.md §3.4 word-start clause (spec 1.2.0), §6 row 10h: an ASCII digit after the dot counts the same as a letter."
},
{
"id": "en-us-spaces-dot-word-start-misplaced-full-stop",
"rule": "spaces",
"mode": "text",
"in": "the end .Next sentence",
"out": "the end .Next sentence",
"note": "spaces.md §3.4, §6 row 10i: the accepted cost of the word-start clause. A misplaced full stop followed directly by a letter keeps its stray space, because deleting it would glue two words that a later pass cannot separate again."
},
{
"id": "en-us-spaces-dot-terminal-still-strips",
"rule": "spaces",
"mode": "text",
"in": "See p. 12 . Next",
"out": "See p. 12. Next",
"note": "spaces.md §3.4, §6 row 10j: a dot followed by a space is terminal punctuation, so the space before it is still deleted under spec 1.2.0."
},
{
"id": "en-us-nbsp-span-boundary-no-punctuation-data",
"rule": "nbsp",
"mode": "html",
"in": "<strong>Note :</strong> read this",
"out": "<strong>Note:</strong> read this",
"note": "nbsp.md §3.3 step 2 (spec 1.2.0): accepting the marker changes nothing in a locale whose punctuation lists are empty. `spaces` deletes the space and nothing is put back, the same result as in text mode."
},
{
"id": "en-us-apostrophe-elision-before-quote",
"rule": "apostrophe",
"mode": "text",
"in": "the magazine l'\"Uomo\" today",
"out": "the magazine l’“Uomo” today",
"note": "apostrophe.md §3.3 case 3a (spec 1.2.0): `quotes` converts the straight double quotes to U+201C/U+201D, and the elision before U+201C converts to U+2019."
},
{
"id": "en-us-apostrophe-prime-before-bracket-untouched",
"rule": "apostrophe",
"mode": "text",
"in": "f'(x) = 2x",
"out": "f'(x) = 2x",
"note": "apostrophe.md §3.3 case 3a (spec 1.2.0), §6 row 18: `(` is in OPENISH but not in OPENQUOTE, so a prime on a function name is left alone."
},
{
"id": "en-us-spaces-dot-before-span-boundary-is-terminal",
"rule": "spaces",
"mode": "html",
"in": "Use .<b>NET</b> today",
"out": "Use.<b>NET</b> today",
"note": "spaces.md §3.4 word-start clause (spec 1.2.0): the code point after the dot is the −1 span boundary marker, which is neither a LETTER nor an ASCII digit (modes.md §3.3), so the dot counts as terminal and the space before it is deleted."
}
]
}
10 changes: 9 additions & 1 deletion vendor/polytypo-spec/fixtures/fi.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"spec": "1.1.0",
"spec": "1.2.0",
"locale": "fi",
"cases": [
{
Expand Down Expand Up @@ -1309,6 +1309,14 @@
"in": "'90s were fun,' he said",
"out": "”90s were fun,” he said",
"note": "quotes.md §6 row E3s: the straight-input control for E3."
},
{
"id": "fi-apostrophe-elision-before-shared-quote-glyph",
"rule": "apostrophe",
"mode": "text",
"in": "lehti l'\"Humanité\"",
"out": "lehti l’”Humanité”",
"note": "apostrophe.md §3.3 cases 3 and 3a (spec 1.2.0): fi opens and closes with U+201D, which is in CLOSEISH, so case 3 already converted this before 1.2.0. Pinned so that case 3a is not read as changing a shared-glyph locale."
}
]
}
Loading