typohero 0.0.3 → 0.0.4

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
  SHA1:
3
- metadata.gz: 6e1601fa507395ff5380983659eb920e745028be
4
- data.tar.gz: 9d4b8a981513c84ed843cdacc24956839ca18959
3
+ metadata.gz: 13d3687a6f31edadcea2ff02592d22225f5b0b19
4
+ data.tar.gz: 78d20a873b169e808800dbf6158ad93889f750d7
5
5
  SHA512:
6
- metadata.gz: bcb638d198532592fafe7dede571f585c70d430b64a2a5814bcddadddda4955943c3db9f83caf216b4bfcb692d9c0c1109098b00c9085138c24de773e3d97b8a
7
- data.tar.gz: c1e3e1f076b1cf66c12c6263de789c2d0ab755a020f436fb5b282205aab1773f6832f98ce8bea8541f0f6f90be15533f301b13e06b418f40a290d1c60b8d69a4
6
+ metadata.gz: 2404afeb00889cbbe73d21e5b64fc18cbcd74d7cf568baaa1eb9188f491876bfb9e618a46d701698b12f949d28ba0fb560e48460b574b638d83784ed63078b16
7
+ data.tar.gz: eb7d1528137c7a91a4f67b8a96e1295e8b72735bdf11faa0e7eb33ede2ae892b88ff09440fd41e9849982331a15b15d37b11ab4b06e91c29d3834296038dc772
data/lib/typohero.rb CHANGED
@@ -27,8 +27,8 @@ module TypoHero
27
27
  '\`' => '`',
28
28
  }
29
29
  UNESCAPE = Hash[ESCAPE.map {|k,v| [v,k[1..-1]] }]
30
- ESCAPE_RE = Regexp.union(*ESCAPE.keys)
31
- UNESCAPE_RE = Regexp.union(*UNESCAPE.keys)
30
+ ESCAPE_RE = Regexp.union ESCAPE.keys
31
+ UNESCAPE_RE = Regexp.union UNESCAPE.keys
32
32
 
33
33
  NBSP = "\u00a0"
34
34
  NBSP_THIN = "\u202F"
@@ -68,8 +68,8 @@ module TypoHero
68
68
  '–' => NDASH,
69
69
  '—' => MDASH
70
70
  }
71
- SPECIAL_RE = Regexp.union(*SPECIAL.keys)
72
- LATEX_RE = /(#{Regexp.union *LATEX.keys})(?=\p{Space}|$)/m
71
+ SPECIAL_RE = Regexp.union SPECIAL.keys
72
+ LATEX_RE = /(#{Regexp.union LATEX.keys})(?=\p{Space}|$)/m
73
73
 
74
74
  DASH_RE = "[#{MDASH}#{NDASH}]"
75
75
  AMP_RE = '&(?:amp;)?'
@@ -83,6 +83,9 @@ module TypoHero
83
83
  }
84
84
  ORDINAL_RE = /(?<=\d)(st|nd|rd|th)(?=\p{Space}|$)/
85
85
 
86
+ UNITS_RE = /(?<=\p{Space}|^)(-?\d+(?:\.\d+)?)\p{Space}*([mk]?m|km\/h|m\/s|[m]?l|[mk]g)(?=\p{Space}|\p{Punct}|$)/
87
+ UNITS = "\\1#{NBSP_THIN}\\2"
88
+
86
89
  MDASH_SPACE_RE = /\p{Space}*#{MDASH}\p{Space}*/
87
90
  NDASH_SPACE_RE = /\p{Space}*#{NDASH}\p{Space}*/
88
91
  MDASH_SPACE = "#{NBSP_THIN}#{MDASH}#{NBSP_THIN}"
@@ -90,10 +93,6 @@ module TypoHero
90
93
 
91
94
  REPLACE_AMP_RE = /(?<=\p{Space})#{AMP_RE}(?=\p{Space})/
92
95
 
93
- CAPS_BEGIN_RE = "(^|\\p{Space}|#{LEFT_QUOTE_RE})"
94
- CAPS_INNER_RE = "(?:#{AMP_RE}|[A-Z\\d\\.]|#{RSQUO})*" # right quote for posession (e.g. JIMMY'S)
95
- CAPS_RE = /#{CAPS_BEGIN_RE}([A-Z\d]#{CAPS_INNER_RE}[A-Z]#{CAPS_INNER_RE}|[A-Z]#{CAPS_INNER_RE}[A-Z\d]#{CAPS_INNER_RE})/m
96
-
97
96
  RIGHT_QUOTE_RE = %r{
98
97
  ^['"](?=\p{Punct})\B| # Very first character is a closing quote followed by punctuation at a non-word-break
99
98
  (?<!^|#{DASH_RE}|\p{Space}|[\[\{\(\-])['"]| # Not after dash, space or opening parentheses
@@ -103,6 +102,9 @@ module TypoHero
103
102
  '(?=(\d\d(?:s|\p{Space}|$))) # Decade abbreviations (the '80s)
104
103
  }xm
105
104
 
105
+ CAPS_INNER_RE = "(?:#{AMP_RE}|[A-Z\\d\\.\-]|#{RSQUO})*" # right quote for posession (e.g. JIMMY'S)
106
+ CAPS_RE = /(?<=^|\p{Space}|#{LEFT_QUOTE_RE})([A-Z\d]#{CAPS_INNER_RE}[A-Z]#{CAPS_INNER_RE}|[A-Z]#{CAPS_INNER_RE}[A-Z\d]#{CAPS_INNER_RE})(?=$|\p{Space}|#{RIGHT_QUOTE_RE}|\p{Punct})/m
107
+
106
108
  LEFT_QUOTES = {
107
109
  "'" => LSQUO,
108
110
  '"' => LDQUO,
@@ -133,7 +135,7 @@ module TypoHero
133
135
  }
134
136
 
135
137
  def tokenize(input)
136
- comment, excluded, latex, dollar = false, 0, 0, 0
138
+ excluded, latex, dollar = 0, 0, 0
137
139
  input.scan TOKENIZER_RE do |s|
138
140
  type =
139
141
  if s =~ /\A<!--/
@@ -198,7 +200,7 @@ module TypoHero
198
200
  def truncate(input, *max_words_or_separator)
199
201
  max_words = max_words_or_separator.select {|i| Fixnum === i }.first
200
202
  if separator = max_words_or_separator.reject {|i| Fixnum === i }.first
201
- separator = Regexp.union(*separator) unless Regexp === separator
203
+ separator = Regexp.union(separator) unless Regexp === separator
202
204
  separator = nil unless input =~ separator
203
205
  end
204
206
  out, tail, truncated = '', '', false
@@ -254,6 +256,7 @@ module TypoHero
254
256
  last_char = s[-1]
255
257
  decode(s)
256
258
  escape(s)
259
+ units(s)
257
260
  primes(s)
258
261
  special(s)
259
262
  latex(s)
@@ -343,7 +346,7 @@ module TypoHero
343
346
  end
344
347
 
345
348
  def caps(s)
346
- s.gsub!(CAPS_RE, '\1<span class="caps">\2</span>')
349
+ s.gsub!(CAPS_RE, '<span class="caps">\1</span>')
347
350
  end
348
351
 
349
352
  def initial_quotes(s)
@@ -351,7 +354,7 @@ module TypoHero
351
354
  end
352
355
 
353
356
  def nobr(s)
354
- s.gsub!(/[\p{Digit}\p{Word}]+(-[\p{Digit}\p{Word}]+)+/, '<span class="nobr">\0</span>')
357
+ s.gsub!(/[\p{Word}]+(-[\p{Word}]+)+/, '<span class="nobr">\0</span>')
355
358
  end
356
359
 
357
360
  def primes(s)
@@ -375,4 +378,8 @@ module TypoHero
375
378
  s.gsub!(RIGHT_QUOTE_RE, RIGHT_QUOTES)
376
379
  s.gsub!(/['"]/, LEFT_QUOTES)
377
380
  end
381
+
382
+ def units(s)
383
+ s.gsub!(UNITS_RE, UNITS)
384
+ end
378
385
  end
@@ -1,3 +1,3 @@
1
1
  module TypoHero
2
- VERSION = '0.0.3'
2
+ VERSION = '0.0.4'
3
3
  end
@@ -1,4 +1,5 @@
1
1
  # -*- coding: utf-8 -*-
2
+ $VERBOSE = true
2
3
  require 'minitest/autorun'
3
4
  require 'typohero'
4
5
 
@@ -161,6 +162,7 @@ multiline
161
162
 
162
163
  def test_should_replace_caps
163
164
  assert_enhance "A message from KU", 'A message from&nbsp;<span class="caps">KU</span>'
165
+ assert_enhance "DG-1000", '<span class="caps"><span class="nobr">DG-1000</span></span>'
164
166
  assert_enhance 'Replace text <a href=".">IN</a> tags', 'Replace text <a href="."><span class="caps">IN</span></a>&nbsp;tags'
165
167
  assert_enhance 'Replace text <i>IN</i> tags', 'Replace text <i><span class="caps">IN</span></i>&nbsp;tags'
166
168
  assert_enhance 'AB, CD, EF', '<span class="caps">AB</span>, <span class="caps">CD</span>,&nbsp;<span class="caps">EF</span>'
@@ -253,6 +255,17 @@ multiline
253
255
  assert_enhance 'I am the 1st', 'I am the&nbsp;1<span class="ord">st</span>'
254
256
  end
255
257
 
258
+ def test_units
259
+ assert_enhance '-1m', "-1\u202Fm"
260
+ assert_enhance '1m', "1\u202Fm"
261
+ assert_enhance '100 m.', "100\u202Fm."
262
+ assert_enhance '1.2 m', "1.2\u202Fm"
263
+ assert_enhance '1.2 km', "1.2\u202Fkm"
264
+ assert_enhance '1.2 km/h', "1.2\u202Fkm/h"
265
+ assert_enhance 'text 1m text', "text 1\u202Fm text"
266
+ assert_enhance 'text 1.2 m text', "text 1.2\u202Fm text"
267
+ end
268
+
256
269
  def test_latex
257
270
  assert_enhance '\\textbackslash', '\\'
258
271
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: typohero
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Mendler
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-09-09 00:00:00.000000000 Z
11
+ date: 2014-11-09 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: TypoHero improves web typography by applying various filters (similar
14
14
  to rubypants, typogruby, typogrify).