typefactory 0.0.11 → 0.0.12

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: c9f5fcbeb7da610147172db87669cf14d2276f78
4
- data.tar.gz: aa9b1fe52967584ebf7e7152f6de9ae22c86f58b
3
+ metadata.gz: c2fe21cb2ab6aa014e67f167bfe81b267ef4c40c
4
+ data.tar.gz: d73ee014c70f97d1c22c2a15fb1a19308e524241
5
5
  SHA512:
6
- metadata.gz: 7319b9cafe6b6c17b7d316b3db07a6d90e9bc271503fb821b7eba70d2ebbf73924afb835313b79c86754d7a4a9a140d257ffdfba5105b6b56f8ea58c9ad39ce1
7
- data.tar.gz: 5ffdebc7571581a5580278b50ba0aca1dbf55b421dbac0150008dd2b1eb26816642aec46834617cb42f9d33ac59059c0fec2dfbc018a133297cdaad5b9978036
6
+ metadata.gz: 6a003ce61ed456fcc81d4d7c9fc25f0c91429995c01e37975c3af3545be14f8a047060107ea0480deb14616716afe60e6924889fe1e7f09d8c0c1dbc96e25a6a
7
+ data.tar.gz: f419813e8aca8619caae749a68c93fd2c531963ecd5294e2234d1bd07a38ae6455b9cdb2847c848d56b2803425775f261d6c98766faff691e21740ef64640c02
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ -- color
data/README.md CHANGED
@@ -27,7 +27,9 @@ Typefactory adds method `prepare` for standard Ruby class `String`. You can use
27
27
  ## Customize
28
28
 
29
29
  Need customization? Please use generator:
30
+
30
31
  $ rails generate typefactory config
32
+
31
33
  Now you can modify `config/initializers/typefactory.rb`
32
34
 
33
35
  ## Help to improve
data/lib/typefactory.rb CHANGED
@@ -5,40 +5,44 @@ module Typefactory
5
5
 
6
6
  autoload :Processor, 'typefactory/processor'
7
7
 
8
- @@quote_marks = [
9
- { :left => '«', :right => '»' },
10
- { :left => '„', :right => '“' },
11
- { :left => '‘', :right => '’' }
12
- ]
13
- mattr_accessor :quote_marks
14
-
15
- @@glyphs = {
16
- nbsp: { mark: ' ', sign: ' ', letter_code: ' ', digital_code: ' ' },
17
- quot: { mark: '"', sign: '"', letter_code: '"', digital_code: '"' },
18
- mdash: { mark: '-', sign: '—', letter_code: '—', digital_code: '—' }
19
- # :nbsp => ' ',
20
- # :ndash => '',
21
- # :mdash => '—',
22
- # :minus => '−',
23
- # :section => '§',
24
- # :paragraph => '',
25
- # :copy => '©',
26
- # :registered => '®',
27
- # :trademark => '™',
28
- # :degree => '°',
29
- # :inch => '',
30
- # :multiple => '×',
31
- # :middot => '·',
32
- # :bullit => '•',
33
- # :quot => '"'
8
+ MODE = :entity
9
+
10
+ LOCALE = :ru
11
+
12
+ QUOTES = {
13
+ ru: [
14
+ {
15
+ left: { mark: '"', symbol: '«', entity: '«', decimal: '«' },
16
+ right: { mark: '"', symbol: '»', entity: '»', decimal: '»' }
17
+ },
18
+ {
19
+ left: { mark: '"', symbol: '„', entity: '„', decimal: '„' },
20
+ right: { mark: '"', symbol: '“', entity: '“', decimal: '“' }
21
+ },
22
+ {
23
+ left: { mark: '"', symbol: '‘', entity: '‘', decimal: '‘' },
24
+ right: { mark: '"', symbol: '’', entity: '’', decimal: '’' }
25
+ }
26
+ ],
27
+ en: [
28
+ {
29
+ left: { mark: '"', symbol: '“', entity: '“', decimal: '“' },
30
+ right: { mark: '"', symbol: '”', entity: '”', decimal: '”' }
31
+ },
32
+ {
33
+ left: { mark: '"', symbol: '‘', entity: '‘', decimal: '‘' },
34
+ right: { mark: '"', symbol: '’', entity: '’', decimal: '’' }
35
+ }
36
+ ]
34
37
  }
35
- mattr_accessor :glyphs
36
38
 
37
- def self.setup
38
- yield self
39
- end
39
+ GLYPHS = {
40
+ nbsp: { mark: ' ', symbol: ' ', entity: ' ', decimal: ' ' },
41
+ quot: { mark: '"', symbol: '"', entity: '"', decimal: '"' },
42
+ mdash: { mark: '-', symbol: '—', entity: '—', decimal: '—' },
43
+ copy: { mark: '(c)', symbol: '©', entity: '©', decimal: '©' }
44
+ }
40
45
 
41
- # @note In next versions here will be `settings`
42
46
  def self.prepare(text)
43
47
  Processor.new(text).prepare
44
48
  end
@@ -4,51 +4,54 @@ module Typefactory
4
4
 
5
5
  @buffer = nil
6
6
 
7
- # @param original [String] a text for preparing
8
7
  def initialize(original)
9
8
  @buffer = original
10
9
  end
11
10
 
12
- # @return [String] web-prepared text
13
11
  def prepare
14
12
  cleanup
15
- escape_html
16
- quotes
17
- descape_html
18
- emdashes
19
- short_words
13
+ preserve_html
14
+ process_quotes
15
+ revive_html
16
+ process_emdaches
17
+ process_short_words
18
+
20
19
  @buffer
21
20
  end
22
21
 
23
22
  private
24
23
 
25
- # Purging text from already replaced glyphs (includes quotation marks of whole levels)
26
24
  def cleanup
27
-
28
- # Replacing all special chars to human-like marks
29
- Typefactory::glyphs.each do |glyph, settings|
30
- @buffer.gsub!(/#{settings[:sign]}|#{settings[:letter_code]}|#{settings[:digital_code]}/, settings[:mark])
25
+ GLYPHS.each do |key, glyph|
26
+ @buffer.gsub!(/#{glyph[:entity]}|#{glyph[:decimal]}|#{glyph[:symbol]}/) do
27
+ glyph[:mark]
28
+ end
31
29
  end
32
30
 
33
- # Replacing quote marks
34
- expression = String.new
35
- Typefactory::quote_marks.each do |mark|
36
- expression += "#{mark[:left]}|#{mark[:right]}|"
31
+ expression = ''
32
+ QUOTES.each_pair do |locale, quotes|
33
+ quotes.each do |level|
34
+ level.each do |side, glyph|
35
+ expression += "#{glyph[:symbol]}|#{glyph[:entity]}|#{glyph[:decimal]}|"
36
+ end
37
+ end
37
38
  end
38
- @buffer.gsub!(/#{expression[0..-2]}/, Typefactory::glyphs[:quot][:mark])
39
-
39
+ @buffer.gsub!(/#{expression[0..-2]}/, GLYPHS[:quot][:mark])
40
40
  end
41
41
 
42
- def escape_html
43
- @buffer.gsub!(/<([^\/].*?)>/) { |block| block.gsub(/"/, '\'') }
42
+ def preserve_html
43
+ @buffer.gsub!(/<([^\/].*?)>/) do |tag|
44
+ tag.gsub(/"/, '\'')
45
+ end
44
46
  end
45
47
 
46
- def descape_html
47
- @buffer.gsub!(/<([^\/].*?)>/) { |block| block.gsub(/'/, '"') }
48
+ def revive_html
49
+ @buffer.gsub!(/<([^\/].*?)>/) do |tag|
50
+ tag.gsub(/'/, '"')
51
+ end
48
52
  end
49
53
 
50
- # Quotes
51
- def quotes
54
+ def process_quotes
52
55
  result = String.new
53
56
  level = -1
54
57
  @buffer.split('').each_with_index do |character, index|
@@ -56,10 +59,12 @@ module Typefactory
56
59
  side = quote_mark_side(index)
57
60
  if side == :left
58
61
  level += 1
59
- result += Typefactory::quote_marks[level][side]
62
+ level = QUOTES[LOCALE].length - 1 if level > QUOTES[LOCALE].length - 1
63
+ result += QUOTES[LOCALE][level][side][MODE]
60
64
  else
61
- result += Typefactory::quote_marks[level][side]
65
+ result += QUOTES[LOCALE][level][side][MODE]
62
66
  level -= 1
67
+ level = -1 if level < -1
63
68
  end
64
69
  else
65
70
  result += character
@@ -104,12 +109,16 @@ module Typefactory
104
109
  end
105
110
  end
106
111
 
107
- def short_words
108
- @buffer.gsub!(/\s([a-zA-Z,а-яА-Я]{1,2})\s/) { |word| " #{$1}#{Typefactory::glyphs[:nbsp][:sign]}" }
112
+ def process_short_words
113
+ @buffer.gsub!(/(\s)([a-z,A-Z,а-я,А-Я]{1,2})\s(\S)/) do
114
+ "#{$1}#{$2}#{GLYPHS[:nbsp][MODE]}#{$3}"
115
+ end
109
116
  end
110
117
 
111
- def emdashes
112
- @buffer.gsub!(/\s\-\s/, "#{Typefactory::glyphs[:nbsp][:sign]}#{Typefactory::glyphs[:mdash][:sign]} ")
118
+ def process_emdaches
119
+ @buffer.gsub!(/\s\-\s/) do
120
+ "#{GLYPHS[:nbsp][MODE]}#{GLYPHS[:mdash][MODE]} "
121
+ end
113
122
  end
114
123
 
115
124
  end
@@ -1,3 +1,3 @@
1
1
  module Typefactory
2
- VERSION = '0.0.11'
2
+ VERSION = '0.0.12'
3
3
  end
@@ -5,11 +5,12 @@ RSpec.describe 'Typefactory' do
5
5
  describe 'Core' do
6
6
  it 'Parameters can be updated successfully' do
7
7
  Typefactory.setup do |config|
8
- config.quote_marks = [
9
- { :left => '«', :right => '»' },
10
- { :left => '', :right => '' },
11
- { :left => '', :right => '' }
12
- ]
8
+ config.locale = :ru
9
+ # config.quote_marks = [
10
+ # { :left => '«', :right => '»' },
11
+ # { :left => '', :right => '' },
12
+ # { :left => '‘', :right => '’' }
13
+ # ]
13
14
  expect(config.quote_marks.class).to eq(Array)
14
15
  expect(config.quote_marks[2][:right]).to eq('’')
15
16
  end
@@ -52,6 +53,13 @@ RSpec.describe 'Typefactory' do
52
53
  example = 'Едет Санта на оленях'
53
54
  expect(example.prepare).to eq('Едет Санта на оленях')
54
55
  end
56
+
57
+ it 'Processing demo paragraph' do
58
+ example = <<eot
59
+ <p>&laquo;Можем "Демо" однозначно констатировать, что, по&nbsp;данным Системы контроля космического пространства Российской Федерации, в&nbsp;период с&nbsp;10:00 до&nbsp;13:00 мск (09:00 до&nbsp;12:00 укр) 12, 16, 17, и&nbsp;18 июля украинские спутники &bdquo;Сич-1&ldquo; и&nbsp;&bdquo;Сич-2&ldquo; над указанными территориями не&nbsp;пролетали. В&nbsp;обозначенное на&nbsp;снимках время над зоной катастрофы находился американский спутник оптико-электронной разведки типа &bdquo;Кихоул&ldquo; (KeyHole). Таким образом, источник получения фотоосновы для дальнейшей обработки СБУ не&nbsp;должен вызывать ни&nbsp;у кого сомнений&raquo;,&nbsp;&mdash; замечает Минобороны.</p>
60
+ eot
61
+ puts example.prepare
62
+ end
55
63
  end
56
64
 
57
65
  describe 'Glyphs' do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: typefactory
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.11
4
+ version: 0.0.12
5
5
  platform: ruby
6
6
  authors:
7
7
  - Evgeny Boyko
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-08-01 00:00:00.000000000 Z
12
+ date: 2014-08-02 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -93,6 +93,7 @@ extensions: []
93
93
  extra_rdoc_files: []
94
94
  files:
95
95
  - ".gitignore"
96
+ - ".rspec"
96
97
  - ".yardopts"
97
98
  - Gemfile
98
99
  - LICENSE.txt