polytypo 1.20.1 → 1.22.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/polytypo/data/VERSION +1 -1
- data/lib/polytypo/data/fixtures/bg.json +23 -4
- data/lib/polytypo/data/fixtures/cs.json +20 -1
- data/lib/polytypo/data/fixtures/de-CH.json +23 -4
- data/lib/polytypo/data/fixtures/de-DE.json +124 -13
- data/lib/polytypo/data/fixtures/el.json +39 -4
- data/lib/polytypo/data/fixtures/en-GB.json +34 -7
- data/lib/polytypo/data/fixtures/en-US.json +988 -162
- data/lib/polytypo/data/fixtures/es.json +12 -1
- data/lib/polytypo/data/fixtures/fi.json +21 -2
- data/lib/polytypo/data/fixtures/fr-CA.json +20 -1
- data/lib/polytypo/data/fixtures/fr.json +57 -13
- data/lib/polytypo/data/fixtures/hr.json +20 -1
- data/lib/polytypo/data/fixtures/hu.json +39 -4
- data/lib/polytypo/data/fixtures/it.json +12 -1
- data/lib/polytypo/data/fixtures/locale-resolution.json +1 -1
- data/lib/polytypo/data/fixtures/lt.json +20 -1
- data/lib/polytypo/data/fixtures/nl.json +28 -1
- data/lib/polytypo/data/fixtures/pl.json +28 -1
- data/lib/polytypo/data/fixtures/pt-BR.json +20 -1
- data/lib/polytypo/data/fixtures/pt-PT.json +20 -1
- data/lib/polytypo/data/fixtures/ro.json +13 -2
- data/lib/polytypo/data/fixtures/ru.json +49 -6
- data/lib/polytypo/data/fixtures/sk.json +20 -1
- data/lib/polytypo/data/fixtures/sl.json +20 -1
- data/lib/polytypo/data/fixtures/sr.json +23 -4
- data/lib/polytypo/data/fixtures/sv.json +23 -4
- data/lib/polytypo/data/fixtures/tr.json +20 -1
- data/lib/polytypo/data/fixtures/uk.json +12 -1
- data/lib/polytypo/data/locales/bg.json +10 -1
- data/lib/polytypo/data/locales/cs.json +3 -0
- data/lib/polytypo/data/locales/de-CH.json +15 -1
- data/lib/polytypo/data/locales/de-DE.json +16 -1
- data/lib/polytypo/data/locales/el.json +10 -1
- data/lib/polytypo/data/locales/en-GB.json +16 -4
- data/lib/polytypo/data/locales/en-US.json +18 -11
- data/lib/polytypo/data/locales/es.json +3 -0
- data/lib/polytypo/data/locales/fi.json +9 -3
- data/lib/polytypo/data/locales/fr-CA.json +10 -1
- data/lib/polytypo/data/locales/fr.json +4 -1
- data/lib/polytypo/data/locales/hr.json +4 -1
- data/lib/polytypo/data/locales/hu.json +10 -1
- data/lib/polytypo/data/locales/it.json +3 -0
- data/lib/polytypo/data/locales/lt.json +13 -5
- data/lib/polytypo/data/locales/nl.json +11 -2
- data/lib/polytypo/data/locales/pl.json +11 -2
- data/lib/polytypo/data/locales/pt-BR.json +5 -2
- data/lib/polytypo/data/locales/pt-PT.json +6 -3
- data/lib/polytypo/data/locales/registry.json +1 -1
- data/lib/polytypo/data/locales/ro.json +3 -0
- data/lib/polytypo/data/locales/ru.json +10 -17
- data/lib/polytypo/data/locales/sk.json +8 -5
- data/lib/polytypo/data/locales/sl.json +4 -1
- data/lib/polytypo/data/locales/sr.json +10 -2
- data/lib/polytypo/data/locales/sv.json +9 -6
- data/lib/polytypo/data/locales/tr.json +5 -2
- data/lib/polytypo/data/locales/uk.json +3 -0
- data/lib/polytypo/data/rules/modes.md +2 -2
- data/lib/polytypo/data/rules/nbsp.md +271 -74
- data/lib/polytypo/data/rules/order.json +9 -1
- data/lib/polytypo/data/rules/pipeline-idempotency.md +76 -9
- data/lib/polytypo/data/rules/quote-punctuation.md +337 -0
- data/lib/polytypo/data/rules/quotes.md +23 -5
- data/lib/polytypo/data/rules/ranges.md +7 -1
- data/lib/polytypo/data/rules/spaces.md +4 -2
- data/lib/polytypo/data/rules/symbols.md +2 -1
- data/lib/polytypo/data/schema/fixtures.schema.json +1 -0
- data/lib/polytypo/data/schema/locale.schema.json +27 -2
- data/lib/polytypo/engine/locale.rb +22 -1
- data/lib/polytypo/engine/pipeline.rb +1 -1
- data/lib/polytypo/engine/rules/nbsp.rb +161 -11
- data/lib/polytypo/engine/rules/quote_punctuation.rb +79 -0
- data/lib/polytypo/engine/rules.rb +1 -0
- data/lib/polytypo/version.rb +1 -1
- metadata +3 -1
|
@@ -19,10 +19,20 @@
|
|
|
19
19
|
"quotes": {
|
|
20
20
|
"type": "object",
|
|
21
21
|
"additionalProperties": false,
|
|
22
|
-
"required": [
|
|
22
|
+
"required": [
|
|
23
|
+
"primary",
|
|
24
|
+
"secondary",
|
|
25
|
+
"punctuationPlacement",
|
|
26
|
+
"elisionIdioms",
|
|
27
|
+
"elisionClitics"
|
|
28
|
+
],
|
|
23
29
|
"properties": {
|
|
24
30
|
"primary": { "$ref": "#/$defs/quotePair" },
|
|
25
31
|
"secondary": { "$ref": "#/$defs/quotePair" },
|
|
32
|
+
"punctuationPlacement": {
|
|
33
|
+
"enum": ["inside", "none"],
|
|
34
|
+
"description": "Where a comma or full stop goes relative to the closing PRIMARY quotation mark, read only by the opt-in `quote-punctuation` rule (spec 1.21.0). `inside`: the locale's cited convention puts them before the mark, and the rule moves one that was typed after it. `none`: the rule does nothing, either because the locale places them by meaning (a mark that belongs to the quoted material goes inside, one that belongs to the surrounding sentence goes outside, which no scan of code points can decide) or because no convention has been verified; a missing citation is never a licence to guess. `inside` is constrained by scripts/validate-spec.mjs (quote-punctuation.md section 2): the primary close must be a WIDE glyph other than the primary open, `dash.parenthetical` must not be `em-paired`, and neither pair may carry an inner space."
|
|
35
|
+
},
|
|
26
36
|
"elisionIdioms": {
|
|
27
37
|
"type": "array",
|
|
28
38
|
"items": { "$ref": "#/$defs/elisionIdiom" },
|
|
@@ -87,6 +97,8 @@
|
|
|
87
97
|
"afterShortWords",
|
|
88
98
|
"abbreviations",
|
|
89
99
|
"beforeUnits",
|
|
100
|
+
"spacedUnits",
|
|
101
|
+
"closedUnits",
|
|
90
102
|
"beforeNumber",
|
|
91
103
|
"beforeWord",
|
|
92
104
|
"afterSymbols",
|
|
@@ -117,7 +129,19 @@
|
|
|
117
129
|
"type": "array",
|
|
118
130
|
"items": { "type": "string", "minLength": 1 },
|
|
119
131
|
"uniqueItems": true,
|
|
120
|
-
"description": "Units and signs that bind to a preceding number with U+00A0. Example: \"%\", \"km\", \"°C\"."
|
|
132
|
+
"description": "Units and signs that bind to a preceding number with U+00A0 when the author already typed a space. Conversion only: a missing space is never inserted and an existing one is never removed. Use for a unit whose spacing the locale's sources leave to the author or make depend on context. Example: \"%\", \"km\", \"°C\". Disjoint from spacedUnits and closedUnits (scripts/validate-spec.mjs)."
|
|
133
|
+
},
|
|
134
|
+
"spacedUnits": {
|
|
135
|
+
"type": "array",
|
|
136
|
+
"items": { "type": "string", "minLength": 1 },
|
|
137
|
+
"uniqueItems": true,
|
|
138
|
+
"description": "Units and signs the locale writes with a space after the number. N5 binds an existing space with U+00A0 and inserts U+00A0 when the number and the unit touch (nbsp.md 3.7). Disjoint from beforeUnits, closedUnits and afterSymbols (scripts/validate-spec.mjs). Example: \"%\" in German, \"°C\" in Russian."
|
|
139
|
+
},
|
|
140
|
+
"closedUnits": {
|
|
141
|
+
"type": "array",
|
|
142
|
+
"items": { "type": "string", "minLength": 1 },
|
|
143
|
+
"uniqueItems": true,
|
|
144
|
+
"description": "Units and signs the locale writes closed up to the number. N5 deletes a single U+0020, U+00A0 or U+202F between the number and the unit (nbsp.md 3.7). Disjoint from beforeUnits, spacedUnits and afterSymbols (scripts/validate-spec.mjs). Example: \"%\" in English."
|
|
121
145
|
},
|
|
122
146
|
"beforeNumber": {
|
|
123
147
|
"type": "array",
|
|
@@ -154,6 +178,7 @@
|
|
|
154
178
|
"rule": {
|
|
155
179
|
"enum": [
|
|
156
180
|
"quotes",
|
|
181
|
+
"quote-punctuation",
|
|
157
182
|
"dashes",
|
|
158
183
|
"ranges",
|
|
159
184
|
"ellipsis",
|
|
@@ -29,8 +29,29 @@ module Polytypo
|
|
|
29
29
|
def self.locale_data_raw(locale_id)
|
|
30
30
|
@locale_data_raw ||= {}
|
|
31
31
|
@locale_data_raw[locale_id] ||=
|
|
32
|
-
|
|
32
|
+
validate_locale_data(
|
|
33
|
+
locale_id,
|
|
34
|
+
JSON.parse(File.read(File.join(DATA_DIR, "locales", "#{locale_id}.json"))),
|
|
35
|
+
)
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
PUNCTUATION_PLACEMENTS = %w[inside none].freeze
|
|
39
|
+
|
|
40
|
+
# quote-punctuation.md 2: `quotes.punctuationPlacement` is required in every locale file and
|
|
41
|
+
# is one of "inside" / "none". A missing or unknown value is a spec-data bug, reported
|
|
42
|
+
# rather than treated as "none".
|
|
43
|
+
def self.validate_locale_data(locale_id, data)
|
|
44
|
+
quotes = data.is_a?(Hash) ? data["quotes"] : nil
|
|
45
|
+
placement = quotes.is_a?(Hash) ? quotes["punctuationPlacement"] : nil
|
|
46
|
+
return data if PUNCTUATION_PLACEMENTS.include?(placement)
|
|
47
|
+
|
|
48
|
+
raise Polytypo::Error.new(
|
|
49
|
+
Polytypo::CODE_MALFORMED_LOCALE_DATA,
|
|
50
|
+
"locale #{locale_id.inspect}: quotes.punctuationPlacement must be \"inside\" or " \
|
|
51
|
+
"\"none\"; received #{placement.inspect}"
|
|
52
|
+
)
|
|
33
53
|
end
|
|
54
|
+
private_class_method :validate_locale_data
|
|
34
55
|
|
|
35
56
|
def self.lower_ascii?(cp)
|
|
36
57
|
cp >= LOWER_A && cp <= LOWER_Z
|
|
@@ -94,7 +94,7 @@ module Polytypo
|
|
|
94
94
|
# spec data in Registry/Locale (ARCHITECTURE.md section 7).
|
|
95
95
|
module Pipeline
|
|
96
96
|
# Defaults + opt-out overrides. rules_option is opt-out only (ARCHITECTURE.md section 7):
|
|
97
|
-
# it may only disable a default-on rule or enable
|
|
97
|
+
# it may only disable a default-on rule or enable a default-off one ("ranges", "quote-punctuation"). An
|
|
98
98
|
# unknown key raises POLYTYPO_UNKNOWN_RULE.
|
|
99
99
|
def self.resolve_rule_plan(rules_option)
|
|
100
100
|
defaults = Registry.rule_defaults
|
|
@@ -127,6 +127,18 @@ module Polytypo
|
|
|
127
127
|
keyword_init: true,
|
|
128
128
|
)
|
|
129
129
|
|
|
130
|
+
# One N5 entry: the unit as code points and which list it came from.
|
|
131
|
+
UnitEntry = Struct.new(:w, :kind)
|
|
132
|
+
|
|
133
|
+
# nbsp.md 3.7: the three unit lists merged so the longest entry wins across all of them.
|
|
134
|
+
def self.prepare_units(data)
|
|
135
|
+
out = []
|
|
136
|
+
[["beforeUnits", :before], ["spacedUnits", :spaced], ["closedUnits", :closed]].each do |field, kind|
|
|
137
|
+
data[field].each { |entry| out << UnitEntry.new(entry.codepoints, kind) }
|
|
138
|
+
end
|
|
139
|
+
out.each_with_index.sort_by { |entry, i| [-entry.w.length, i] }.map(&:first)
|
|
140
|
+
end
|
|
141
|
+
|
|
130
142
|
def self.malformed(message)
|
|
131
143
|
raise Polytypo::Error.new(Polytypo::CODE_MALFORMED_LOCALE_DATA, message)
|
|
132
144
|
end
|
|
@@ -202,7 +214,7 @@ module Polytypo
|
|
|
202
214
|
narrow_before_punctuation: narrow_before_punctuation,
|
|
203
215
|
short_words: prepare_list(data["afterShortWords"]),
|
|
204
216
|
abbreviations: prepare_list(data["abbreviations"]),
|
|
205
|
-
units:
|
|
217
|
+
units: prepare_units(data),
|
|
206
218
|
before_number: prepare_list(data["beforeNumber"]),
|
|
207
219
|
before_word: prepare_list(data["beforeWord"]),
|
|
208
220
|
symbols: prepare_list(data["afterSymbols"]),
|
|
@@ -389,29 +401,167 @@ module Polytypo
|
|
|
389
401
|
end
|
|
390
402
|
end
|
|
391
403
|
|
|
392
|
-
#
|
|
404
|
+
# nbsp.md 3.7 step 5.1: what may stand left of a number that N5 inserts or deletes beside.
|
|
405
|
+
NUMBER_PREFIXES = [0x2B, 0xB1, 0xD7, 0x2212, 0x7E, 0x2248, 0x3C, 0x3E, 0x2060].freeze
|
|
406
|
+
HYPHEN_MINUS = 0x2D
|
|
407
|
+
NON_BREAKING_HYPHEN = 0x2011
|
|
408
|
+
LOW_LINE = 0x5F
|
|
409
|
+
COMMA = 0x2C
|
|
410
|
+
WORD_JOINER = 0x2060
|
|
411
|
+
|
|
412
|
+
# ranges.md 3.2a CLOSED-SYMBOL, mirrored (this rule does not depend on `ranges`).
|
|
413
|
+
def self.closed_symbol?(cp)
|
|
414
|
+
cp == 0x24 || (cp >= 0xA2 && cp <= 0xA5) || (cp >= 0x20A0 && cp <= 0x20CF) ||
|
|
415
|
+
cp == 0x25 || cp == 0x2030 || cp == 0x2031 || cp == 0xB0
|
|
416
|
+
end
|
|
417
|
+
|
|
418
|
+
ANY_DASH = [
|
|
419
|
+
0x2D, 0x2010, 0x2013, 0x2014, 0x2212, 0xAD, 0x2011, 0x2012, 0x2015, 0xFE58, 0xFE63, 0xFF0D,
|
|
420
|
+
].freeze
|
|
421
|
+
|
|
422
|
+
# dashes.md 3.1 DASH + INERT-DASH.
|
|
423
|
+
def self.any_dash?(cp)
|
|
424
|
+
ANY_DASH.include?(cp)
|
|
425
|
+
end
|
|
426
|
+
|
|
427
|
+
# 3.7 step 5.4: the first code point past a run of non-breaking-line spaces and joiners.
|
|
428
|
+
def self.nearest_beyond(cp, from, step)
|
|
429
|
+
i = from
|
|
430
|
+
while i >= 0 && i < cp.length
|
|
431
|
+
c = cp[i]
|
|
432
|
+
if c == WORD_JOINER || (space_like?(c) && !break?(c))
|
|
433
|
+
i += step
|
|
434
|
+
next
|
|
435
|
+
end
|
|
436
|
+
return c
|
|
437
|
+
end
|
|
438
|
+
NONE
|
|
439
|
+
end
|
|
440
|
+
|
|
441
|
+
# Start of the number whose last digit is at `end_index`, extended over a decimal or
|
|
442
|
+
# thousands separator (3.7 step 5.1).
|
|
443
|
+
def self.number_start(cp, end_index)
|
|
444
|
+
b = end_index
|
|
445
|
+
b -= 1 while b - 1 >= 0 && digit?(cp[b - 1])
|
|
446
|
+
while (at(cp, b - 1) == COMMA || at(cp, b - 1) == FULL_STOP) && digit?(at(cp, b - 2))
|
|
447
|
+
b -= 2
|
|
448
|
+
b -= 1 while b - 1 >= 0 && digit?(cp[b - 1])
|
|
449
|
+
end
|
|
450
|
+
b
|
|
451
|
+
end
|
|
452
|
+
|
|
453
|
+
MUL_LETTERS = [0x78, 0x58, 0x445, 0x425].freeze
|
|
454
|
+
|
|
455
|
+
# symbols.md 3.1 SPACE | NOBREAK-SPACE: U+0020, U+00A0 and U+202F.
|
|
456
|
+
def self.mul_space?(cp)
|
|
457
|
+
cp == SPACE || cp == NBSP || cp == NNBSP
|
|
458
|
+
end
|
|
459
|
+
|
|
460
|
+
# 3.7 step 5.5: DIGIT [space] MUL-LETTER [space] immediately before the number start.
|
|
461
|
+
def self.right_operand_of_multiplication?(cp, b)
|
|
462
|
+
i = b - 1
|
|
463
|
+
i -= 1 if mul_space?(at(cp, i))
|
|
464
|
+
return false unless MUL_LETTERS.include?(at(cp, i))
|
|
465
|
+
|
|
466
|
+
i -= 1
|
|
467
|
+
i -= 1 if mul_space?(at(cp, i))
|
|
468
|
+
digit?(at(cp, i))
|
|
469
|
+
end
|
|
470
|
+
|
|
471
|
+
def self.number_start_clean?(cp, prep, b)
|
|
472
|
+
c = at(cp, b - 1)
|
|
473
|
+
return true if c == NONE || space_like?(c) || openish?(prep, c) || sentence_dash?(c)
|
|
474
|
+
return true if NUMBER_PREFIXES.include?(c)
|
|
475
|
+
return false if c != HYPHEN_MINUS
|
|
476
|
+
|
|
477
|
+
before = at(cp, b - 2)
|
|
478
|
+
before == NONE || space_like?(before) || openish?(prep, before)
|
|
479
|
+
end
|
|
480
|
+
|
|
481
|
+
# beforeUnits: convert one existing space after a number, with the letter guard
|
|
482
|
+
# ("H2 O", "A4", "MP3" are not measurements).
|
|
483
|
+
def self.convert_before_unit(cp, a, left, claims)
|
|
484
|
+
return if left != SPACE
|
|
485
|
+
return unless digit?(at(cp, a - 2))
|
|
486
|
+
|
|
487
|
+
b = a - 2
|
|
488
|
+
b -= 1 while b - 1 >= 0 && digit?(cp[b - 1])
|
|
489
|
+
return if UnicodeUtil.letter?(at(cp, b - 1))
|
|
490
|
+
|
|
491
|
+
claim_conversion(claims, a - 1, NBSP)
|
|
492
|
+
end
|
|
493
|
+
|
|
494
|
+
# nbsp.md 3.7 step 5: the guards on inserting or deleting the separator.
|
|
495
|
+
def self.unit_edit_allowed?(cp, entry, a, start, closed)
|
|
496
|
+
k = entry.w.length
|
|
497
|
+
# A closed-up symbol beside a dash decides whether `ranges` and `dashes` read the
|
|
498
|
+
# token as a range; editing the space between the number and the symbol would change
|
|
499
|
+
# that verdict on the next pass (step 5.3).
|
|
500
|
+
if closed_symbol?(entry.w[0]) &&
|
|
501
|
+
(any_dash?(nearest_beyond(cp, start - 1, -1)) || any_dash?(nearest_beyond(cp, a + k, 1)))
|
|
502
|
+
return false
|
|
503
|
+
end
|
|
504
|
+
return false if !closed && right_operand_of_multiplication?(cp, start)
|
|
505
|
+
|
|
506
|
+
following = at(cp, a + k)
|
|
507
|
+
return false if following == HYPHEN_MINUS || following == NON_BREAKING_HYPHEN || following == LOW_LINE
|
|
508
|
+
|
|
509
|
+
!(following == FULL_STOP && alnum?(at(cp, a + k + 1)))
|
|
510
|
+
end
|
|
511
|
+
|
|
512
|
+
# N5 -- nbsp.md 3.7. beforeUnits converts an existing space, spacedUnits converts and
|
|
513
|
+
# inserts, closedUnits deletes. The longest entry across the three lists wins at `a`.
|
|
393
514
|
def self.units_sub_rule(cp, prep, claims)
|
|
394
515
|
return if prep.units.empty?
|
|
395
516
|
|
|
396
517
|
(0...cp.length).each do |a|
|
|
397
|
-
|
|
398
|
-
next if
|
|
518
|
+
entry = prep.units.find { |candidate| match_exact?(cp, a, candidate.w) }
|
|
519
|
+
next if entry.nil?
|
|
399
520
|
|
|
400
|
-
k = w.length
|
|
521
|
+
k = entry.w.length
|
|
401
522
|
next if alnum?(at(cp, a + k))
|
|
402
523
|
|
|
403
524
|
left = at(cp, a - 1)
|
|
404
|
-
next if left == NBSP # already correct
|
|
405
|
-
next if left != SPACE
|
|
406
525
|
|
|
407
|
-
|
|
526
|
+
if entry.kind == :before
|
|
527
|
+
convert_before_unit(cp, a, left, claims)
|
|
528
|
+
next
|
|
529
|
+
end
|
|
530
|
+
|
|
531
|
+
closed = entry.kind == :closed
|
|
532
|
+
separated = left == SPACE || left == NBSP || (closed && left == NNBSP)
|
|
533
|
+
touching = digit?(left)
|
|
534
|
+
next if !separated && !(touching && !closed)
|
|
535
|
+
next if !closed && left == NBSP
|
|
536
|
+
|
|
537
|
+
end_index = separated ? a - 2 : a - 1
|
|
538
|
+
next unless digit?(at(cp, end_index))
|
|
408
539
|
|
|
409
|
-
b =
|
|
540
|
+
b = end_index
|
|
410
541
|
b -= 1 while b - 1 >= 0 && digit?(cp[b - 1])
|
|
411
|
-
# The letter guard: "H2 O", "A4", "MP3" are not measurements.
|
|
412
542
|
next if UnicodeUtil.letter?(at(cp, b - 1))
|
|
413
543
|
|
|
414
|
-
|
|
544
|
+
# Converting an existing space keeps beforeUnits' behaviour; the guards below protect
|
|
545
|
+
# only what N5 would otherwise insert or delete (3.7 step 5).
|
|
546
|
+
if !closed && separated
|
|
547
|
+
claim_conversion(claims, a - 1, NBSP) if left == SPACE
|
|
548
|
+
next
|
|
549
|
+
end
|
|
550
|
+
|
|
551
|
+
start = number_start(cp, end_index)
|
|
552
|
+
next unless number_start_clean?(cp, prep, start)
|
|
553
|
+
|
|
554
|
+
next unless unit_edit_allowed?(cp, entry, a, start, closed)
|
|
555
|
+
|
|
556
|
+
following = at(cp, a + k)
|
|
557
|
+
|
|
558
|
+
if closed
|
|
559
|
+
next if following == SPACE && digit?(at(cp, a + k + 1))
|
|
560
|
+
|
|
561
|
+
claims[a - 1] = Edit.new(a - 1, a, [], "nbsp") if claims[a - 1].nil?
|
|
562
|
+
else
|
|
563
|
+
claim_insertion(claims, a, NBSP)
|
|
564
|
+
end
|
|
415
565
|
end
|
|
416
566
|
end
|
|
417
567
|
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "quote_ambiguity"
|
|
4
|
+
require_relative "../sentinels"
|
|
5
|
+
require_relative "../unicode_util"
|
|
6
|
+
require_relative "../edits"
|
|
7
|
+
require_relative "../registry"
|
|
8
|
+
|
|
9
|
+
module Polytypo
|
|
10
|
+
module Engine
|
|
11
|
+
module Rules
|
|
12
|
+
# `quote-punctuation` -- spec/rules/quote-punctuation.md (spec 1.21.0), order 45, off by
|
|
13
|
+
# default.
|
|
14
|
+
#
|
|
15
|
+
# Moves a comma or a full stop from after the locale's closing primary quotation mark to
|
|
16
|
+
# before it, only where `quotes.punctuationPlacement` is "inside". One left-to-right scan
|
|
17
|
+
# over code points; every edit swaps two adjacent code points, so the text never changes
|
|
18
|
+
# length and no regex or normalization is involved. The inline marker is in no class below,
|
|
19
|
+
# so a span boundary or protected run on any of the three sides declines the move.
|
|
20
|
+
module QuotePunctuation
|
|
21
|
+
COMMA = 0x2C
|
|
22
|
+
FULL_STOP = 0x2E
|
|
23
|
+
|
|
24
|
+
# SPACELIKE (quotes.md 3.1): INLINE-SPACE plus BREAK, with Engine::LINE_MARKER a member of
|
|
25
|
+
# BREAK for every rule (modes.md 3.2).
|
|
26
|
+
BREAK = [0x0A, 0x0D, 0x0B, 0x0C, 0x85, 0x2028, 0x2029, Engine::LINE_MARKER].freeze
|
|
27
|
+
|
|
28
|
+
def self.spacelike?(cp)
|
|
29
|
+
QuoteAmbiguity.inline_space?(cp) || BREAK.include?(cp)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
# cp[i], or NONE if i is out of bounds -- the spec's own boundary value.
|
|
33
|
+
def self.at(cp, i)
|
|
34
|
+
return NONE if i.negative? || i >= cp.length
|
|
35
|
+
|
|
36
|
+
cp[i]
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
# quote-punctuation.md 3.2: C1-C4 for the pair at i, i + 1.
|
|
40
|
+
def self.edit_at?(cp, i, close)
|
|
41
|
+
return false unless cp[i] == close && i >= 1
|
|
42
|
+
|
|
43
|
+
next_cp = cp[i + 1]
|
|
44
|
+
return false unless next_cp == COMMA || next_cp == FULL_STOP
|
|
45
|
+
return false unless UnicodeUtil.letter?(cp[i - 1])
|
|
46
|
+
|
|
47
|
+
after = at(cp, i + 2)
|
|
48
|
+
after == NONE || spacelike?(after)
|
|
49
|
+
end
|
|
50
|
+
private_class_method :edit_at?
|
|
51
|
+
|
|
52
|
+
def self.scan(cp, locale_data, _ctx)
|
|
53
|
+
quotes = locale_data["quotes"]
|
|
54
|
+
return [] unless quotes["punctuationPlacement"] == "inside"
|
|
55
|
+
|
|
56
|
+
close = quotes["primary"]["close"].codepoints.first
|
|
57
|
+
return [] if close.nil?
|
|
58
|
+
|
|
59
|
+
edits = []
|
|
60
|
+
i = 0
|
|
61
|
+
while i + 1 < cp.length
|
|
62
|
+
if edit_at?(cp, i, close)
|
|
63
|
+
edits << Edit.new(i, i + 2, [cp[i + 1], close], "quote-punctuation")
|
|
64
|
+
i += 2
|
|
65
|
+
else
|
|
66
|
+
i += 1
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
edits
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
Polytypo::Engine::Registry.register(
|
|
77
|
+
"quote-punctuation",
|
|
78
|
+
->(cp, locale_data, ctx) { Polytypo::Engine::Rules::QuotePunctuation.scan(cp, locale_data, ctx) },
|
|
79
|
+
)
|
|
@@ -14,6 +14,7 @@ require_relative "rules/ranges"
|
|
|
14
14
|
require_relative "rules/dashes"
|
|
15
15
|
require_relative "rules/hyphen"
|
|
16
16
|
require_relative "rules/quotes"
|
|
17
|
+
require_relative "rules/quote_punctuation"
|
|
17
18
|
require_relative "rules/apostrophe"
|
|
18
19
|
require_relative "rules/symbols"
|
|
19
20
|
require_relative "rules/nbsp"
|
data/lib/polytypo/version.rb
CHANGED
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: polytypo
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.
|
|
4
|
+
version: 1.22.1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Iurii Rogulia
|
|
@@ -106,6 +106,7 @@ files:
|
|
|
106
106
|
- lib/polytypo/data/rules/nbsp.md
|
|
107
107
|
- lib/polytypo/data/rules/order.json
|
|
108
108
|
- lib/polytypo/data/rules/pipeline-idempotency.md
|
|
109
|
+
- lib/polytypo/data/rules/quote-punctuation.md
|
|
109
110
|
- lib/polytypo/data/rules/quotes.md
|
|
110
111
|
- lib/polytypo/data/rules/ranges.md
|
|
111
112
|
- lib/polytypo/data/rules/spaces.md
|
|
@@ -130,6 +131,7 @@ files:
|
|
|
130
131
|
- lib/polytypo/engine/rules/hyphen.rb
|
|
131
132
|
- lib/polytypo/engine/rules/nbsp.rb
|
|
132
133
|
- lib/polytypo/engine/rules/quote_ambiguity.rb
|
|
134
|
+
- lib/polytypo/engine/rules/quote_punctuation.rb
|
|
133
135
|
- lib/polytypo/engine/rules/quotes.rb
|
|
134
136
|
- lib/polytypo/engine/rules/ranges.rb
|
|
135
137
|
- lib/polytypo/engine/rules/spaces.rb
|