improve_typography 0.1.13 → 0.1.16

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
  SHA256:
3
- metadata.gz: a15353e5f98379ad53b4cba25f1bb06a9e871d87525400cbcaef028288a3b7cb
4
- data.tar.gz: 6449e0f1ef011b5178d8a9a9429b7854d22875e44048f191313d11bfe1bd9163
3
+ metadata.gz: d6d3cfe8e809e1e21fcc9eb90c85e6eab6704d96160dab03e3708a88981ac0e9
4
+ data.tar.gz: 2e94d39063ec4ecadbdb034015adca8f70c3c6aae9167756696a38305b80a620
5
5
  SHA512:
6
- metadata.gz: 5263165ffa75b7aad4ae268c52cd2f7db550997c9880a645db074ef16756990abb77c45333db1cc43029fc462735894c517377bfecadf81d091b12027848e0f3
7
- data.tar.gz: 99035ac8f242688c1b3171672ec16e63045d60f5d702bc0be3b561c8a343bfee1792e1d97702dbddc8e62ed6c33a55d9dc0816c3be869345540f7bc988026f9a
6
+ metadata.gz: f4104301d85835ceabe273e522c690dfdd24d19ee644fc98d2864fa2261c241ac0a705e622c7d741fedff1aa633270d4e8fd05b4dafc18a46272c6091939f771
7
+ data.tar.gz: 559c315c4eef1bd0ea29634d84aa02c9d9560119c5cd5cf135ba6147fd5cdced0038dbf71cf724aab645005f7df6f48a1765d14161255be0f83a00b5ec3255c4
data/.travis.yml CHANGED
@@ -2,7 +2,7 @@ sudo: false
2
2
  language: ruby
3
3
  script: 'bundle exec rake'
4
4
  rvm:
5
- - 2.2.5
5
+ - 2.5.1
6
6
  before_install:
7
7
  - gem install bundler -v 1.12.5
8
8
  notifications:
data/CHANGELOG.md CHANGED
@@ -1,5 +1,18 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 0.1.16
4
+
5
+ * guard for missing `i18n` translations (@asgerb)
6
+ * refactor inheritance logic (@asgerb)
7
+
8
+ ## 0.1.15
9
+
10
+ * fix the `ImproveTypography.configure` method
11
+
12
+ ## 0.1.14
13
+
14
+ * first run simplest `match?` before `gsub` to make sure the replacement is actually needed
15
+
3
16
  ## 0.1.13
4
17
 
5
18
  * skip processing if no processor classes defined
data/README.md CHANGED
@@ -74,7 +74,7 @@ You can configure applied processors (and their order).
74
74
 
75
75
  ```ruby
76
76
  ImproveTypography.configure do |config|
77
- processors = [ImproveTypography::Processors::Ellipsis]
77
+ config.processors = [ImproveTypography::Processors::Ellipsis]
78
78
  end
79
79
  ```
80
80
 
@@ -24,9 +24,9 @@ Gem::Specification.new do |spec|
24
24
  spec.add_dependency 'i18n'
25
25
  spec.add_dependency 'nokogiri'
26
26
 
27
- spec.add_development_dependency 'bundler', '~> 1.12'
27
+ spec.add_development_dependency 'bundler'
28
28
  spec.add_development_dependency 'guard'
29
29
  spec.add_development_dependency 'guard-minitest'
30
30
  spec.add_development_dependency 'minitest', '~> 5.0'
31
- spec.add_development_dependency 'rake', '~> 10.0'
31
+ spec.add_development_dependency 'rake', '~> 13.0'
32
32
  end
@@ -0,0 +1,2 @@
1
+ da:
2
+ foo: bar
@@ -2,8 +2,20 @@ require 'nokogiri'
2
2
 
3
3
  module ImproveTypography
4
4
  class Base < Struct.new(:str, :options)
5
- def self.call(*args)
6
- new(*args).call
5
+ class << self
6
+ def all_processor_classes
7
+ @@all_processor_classes ||= []
8
+ end
9
+
10
+ def add_processor_class(klass)
11
+ unless all_processor_classes.include?(klass)
12
+ all_processor_classes << klass
13
+ end
14
+ end
15
+
16
+ def call(*args)
17
+ new(*args).call
18
+ end
7
19
  end
8
20
 
9
21
  def initialize(str, options = {})
@@ -30,12 +42,8 @@ module ImproveTypography
30
42
  @doc ||= Nokogiri::HTML::DocumentFragment.parse("<div>#{str}</div>")
31
43
  end
32
44
 
33
- def configuration
34
- @configuration ||= Configuration.new
35
- end
36
-
37
45
  def processors
38
- options.fetch(:processors, configuration.processors)
46
+ options.fetch(:processors, ImproveTypography.configuration.processors)
39
47
  end
40
48
 
41
49
  def processor_classes
@@ -45,7 +53,7 @@ module ImproveTypography
45
53
  end
46
54
 
47
55
  def all_processor_classes
48
- @all_processor_classes ||= ObjectSpace.each_object(Class).select { |klass| klass < Processor }
56
+ self.class.all_processor_classes
49
57
  end
50
58
 
51
59
  def processor_for_locale(klass)
@@ -1,7 +1,14 @@
1
1
  module ImproveTypography
2
2
  class Processor < Struct.new(:str, :options)
3
- def self.call(*args)
4
- new(*args).call
3
+ class << self
4
+ def call(*args)
5
+ new(*args).call
6
+ end
7
+
8
+ def inherited(klass)
9
+ ImproveTypography::Base.add_processor_class(klass)
10
+ super
11
+ end
5
12
  end
6
13
 
7
14
  def initialize(str, options = {})
@@ -14,6 +21,14 @@ module ImproveTypography
14
21
 
15
22
  private
16
23
 
24
+ def translation(key)
25
+ I18n.t(key, scope: %i(improve_typography), locale: locale)
26
+ end
27
+
28
+ def sign_exists?(sign)
29
+ !sign.nil? && !sign.match?(/translation missing/)
30
+ end
31
+
17
32
  def configuration
18
33
  @configuration ||= Configuration.new
19
34
  end
@@ -4,6 +4,8 @@ module ImproveTypography
4
4
  REGEXP = /(\w+)'(\w*)/i
5
5
 
6
6
  def call
7
+ return str unless sign_exists?(apostrophe_sign)
8
+ return str unless str.match?(/'/)
7
9
  replace_apostrophe
8
10
  end
9
11
 
@@ -14,7 +16,7 @@ module ImproveTypography
14
16
  end
15
17
 
16
18
  def apostrophe_sign
17
- options.fetch(:apostrophe_sign, I18n.t(:apostrophe_sign, scope: %i(improve_typography), locale: locale))
19
+ options.fetch(:apostrophe_sign, translation(:apostrophe_sign))
18
20
  end
19
21
  end
20
22
  end
@@ -2,6 +2,8 @@ module ImproveTypography
2
2
  module Processors
3
3
  class DoubleQuotes < Processor
4
4
  def call
5
+ return str unless sign_exists?(double_quotes)
6
+ return str unless str.match?(/[\"#{double_quotes[0]}#{double_quotes[1]}]/)
5
7
  replace_double_quotes
6
8
  end
7
9
 
@@ -16,7 +18,7 @@ module ImproveTypography
16
18
  end
17
19
 
18
20
  def double_quotes
19
- options.fetch(:double_quotes, I18n.t(:double_quotes, scope: %i(improve_typography), locale: locale))
21
+ options.fetch(:double_quotes, translation(:double_quotes))
20
22
  end
21
23
  end
22
24
  end
@@ -4,7 +4,8 @@ module ImproveTypography
4
4
  REGEXP = /(\.\s*?){3,}/i
5
5
 
6
6
  def call
7
- return str unless ellipsis_sign
7
+ return str unless sign_exists?(ellipsis_sign)
8
+ return str unless str.match?(REGEXP)
8
9
 
9
10
  str.gsub(REGEXP, ellipsis_sign)
10
11
  end
@@ -12,7 +13,7 @@ module ImproveTypography
12
13
  private
13
14
 
14
15
  def ellipsis_sign
15
- options.fetch(:ellipsis_sign, I18n.t(:ellipsis_sign, scope: %i(improve_typography), locale: locale))
16
+ options.fetch(:ellipsis_sign, translation(:ellipsis_sign))
16
17
  end
17
18
  end
18
19
  end
@@ -4,13 +4,15 @@ module ImproveTypography
4
4
  REGEXP = /(\w+?)\s+-{1,3}\s+(\w+?)/i
5
5
 
6
6
  def call
7
+ return str unless sign_exists?(em_dash_sign)
8
+ return str unless str.match?(/-{1,3}/)
7
9
  str.gsub(REGEXP, '\1'+em_dash_sign+'\2')
8
10
  end
9
11
 
10
12
  private
11
13
 
12
14
  def em_dash_sign
13
- options.fetch(:em_dash_sign, I18n.t(:em_dash_sign, scope: %i(improve_typography), locale: locale))
15
+ options.fetch(:em_dash_sign, translation(:em_dash_sign))
14
16
  end
15
17
  end
16
18
  end
@@ -4,13 +4,15 @@ module ImproveTypography
4
4
  REGEXP = /(\w+?)\s+-{1,2}\s+(\w+?)/i
5
5
 
6
6
  def call
7
+ return str unless sign_exists?(en_dash_sign)
8
+ return str unless str.match?(/-{1,3}/)
7
9
  str.gsub(REGEXP, '\1'+en_dash_sign+'\2')
8
10
  end
9
11
 
10
12
  private
11
13
 
12
14
  def en_dash_sign
13
- options.fetch(:en_dash_sign, I18n.t(:en_dash_sign, scope: %i(improve_typography), locale: locale))
15
+ options.fetch(:en_dash_sign, translation(:en_dash_sign))
14
16
  end
15
17
  end
16
18
  end
@@ -4,7 +4,8 @@ module ImproveTypography
4
4
  REGEXP = /(\d+|[½⅓¼⅔⅛⅜⅝⅞])(\s*)x(\s*)(\d+|[½⅓¼⅔⅛⅜⅝⅞])/i
5
5
 
6
6
  def call
7
- return str unless multiply_sign
7
+ return str unless sign_exists?(multiply_sign)
8
+ return str unless str.match?(REGEXP)
8
9
 
9
10
  str.gsub(REGEXP, '\1'+multiply_sign+'\4')
10
11
  end
@@ -12,7 +13,7 @@ module ImproveTypography
12
13
  private
13
14
 
14
15
  def multiply_sign
15
- options.fetch(:multiply_sign, I18n.t(:multiply_sign, scope: %i(improve_typography), locale: locale))
16
+ options.fetch(:multiply_sign, translation(:multiply_sign))
16
17
  end
17
18
  end
18
19
  end
@@ -2,6 +2,8 @@ module ImproveTypography
2
2
  module Processors
3
3
  class Nbsp < Processor
4
4
  def call
5
+ return str unless str.match?(/(\A|\s+)(\S|\d+)([\.?!]?)(\s+|\z)/)
6
+
5
7
  str
6
8
  .gsub(/(\s+)(\S|\d+|\S\.)(\s+)(\S)/, '\1\2&nbsp;\4') # in the middle of string
7
9
  .gsub(/\A(\w|\d+|\w\.)(\s+)(\S)/, '\1&nbsp;\3') # at the beginning of string
@@ -2,6 +2,8 @@ module ImproveTypography
2
2
  module Processors
3
3
  class SingleQuotes < Processor
4
4
  def call
5
+ return str unless sign_exists?(single_quotes)
6
+ return str unless str.match?(/[\'#{single_quotes[0]}#{single_quotes[1]}]/)
5
7
  replace_single_quotes
6
8
  end
7
9
 
@@ -16,7 +18,7 @@ module ImproveTypography
16
18
  end
17
19
 
18
20
  def single_quotes
19
- options.fetch(:single_quotes, I18n.t(:single_quotes, scope: %i(improve_typography), locale: locale))
21
+ options.fetch(:single_quotes, translation(:single_quotes))
20
22
  end
21
23
  end
22
24
  end
@@ -4,6 +4,7 @@ module ImproveTypography
4
4
  REGEXP = /(\d\s*)(m|mm|cm|km)(2|3)/
5
5
 
6
6
  def call
7
+ return str unless str.match?(REGEXP)
7
8
  str.gsub(REGEXP, '\1\2<sup>\3</sup>')
8
9
  end
9
10
  end
@@ -4,6 +4,7 @@ module ImproveTypography
4
4
  REGEXP = /\u2028/
5
5
 
6
6
  def call
7
+ return str unless str.match?(REGEXP)
7
8
  str.gsub(REGEXP, "\n")
8
9
  end
9
10
  end
@@ -1,3 +1,3 @@
1
1
  module ImproveTypography
2
- VERSION = '0.1.13'.freeze
2
+ VERSION = '0.1.16'.freeze
3
3
  end
@@ -8,6 +8,24 @@ require 'improve_typography/processor'
8
8
  Dir["#{File.dirname(__FILE__)}/improve_typography/processors/**/*.rb"].each { |f| require f }
9
9
 
10
10
  I18n::Backend::Simple.send(:include, I18n::Backend::Fallbacks)
11
- I18n.load_path += Dir.glob(File.join( File.dirname(__FILE__), 'config', 'locales', '*.yml' ))
11
+ I18n.load_path += Dir.glob(File.join(File.dirname(__FILE__), 'config', 'locales', '*.yml'))
12
12
 
13
13
  require 'improve_typography/version'
14
+
15
+ module ImproveTypography
16
+ class << self
17
+ attr_accessor :configuration
18
+ end
19
+
20
+ def self.configuration
21
+ @configuration ||= Configuration.new
22
+ end
23
+
24
+ def self.configuration=(config)
25
+ @configuration = config
26
+ end
27
+
28
+ def self.configure
29
+ yield configuration
30
+ end
31
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: improve_typography
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.13
4
+ version: 0.1.16
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tomas Celizna
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-08-12 00:00:00.000000000 Z
11
+ date: 2022-05-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: i18n
@@ -42,16 +42,16 @@ dependencies:
42
42
  name: bundler
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - "~>"
45
+ - - ">="
46
46
  - !ruby/object:Gem::Version
47
- version: '1.12'
47
+ version: '0'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - "~>"
52
+ - - ">="
53
53
  - !ruby/object:Gem::Version
54
- version: '1.12'
54
+ version: '0'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: guard
57
57
  requirement: !ruby/object:Gem::Requirement
@@ -100,14 +100,14 @@ dependencies:
100
100
  requirements:
101
101
  - - "~>"
102
102
  - !ruby/object:Gem::Version
103
- version: '10.0'
103
+ version: '13.0'
104
104
  type: :development
105
105
  prerelease: false
106
106
  version_requirements: !ruby/object:Gem::Requirement
107
107
  requirements:
108
108
  - - "~>"
109
109
  - !ruby/object:Gem::Version
110
- version: '10.0'
110
+ version: '13.0'
111
111
  description:
112
112
  email:
113
113
  - tomas.celizna@gmail.com
@@ -127,6 +127,7 @@ files:
127
127
  - bin/setup
128
128
  - improve_typography.gemspec
129
129
  - lib/config/locales/cs.yml
130
+ - lib/config/locales/da.yml
130
131
  - lib/config/locales/en.yml
131
132
  - lib/improve_typography.rb
132
133
  - lib/improve_typography/base.rb
@@ -163,8 +164,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
163
164
  - !ruby/object:Gem::Version
164
165
  version: '0'
165
166
  requirements: []
166
- rubyforge_project:
167
- rubygems_version: 2.7.6
167
+ rubygems_version: 3.1.4
168
168
  signing_key:
169
169
  specification_version: 4
170
170
  summary: Improves typography (quotes, hyphens, etc.) of a given string. Works well