rubypants 0.5.0 → 0.5.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 505ccfdec7463152b84a66f2d0c0b6ee4df7d958
4
- data.tar.gz: 8e04eed0de2037978c2733121d4021f96b6fffb7
3
+ metadata.gz: 0b29b8d72353258ac6b847c78c77fedaa8a9cb33
4
+ data.tar.gz: 965af58a1f2e7770e5a76a748e7ad564f6e24335
5
5
  SHA512:
6
- metadata.gz: d87f017e65610d9c582195716164ecfef7006d4125b1754f0901b269a32f54232fa139c06c6cd1797748222e303f007e32857cdf6b2b3133dc58f4e3a7588acc
7
- data.tar.gz: 07b29cb1837778b5c54bc18e6345f8202c51366d42f51c6d5436008b2e54da9d79de6a7c5bb601fefe854f0f9c04da936f0abd1b87315b6649ac8d9f6d44b952
6
+ metadata.gz: 7e8c42ca85e3a117f954311cbb016779329c9e390200a70244fd2f5828959a60d4226a924ae777904032ea756f4b774367c9ddd578b241621ce18b5ff1a33f4c
7
+ data.tar.gz: 4456339e90147d4887917441d9400f17f1cb0f5df3a45e2528629eed88ed5da309eb99b6d0dff70819538ff1966d8351d6dd2d3e3bb3ad5ab630868db759f6f4
@@ -48,7 +48,7 @@ class RubyPants < String
48
48
  # Apply SmartyPants transformations.
49
49
  def to_html
50
50
  do_quotes = do_backticks = do_dashes = do_ellipses = do_stupify = nil
51
- convert_quotes = false
51
+ convert_quotes = prevent_breaks = nil
52
52
 
53
53
  if @options.include?(0)
54
54
  # Do nothing.
@@ -68,18 +68,20 @@ class RubyPants < String
68
68
  do_dashes = :inverted
69
69
  elsif @options.include?(-1)
70
70
  do_stupefy = true
71
- else
72
- do_quotes = @options.include?(:quotes)
73
- do_backticks = @options.include?(:backticks)
74
- do_backticks = :both if @options.include?(:allbackticks)
75
- do_dashes = :normal if @options.include?(:dashes)
76
- do_dashes = :oldschool if @options.include?(:oldschool)
77
- do_dashes = :inverted if @options.include?(:inverted)
78
- do_ellipses = @options.include?(:ellipses)
79
- convert_quotes = @options.include?(:convertquotes)
80
- do_stupefy = @options.include?(:stupefy)
81
71
  end
82
72
 
73
+ # Explicit flags override numeric flag groups.
74
+ do_quotes = true if @options.include?(:quotes)
75
+ do_backticks = true if @options.include?(:backticks)
76
+ do_backticks = :both if @options.include?(:allbackticks)
77
+ do_dashes = :normal if @options.include?(:dashes)
78
+ do_dashes = :oldschool if @options.include?(:oldschool)
79
+ do_dashes = :inverted if @options.include?(:inverted)
80
+ prevent_breaks = true if @options.include?(:prevent_breaks)
81
+ do_ellipses = true if @options.include?(:ellipses)
82
+ convert_quotes = true if @options.include?(:convertquotes)
83
+ do_stupefy = true if @options.include?(:stupefy)
84
+
83
85
  # Parse the HTML
84
86
  tokens = tokenize
85
87
 
@@ -119,12 +121,12 @@ class RubyPants < String
119
121
  t.gsub!(/&quot;/, '"') if convert_quotes
120
122
 
121
123
  if do_dashes
122
- t = educate_dashes t if do_dashes == :normal
123
- t = educate_dashes_oldschool t if do_dashes == :oldschool
124
- t = educate_dashes_inverted t if do_dashes == :inverted
124
+ t = educate_dashes t, prevent_breaks if do_dashes == :normal
125
+ t = educate_dashes_oldschool t, prevent_breaks if do_dashes == :oldschool
126
+ t = educate_dashes_inverted t, prevent_breaks if do_dashes == :inverted
125
127
  end
126
128
 
127
- t = educate_ellipses t if do_ellipses
129
+ t = educate_ellipses t, prevent_breaks if do_ellipses
128
130
 
129
131
  # Note: backticks need to be processed before quotes.
130
132
  if do_backticks
@@ -195,22 +197,38 @@ class RubyPants < String
195
197
  DOUBLE_DASH = n_of(2, '-')
196
198
  TRIPLE_DASH = n_of(3, '-')
197
199
 
198
- # The string, with each instance of "<tt>--</tt>" translated to an
200
+ # Return +str+ replacing all +patt+ with +repl+. If +prevent_breaks+ is true,
201
+ # then replace spaces preceding +patt+ with a non-breaking space, and if there
202
+ # are no spaces, then insert a word-joiner.
203
+ #
204
+ def educate(str, patt, repl, prevent_breaks)
205
+ patt = /(?<spaces>[[:space:]]*)#{patt}/
206
+ str.gsub(patt) do
207
+ spaces = if prevent_breaks && $~['spaces'].length > 0
208
+ entity(:non_breaking_space) # * $~['spaces'].length
209
+ elsif prevent_breaks
210
+ entity(:word_joiner)
211
+ else
212
+ $~['spaces']
213
+ end
214
+ spaces + repl
215
+ end
216
+ end
217
+
218
+ # Return the string, with each instance of "<tt>--</tt>" translated to an
199
219
  # em-dash HTML entity.
200
220
  #
201
- def educate_dashes(str)
202
- str.
203
- gsub(DOUBLE_DASH, entity(:em_dash))
221
+ def educate_dashes(str, prevent_breaks=false)
222
+ educate(str, DOUBLE_DASH, entity(:em_dash), prevent_breaks)
204
223
  end
205
224
 
206
- # The string, with each instance of "<tt>--</tt>" translated to an
225
+ # Return the string, with each instance of "<tt>--</tt>" translated to an
207
226
  # en-dash HTML entity, and each "<tt>---</tt>" translated to an
208
227
  # em-dash HTML entity.
209
228
  #
210
- def educate_dashes_oldschool(str)
211
- str.
212
- gsub(TRIPLE_DASH, entity(:em_dash)).
213
- gsub(DOUBLE_DASH, entity(:en_dash))
229
+ def educate_dashes_oldschool(str, prevent_breaks=false)
230
+ str = educate(str, TRIPLE_DASH, entity(:em_dash), prevent_breaks)
231
+ educate(str, DOUBLE_DASH, entity(:en_dash), prevent_breaks)
214
232
  end
215
233
 
216
234
  # Return the string, with each instance of "<tt>--</tt>" translated
@@ -223,20 +241,19 @@ class RubyPants < String
223
241
  # sense that the shortcut should be shorter to type. (Thanks to
224
242
  # Aaron Swartz for the idea.)
225
243
  #
226
- def educate_dashes_inverted(str)
227
- str.
228
- gsub(TRIPLE_DASH, entity(:en_dash)).
229
- gsub(DOUBLE_DASH, entity(:em_dash))
244
+ def educate_dashes_inverted(str, prevent_breaks=false)
245
+ str = educate(str, TRIPLE_DASH, entity(:en_dash), prevent_breaks)
246
+ educate(str, DOUBLE_DASH, entity(:em_dash), prevent_breaks)
230
247
  end
231
248
 
232
249
  # Return the string, with each instance of "<tt>...</tt>" translated
233
250
  # to an ellipsis HTML entity. Also converts the case where there are
234
251
  # spaces between the dots.
235
252
  #
236
- def educate_ellipses(str)
237
- str.
238
- gsub(RubyPants.n_of(3, '.'), entity(:ellipsis)).
239
- gsub(/(?<!\.|\. )\. \. \.(?!\.| \.)/, entity(:ellipsis))
253
+ def educate_ellipses(str, prevent_breaks=false)
254
+ str = educate(str, RubyPants.n_of(3, '.'), entity(:ellipsis), prevent_breaks)
255
+ educate(str, /(?<!\.|\.[[:space:]])\.[[:space:]]\.[[:space:]]\.(?!\.|[[:space:]]\.)/,
256
+ entity(:ellipsis), prevent_breaks)
240
257
  end
241
258
 
242
259
  # Return the string, with "<tt>``backticks''</tt>"-style single quotes
@@ -353,7 +370,7 @@ class RubyPants < String
353
370
  # Chad Miller in the Python port of SmartyPants.
354
371
  #
355
372
  def tokenize
356
- tag_soup = /([^<]*)(<!--.*?-->|<[^>]*>)/
373
+ tag_soup = /([^<]*)(<!--.*?-->|<[^>]*>)/m
357
374
 
358
375
  tokens = []
359
376
 
@@ -381,7 +398,9 @@ class RubyPants < String
381
398
  :em_dash => "&#8212;",
382
399
  :en_dash => "&#8211;",
383
400
  :ellipsis => "&#8230;",
384
- :html_quote => "&quot;"
401
+ :html_quote => "&quot;",
402
+ :non_breaking_space => "&nbsp;",
403
+ :word_joiner => "&#65279;",
385
404
  }
386
405
  end
387
406
 
@@ -1,3 +1,3 @@
1
1
  class RubyPants
2
- VERSION = "0.5.0"
2
+ VERSION = "0.5.1"
3
3
  end
@@ -74,22 +74,53 @@ EOF
74
74
  assert_rp_equal "--foo--bar--quux--",
75
75
  '&#8212;foo&#8212;bar&#8212;quux&#8212;', 1
76
76
 
77
+ assert_rp_equal "foo--bar", 'foo&#65279;&#8212;bar', [1, :prevent_breaks]
78
+ assert_rp_equal "foo --bar", 'foo &#8212;bar', 1
79
+ assert_rp_equal "foo --bar", 'foo&nbsp;&#8212;bar', [1, :prevent_breaks]
80
+ assert_rp_equal "foo -- bar", 'foo&nbsp;&#8212; bar', [1, :prevent_breaks]
81
+ assert_rp_equal "foo --bar", 'foo&nbsp;&#8212;bar', [1, :prevent_breaks]
82
+
77
83
  assert_rp_equal "foo--bar", 'foo&#8211;bar', 2
78
84
  assert_rp_equal "foo---bar", 'foo&#8212;bar', 2
79
85
  assert_rp_equal "foo----bar", 'foo----bar', 2
80
86
  assert_rp_equal "--foo--bar--quux--",
81
87
  '&#8211;foo&#8211;bar&#8211;quux&#8211;', 2
82
88
 
89
+ assert_rp_equal "foo--bar", 'foo&#65279;&#8211;bar', [2, :prevent_breaks]
90
+ assert_rp_equal "foo --bar", 'foo &#8211;bar', 2
91
+ assert_rp_equal "foo --bar", 'foo&nbsp;&#8211;bar', [2, :prevent_breaks]
92
+ assert_rp_equal "foo -- bar", 'foo&nbsp;&#8211; bar', [2, :prevent_breaks]
93
+ assert_rp_equal "foo --bar", 'foo&nbsp;&#8211;bar', [2, :prevent_breaks]
94
+
95
+ assert_rp_equal "foo---bar", 'foo&#65279;&#8212;bar', [2, :prevent_breaks]
96
+ assert_rp_equal "foo ---bar", 'foo &#8212;bar', 2
97
+ assert_rp_equal "foo ---bar", 'foo&nbsp;&#8212;bar', [2, :prevent_breaks]
98
+ assert_rp_equal "foo --- bar", 'foo&nbsp;&#8212; bar', [2, :prevent_breaks]
99
+ assert_rp_equal "foo ---bar", 'foo&nbsp;&#8212;bar', [2, :prevent_breaks]
100
+
83
101
  assert_rp_equal "foo--bar", 'foo&#8212;bar', 3
84
102
  assert_rp_equal "foo---bar", 'foo&#8211;bar', 3
85
103
  assert_rp_equal "foo----bar", 'foo----bar', 3
86
104
  assert_rp_equal "--foo--bar--quux--",
87
105
  '&#8212;foo&#8212;bar&#8212;quux&#8212;', 3
106
+
107
+ assert_rp_equal "foo--bar", 'foo&#65279;&#8212;bar', [3, :prevent_breaks]
108
+ assert_rp_equal "foo --bar", 'foo &#8212;bar', 3
109
+ assert_rp_equal "foo --bar", 'foo&nbsp;&#8212;bar', [3, :prevent_breaks]
110
+ assert_rp_equal "foo -- bar", 'foo&nbsp;&#8212; bar', [3, :prevent_breaks]
111
+ assert_rp_equal "foo --bar", 'foo&nbsp;&#8212;bar', [3, :prevent_breaks]
112
+
113
+ assert_rp_equal "foo---bar", 'foo&#65279;&#8211;bar', [3, :prevent_breaks]
114
+ assert_rp_equal "foo ---bar", 'foo &#8211;bar', 3
115
+ assert_rp_equal "foo ---bar", 'foo&nbsp;&#8211;bar', [3, :prevent_breaks]
116
+ assert_rp_equal "foo --- bar", 'foo&nbsp;&#8211; bar', [3, :prevent_breaks]
117
+ assert_rp_equal "foo ---bar", 'foo&nbsp;&#8211;bar', [3, :prevent_breaks]
88
118
  end
89
119
 
90
120
  def test_html_comments
91
121
  assert_verbatim "<!-- comment -->"
92
122
  assert_verbatim "<!-- <p>foo bar</p> -->"
123
+ assert_verbatim "<!-- <p>foo\nbar</p> -->"
93
124
  assert_rp_equal "--<!-- -- -->--", '&#8211;<!-- -- -->&#8211;'
94
125
  end
95
126
 
@@ -100,20 +131,33 @@ EOF
100
131
  end
101
132
 
102
133
  def test_ellipses
103
- assert_rp_equal "foo..bar", 'foo..bar'
104
- assert_rp_equal "foo...bar", 'foo&#8230;bar'
105
- assert_rp_equal "foo....bar", 'foo....bar'
134
+ assert_rp_equal "foo..bar", 'foo..bar', [:ellipses]
135
+ assert_rp_equal "foo...bar", 'foo&#8230;bar', [:ellipses]
136
+ assert_rp_equal "foo....bar", 'foo....bar', [:ellipses]
137
+ # and with :prevent_breaks
138
+ assert_rp_equal "foo..bar", 'foo..bar', [:ellipses, :prevent_breaks]
139
+ assert_rp_equal "foo...bar", 'foo&#65279;&#8230;bar', [:ellipses, :prevent_breaks]
140
+ assert_rp_equal "foo....bar", 'foo....bar', [:ellipses, :prevent_breaks]
106
141
 
107
142
  # dots and spaces
108
- assert_rp_equal "foo. . .bar", 'foo&#8230;bar'
109
- assert_rp_equal "foo . . . bar", 'foo &#8230; bar'
110
- assert_rp_equal "foo. . . .bar", 'foo. . . .bar'
111
- assert_rp_equal "foo . . . . bar", 'foo . . . . bar'
112
-
113
- # Nasty ones
114
- assert_rp_equal "foo. . ..bar", 'foo. . ..bar'
115
- assert_rp_equal "foo. . ...bar", 'foo. . &#8230;bar'
116
- assert_rp_equal "foo. . ....bar", 'foo. . ....bar'
143
+ assert_rp_equal "foo. . .bar", 'foo&#8230;bar', [:ellipses]
144
+ assert_rp_equal "foo . . . bar", 'foo &#8230; bar', [:ellipses]
145
+ assert_rp_equal "foo. . . .bar", 'foo. . . .bar', [:ellipses]
146
+ assert_rp_equal "foo . . . . bar", 'foo . . . . bar', [:ellipses]
147
+ # and with :prevent_breaks
148
+ assert_rp_equal "foo. . .bar", 'foo&#65279;&#8230;bar', [:ellipses, :prevent_breaks]
149
+ assert_rp_equal "foo . . . bar", 'foo&nbsp;&#8230; bar', [:ellipses, :prevent_breaks]
150
+ assert_rp_equal "foo. . . .bar", 'foo. . . .bar', [:ellipses, :prevent_breaks]
151
+ assert_rp_equal "foo . . . . bar", 'foo . . . . bar', [:ellipses, :prevent_breaks]
152
+
153
+ # nasty ones
154
+ assert_rp_equal "foo. . ..bar", 'foo. . ..bar', [:ellipses]
155
+ assert_rp_equal "foo. . ...bar", 'foo. . &#8230;bar', [:ellipses]
156
+ assert_rp_equal "foo. . ....bar", 'foo. . ....bar', [:ellipses]
157
+ # and with :prevent_breaks
158
+ assert_rp_equal "foo. . ..bar", 'foo. . ..bar', [:ellipses, :prevent_breaks]
159
+ assert_rp_equal "foo. . ...bar", 'foo. .&nbsp;&#8230;bar', [:ellipses, :prevent_breaks]
160
+ assert_rp_equal "foo. . ....bar", 'foo. . ....bar', [:ellipses, :prevent_breaks]
117
161
  end
118
162
 
119
163
  def test_backticks
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubypants
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.5.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Gruber
@@ -12,7 +12,7 @@ authors:
12
12
  autorequire:
13
13
  bindir: bin
14
14
  cert_chain: []
15
- date: 2016-08-06 00:00:00.000000000 Z
15
+ date: 2016-10-02 00:00:00.000000000 Z
16
16
  dependencies:
17
17
  - !ruby/object:Gem::Dependency
18
18
  name: minitest
@@ -67,7 +67,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
67
67
  version: '0'
68
68
  requirements: []
69
69
  rubyforge_project:
70
- rubygems_version: 2.4.8
70
+ rubygems_version: 2.5.1
71
71
  signing_key:
72
72
  specification_version: 4
73
73
  summary: RubyPants is a Ruby port of the smart-quotes library SmartyPants.