sterile 1.0.27 → 1.0.28

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3217764c5d98385b4370583410521fc77e5c6f32c9a614b40f8ad091c2d463e2
4
- data.tar.gz: 968a2433301ba635902d0bd04211c88086f314d5ce09c33562c2428b0f1d4dc1
3
+ metadata.gz: af85f613df97b0ecc301efe9c80e2542a382e54ccdd92b51643dc27897bc04c3
4
+ data.tar.gz: 4c5fcbd47f06a3ca28c4bde53150d20276fc419b0a6d87ed0f57b2bd81b14b40
5
5
  SHA512:
6
- metadata.gz: 2320eebe4664211f01bef43ff1b824c55a4c3a6bb0933d45d6b5685b2d31b6e7e5a3b5269844d5b6283e41bd7a9e81865cd14cf23217960a7cee729922f07a2a
7
- data.tar.gz: 7324ee22466c0e9359da028980cac6eb6640189b7c36eaef30475612c2f34aff1a5c73fe07090ec5e8df4877827eaefa72893d9ec03d55fbf79bcba4150469df
6
+ metadata.gz: 71b43104d13031c2af273917149884957499ebb60de85b6e9889184b8b67ae5b902e4581c36af1b08c84a1b6ea975c8df8526c1df45426916ac4f19f209e9995
7
+ data.tar.gz: f3312ce5d49836154ce8b5f7f332bac27bd74ad4943e67fb5eced0eb86b1b2e477e8f28183c1f4a650b2682bc6b725a5a8ff013d47e64d980cc6b0e7a6593741
@@ -10,6 +10,8 @@ module Sterile
10
10
  ["'twas", "’twas"],
11
11
  ["'tis", "’tis"],
12
12
  ["'twill", "’twill"],
13
+ ["'Tain't", "’Tain’t"],
14
+ [/\'(Tis|Twas|Twill|Twere)\b/, "’\\1"],
13
15
  ["'til", "’til"],
14
16
  ["'bout", "’bout"],
15
17
  ["'nuff", "’nuff"],
@@ -17,10 +19,15 @@ module Sterile
17
19
  ["'cause", "’cause"],
18
20
  ["'cos", "’cos"],
19
21
  ["i'm", "i’m"],
20
- ['--"', "—“"],
21
- ["--'", "—‘"],
22
- ["--", "—"],
22
+ # The Afrikaans article ("'n man"), and "rock 'n roll".
23
+ [/\'n(?=[\p{Z}\s])/, "’n"],
24
+ # Not three hyphens ("h---"); a longer run still pairs off.
25
+ [/(?<!-)--(?!-(?!-))|(?<=--)--/, "—"],
23
26
  ["...", "…"],
27
+ # A quote after a dash opens (word--"quote"), unless a space, closing
28
+ # punctuation, another dash or the end follows ("Why--" he said).
29
+ [/([—–])"(?![\p{Z}\s,;:!?)\]—–]|\.(?![\p{Z}\s]?\.)|\z)/, "\\1“"],
30
+ [/([—–])\'(?![\p{Z}\s,;:!?)\]—–\d]|\.(?![\p{Z}\s]?\.)|\z)/, "\\1‘"],
24
31
  ["(tm)", "™"],
25
32
  ["(TM)", "™"],
26
33
  # Only where it's clearly a copyright notice, so list markers like
@@ -39,11 +46,12 @@ module Sterile
39
46
  [/"([:;])/, "”\\1"],
40
47
  [/\'s$/, "’s"],
41
48
  [/\'(\d\d(?:’|\')?s)/, "’\\1"],
42
- [/(\s|\A|"|\(|\[)\'/, "\\1‘"],
49
+ # A quote opens after a space, a non-breaking one included.
50
+ [/([\p{Z}\s]|&nbsp;|\A|"|\(|\[)\'/, "\\1‘"],
43
51
  # [/(\d+)"/, "\\1″"],
44
52
  # [/(\d+)\'/, "\\1′"],
45
53
  [/(\S)\'([^\'\s])/, "\\1’\\2"],
46
- [/(\s|\A|\(|\[)"(?!\s)/, "\\1“\\2"],
54
+ [/([\p{Z}\s]|&nbsp;|\A|\(|\[)"(?![\p{Z}\s]|&nbsp;)/, "\\1“"],
47
55
  [/"(\s|\S|\Z)/, "”\\1"],
48
56
  [/\'([\s.]|\Z)/, "’\\1"],
49
57
  [/(\b[1-9]\d*|0)x([1-9]\d*|0\b)/, "\\1×\\2"],
@@ -52,4 +60,3 @@ module Sterile
52
60
  end
53
61
  end
54
62
  end
55
-
@@ -18,18 +18,99 @@ module Sterile
18
18
  end
19
19
 
20
20
 
21
- # Like +smart_format+, but works with HTML/XML (somewhat).
21
+ # Like +smart_format+, but works with HTML/XML (somewhat). Quotes and
22
+ # apostrophes are formatted by the text on either side of them, across
23
+ # inline tags ("<em>Bible</em>'s"), starting over in each block and
24
+ # after a line break.
22
25
  #
23
26
  def smart_format_tags(string)
24
27
  string = string.gsub(/[\p{Z}\s]+(<\/[a-zA-Z]+>)(['"][a-zA-Z])/, "\\1 \\2") # Fixes quote after whitespace + tag "<em>Dan. </em>'And"
25
- string.gsub_tags do |text|
26
- text.smart_format
27
- end.encode_entities.gsub(/(\<\/\w+\>)&ldquo;/, "\\1&rdquo;").gsub(/(\<\/\w+\>)&lsquo;/, "\\1&rsquo;")
28
+ fragment = Nokogiri::HTML::DocumentFragment.parse string
29
+ smart_format_text_runs(fragment).each do |run|
30
+ texts = run.map(&:content)
31
+ run.each_with_index do |node, index|
32
+ before = texts[0...index].reverse.find { |text| !text.empty? }
33
+ after = texts[(index + 1)..-1].find { |text| !text.empty? }
34
+ node.content = smart_format_in_context(texts[index], before, after)
35
+ end
36
+ end
37
+ fragment.to_html.encode_entities
28
38
  end
29
39
 
30
40
 
31
41
  private
32
42
 
43
+ # Elements that start a new stretch of text. Text in any other element,
44
+ # known or not, reads on from the text before it.
45
+ SMART_FORMAT_BLOCK_TAGS = %w[
46
+ address article aside blockquote body br caption center dd details dialog dir div dl dt fieldset
47
+ figcaption figure footer form h1 h2 h3 h4 h5 h6 header hgroup hr html li main menu nav ol p pre
48
+ section summary table tbody td tfoot th thead title tr ul
49
+ ].freeze
50
+
51
+ # Stands in for neighboring text no rule should treat as a space, a letter
52
+ # or punctuation (a word joiner).
53
+ SMART_FORMAT_NEUTRAL = "\u2060".freeze
54
+
55
+
56
+ # Text nodes in document order, grouped into runs that read as continuous
57
+ # text: a run ends at a block element or a line break.
58
+ #
59
+ def smart_format_text_runs(node, runs = [[]])
60
+ node.children.each do |child|
61
+ if child.text?
62
+ runs.last << child
63
+ elsif child.element? && SMART_FORMAT_BLOCK_TAGS.include?(child.name)
64
+ runs << []
65
+ smart_format_text_runs child, runs
66
+ runs << []
67
+ elsif child.element?
68
+ smart_format_text_runs child, runs
69
+ end
70
+ end
71
+ runs
72
+ end
73
+
74
+
75
+ # Smart format text that sits between two other pieces of text. One
76
+ # character stands in for each neighbor while the rules run.
77
+ #
78
+ def smart_format_in_context(text, before, after)
79
+ head = smart_format_stand_in_before(before)
80
+ tail = smart_format_stand_in_after(after)
81
+ formatted = smart_format("#{head}#{text}#{tail}")
82
+ if formatted.start_with?(head) && formatted.end_with?(tail) && formatted.length >= head.length + tail.length
83
+ formatted[head.length, formatted.length - head.length - tail.length]
84
+ else
85
+ smart_format(text.dup)
86
+ end
87
+ end
88
+
89
+
90
+ def smart_format_stand_in_before(text)
91
+ case text
92
+ when nil then ""
93
+ when /(?:--|[—–])\z/ then "—"
94
+ when /[\p{Z}\s]\z/ then " "
95
+ when /[(\[{"“]\z/ then "["
96
+ when /[\p{L}\p{N}]\z/ then text[-1]
97
+ else SMART_FORMAT_NEUTRAL
98
+ end
99
+ end
100
+
101
+
102
+ def smart_format_stand_in_after(text)
103
+ case text
104
+ when nil then ""
105
+ when /\A[\p{Z}\s]/ then " "
106
+ when /\A\p{L}/ then "a" # Any letter will do, but "s" would end the "'s" rule.
107
+ when /\A[\p{N}:;]/ then text[0]
108
+ when /\A(?:[,!?)\]}]|\.(?![\p{Z}\s]?\.))/ then ","
109
+ else SMART_FORMAT_NEUTRAL
110
+ end
111
+ end
112
+
113
+
33
114
  # Lazy load smart formatting rules
34
115
  #
35
116
  def smart_format_rules
@@ -1,6 +1,6 @@
1
1
  # encoding: UTF-8
2
2
 
3
3
  module Sterile
4
- VERSION = "1.0.27"
4
+ VERSION = "1.0.28"
5
5
  end
6
6
 
data/test/test_sterile.rb CHANGED
@@ -55,8 +55,98 @@ class TestSterile < Minitest::Test
55
55
  assert_equal "1x01", Sterile.smart_format("1x01")
56
56
  end
57
57
 
58
- def test_smart_format_tags
59
- # ?
58
+ def test_smart_format_quote_after_dash
59
+ assert_equal "then—“go” and then—‘go’", Sterile.smart_format(%q{then—"go" and then—'go'})
60
+ assert_equal "word—“go” and word—‘go’", Sterile.smart_format(%q{word--"go" and word--'go'})
61
+ assert_equal "1914–“go”", Sterile.smart_format(%q{1914–"go"})
62
+ assert_equal "composition—“… Because", Sterile.smart_format(%q{composition--"... Because})
63
+ assert_equal "composition—“. . . Because", Sterile.smart_format(%q{composition--". . . Because})
64
+ assert_equal "rock—’90s style", Sterile.smart_format(%q{rock--'90s style})
65
+ end
66
+
67
+ def test_smart_format_closing_quote_after_dash
68
+ assert_equal "“Hello—” he said", Sterile.smart_format(%q{"Hello--" he said})
69
+ assert_equal "“Wait—”, he said. “No—”.", Sterile.smart_format(%q{"Wait--", he said. "No--".})
70
+ assert_equal "“I can’t—”", Sterile.smart_format(%q{"I can't--"})
71
+ assert_equal "‘Why—’ he said", Sterile.smart_format(%q{'Why--' he said})
72
+ assert_equal "“Soda—” and “Movie—”—or", Sterile.smart_format(%q{"Soda--" and "Movie--"--or})
73
+ end
74
+
75
+ def test_smart_format_capitalized_elisions
76
+ assert_equal "’Tis so. ’Twas so. ’Twill be. ’Twere so. ’Tain’t so.", Sterile.smart_format("'Tis so. 'Twas so. 'Twill be. 'Twere so. 'Tain't so.")
77
+ assert_equal "“’Tis the season,” ’tis said", Sterile.smart_format(%q{"'Tis the season," 'tis said})
78
+ assert_equal "‘Tissue’ and ‘Twist’", Sterile.smart_format("'Tissue' and 'Twist'")
79
+ end
80
+
81
+ def test_smart_format_quotes_after_non_breaking_space
82
+ assert_equal "word “quote” and ‘quote’", Sterile.smart_format("word \"quote\" and 'quote'")
83
+ assert_equal "word&nbsp;“quote” and&nbsp;‘quote’", Sterile.smart_format(%q{word&nbsp;"quote" and&nbsp;'quote'})
84
+ assert_equal "“quote” word and ‘quote’ word", Sterile.smart_format("\"quote\" word and 'quote' word")
85
+ assert_equal "“quote”&nbsp;word and ‘quote’&nbsp;word", Sterile.smart_format(%q{"quote"&nbsp;word and 'quote'&nbsp;word})
86
+ assert_equal "said ” and", Sterile.smart_format("said \" and")
87
+ end
88
+
89
+ def test_smart_format_tags_quotes_after_non_breaking_space
90
+ assert_equal "word&nbsp;&ldquo;quote&rdquo;&nbsp;and&nbsp;&lsquo;quote&rsquo;", Sterile.smart_format_tags(%q{word&nbsp;"quote"&nbsp;and&nbsp;'quote'})
91
+ assert_equal "<em>word&nbsp;</em>&ldquo;quote&rdquo;", Sterile.smart_format_tags(%q{<em>word&nbsp;</em>"quote"})
92
+ end
93
+
94
+ def test_smart_format_afrikaans_article
95
+ assert_equal "Dit was ’n besef—’n waarheid. ’n Man", Sterile.smart_format("Dit was 'n besef--'n waarheid. 'n Man")
96
+ assert_equal "the letter ‘n’ and ‘no’", Sterile.smart_format("the letter 'n' and 'no'")
97
+ end
98
+
99
+ def test_smart_format_leaves_three_hyphens
100
+ assert_equal "Go to h---, he said", Sterile.smart_format("Go to h---, he said")
101
+ assert_equal "one—two", Sterile.smart_format("one--two")
102
+ assert_equal "——", Sterile.smart_format("----")
103
+ assert_equal "——-", Sterile.smart_format("-----")
104
+ assert_equal "———", Sterile.smart_format("------")
105
+ end
106
+
107
+ def test_smart_format_tags_quotes_around_tags
108
+ assert_equal "&ldquo;<em>Word</em>&rdquo; and &lsquo;<em>Word</em>&rsquo;", Sterile.smart_format_tags(%q{"<em>Word</em>" and '<em>Word</em>'})
109
+ assert_equal "<em>&ldquo;Word&rdquo;</em> and <em>&lsquo;Word&rsquo;</em>", Sterile.smart_format_tags(%q{<em>"Word"</em> and <em>'Word'</em>})
110
+ assert_equal "He said, &ldquo;<em>Go</em>.&rdquo; Then &ldquo;<em>stop</em>,&rdquo; he said.", Sterile.smart_format_tags(%q{He said, "<em>Go</em>." Then "<em>stop</em>," he said.})
111
+ assert_equal "&ldquo;<em>A</em>&rdquo; &ldquo;<b><em>B</em></b>&rdquo; (&ldquo;<em>C</em>&rdquo;)", Sterile.smart_format_tags(%q{"<em>A</em>" "<b><em>B</em></b>" ("<em>C</em>")})
112
+ assert_equal "<em>Dan.</em> &lsquo;And then&rsquo;", Sterile.smart_format_tags(%q{<em>Dan. </em>'And then'})
113
+ end
114
+
115
+ def test_smart_format_tags_apostrophe_across_tags
116
+ assert_equal "<em>Bible</em>&rsquo;s and <em>Bibles</em>&rsquo; covers", Sterile.smart_format_tags(%q{<em>Bible</em>'s and <em>Bibles</em>' covers})
117
+ assert_equal "Armstrong<span>&rsquo;s footsteps</span>", Sterile.smart_format_tags(%q{Armstrong<span>'s footsteps</span>})
118
+ assert_equal "<span>GM</span><em>&rsquo;s</em> top", Sterile.smart_format_tags(%q{<span>GM</span><em>'s</em> top})
119
+ assert_equal "don<em>&rsquo;t</em>", Sterile.smart_format_tags(%q{don<em>'t</em>})
120
+ assert_equal "described as &lsquo;<em>standing</em>&rsquo; there", Sterile.smart_format_tags(%q{described as '<em>standing</em>' there})
121
+ assert_equal "<em>Youth &rsquo;81</em> magazine", Sterile.smart_format_tags(%q{<em>Youth '81</em> magazine})
122
+ end
123
+
124
+ def test_smart_format_tags_quote_after_dash_across_tags
125
+ assert_equal "word&mdash;&ldquo;<em>Quote</em>&rdquo;&mdash;word", Sterile.smart_format_tags(%q{word--"<em>Quote</em>"--word})
126
+ assert_equal "<em>Doe v. Bolton&mdash;</em>&ldquo;viability&rdquo; was", Sterile.smart_format_tags(%q{<em>Doe v. Bolton--</em>"viability" was})
127
+ assert_equal "<em>word</em>&mdash;&ldquo;quote&rdquo;", Sterile.smart_format_tags(%q{<em>word</em>—"quote"})
128
+ assert_equal "&ldquo;<em>Wait&mdash;</em>&rdquo; he said", Sterile.smart_format_tags(%q{"<em>Wait--</em>" he said})
129
+ end
130
+
131
+ def test_smart_format_tags_starts_over_in_each_block
132
+ assert_equal "<p>one</p><p>&lsquo;two&rsquo;</p>", Sterile.smart_format_tags(%q{<p>one</p><p>'two'</p>})
133
+ assert_equal "<p>one</p>&ldquo;two&rdquo;", Sterile.smart_format_tags(%q{<p>one</p>"two"})
134
+ assert_equal "<p>one<br>&ldquo;two&rdquo;</p>", Sterile.smart_format_tags(%q{<p>one<br>"two"</p>})
135
+ assert_equal "<figure>one</figure>&ldquo;two&rdquo;", Sterile.smart_format_tags(%q{<figure>one</figure>"two"})
136
+ end
137
+
138
+ def test_smart_format_tags_reads_on_through_unknown_tags
139
+ assert_equal "&ldquo;one<uppercase>!</uppercase>&rdquo;", Sterile.smart_format_tags(%q{"one<uppercase>!</uppercase>"})
140
+ end
141
+
142
+ def test_smart_format_tags_lists
143
+ assert_equal "<ul>\n<li>one</li>\n<li>&ldquo;two&rdquo;</li>\n</ul>", Sterile.smart_format_tags(%q{<ul><li>one</li><li>"two"</li></ul>}).gsub("&#10;", "\n")
144
+ end
145
+
146
+ def test_smart_format_tags_leaves_markup_alone
147
+ assert_equal %q{<a href="a--b?x=1&amp;y=2" title="it's">it&rsquo;s &ldquo;x&rdquo;&mdash;y</a>}, Sterile.smart_format_tags(%q{<a href="a--b?x=1&amp;y=2" title="it's">it's "x"--y</a>})
148
+ assert_equal "AT&amp;T &lt;b&gt; 2&times;4 <em>x</em>", Sterile.smart_format_tags("AT&amp;T &lt;b&gt; 2x4 <em>x</em>")
149
+ assert_equal "wait&hellip; <em>(c) 2020</em> (<em>tm</em>)", Sterile.smart_format_tags("wait... <em>(c) 2020</em> (<em>tm</em>)").sub("&copy;", "(c)")
60
150
  end
61
151
 
62
152
  def test_sterilize
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sterile
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.27
4
+ version: 1.0.28
5
5
  platform: ruby
6
6
  authors:
7
7
  - Patrick Hogan
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2026-11-03 00:00:00.000000000 Z
11
+ date: 2026-11-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nokogiri
@@ -82,7 +82,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
82
82
  - !ruby/object:Gem::Version
83
83
  version: '0'
84
84
  requirements: []
85
- rubygems_version: 3.4.22
85
+ rubygems_version: 3.4.19
86
86
  signing_key:
87
87
  specification_version: 4
88
88
  summary: Sterilize your strings! Transliterate, generate slugs, smart format, strip