Class: Typefactory::Processor
- Inherits:
-
Object
- Object
- Typefactory::Processor
- Defined in:
- lib/typefactory/processor.rb
Instance Method Summary collapse
- #characters_after(index, length) ⇒ String private
- #characters_before(index, length) ⇒ String private
-
#cleanup ⇒ Object
private
Очищает текст от уже расставленных глифов.
-
#initialize(original) ⇒ Processor
constructor
A new instance of Processor.
- #prepare(settings = nil) ⇒ String
-
#preserve_html ⇒ Object
private
Экранирует параметры тегов HTML.
-
#process_dashes ⇒ Object
private
Расставляет длинные тире.
-
#process_quotes ⇒ Object
private
Расставляет правильные многоуровневые кавычки.
-
#process_widows ⇒ Object
private
Расставляет неразрывные пробелы после слов длиной до трех символов.
-
#quote_mark_side(index) ⇒ Object
private
Определяет сторону кавычки на указаной позиции.
-
#revive_html ⇒ Object
private
Снимает экранирование с тегов HTML.
-
#space_after_position(index, length = 4) ⇒ Integer, Nil
private
Расстояние до ближайшего пробельного символа справа от текущей позиции.
-
#space_before_position(index, length = 4) ⇒ Integer, Nil
private
Расстояние до ближайшего пробельного символа слева от текущей позиции.
Constructor Details
#initialize(original) ⇒ Processor
Returns a new instance of Processor.
6 7 8 |
# File 'lib/typefactory/processor.rb', line 6 def initialize(original) @buffer = original end |
Instance Method Details
#characters_after(index, length) ⇒ String (private)
136 137 138 139 140 |
# File 'lib/typefactory/processor.rb', line 136 def characters_after(index, length) start_position = index+1 < @buffer.length ? index+1 : @buffer.length-1 end_position = index+length < @buffer.length ? index+length : @buffer.length-1 @buffer[start_position..end_position] end |
#characters_before(index, length) ⇒ String (private)
127 128 129 130 131 |
# File 'lib/typefactory/processor.rb', line 127 def characters_before(index, length) start_position = (index-length > 0) ? index-length : 0 end_position = (index-1 > 0) ? index-1 : 0 @buffer[start_position..end_position] end |
#cleanup ⇒ Object (private)
Очищает текст от уже расставленных глифов
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/typefactory/processor.rb', line 35 def cleanup Typefactory::glyphs.each do |key, glyph| @buffer.gsub!(/#{glyph[:entity]}|#{glyph[:decimal]}|#{glyph[:symbol]}/) do glyph[:mark] end end expression = '' Typefactory::quotes.each_pair do |locale, quotes| quotes.each do |level| level.each do |side, glyph| expression += "#{glyph[:symbol]}|#{glyph[:entity]}|#{glyph[:decimal]}|" end end end @buffer.gsub!(/#{expression[0..-2]}/, Typefactory::glyphs[:quot][:mark]) end |
#prepare(settings = nil) ⇒ String
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/typefactory/processor.rb', line 12 def prepare(settings = nil) @settings = settings.nil? ? Typefactory::settings : Typefactory::settings | settings cleanup if @settings.include?(:clean) and !@settings.include?(:no_clean) preserve_html process_quotes if @settings.include?(:quotes) and !@settings.include?(:no_quotes) revive_html process_dashes if @settings.include?(:dashes) and !@settings.include?(:no_dashes) process_widows if @settings.include?(:glue_widows) and !@settings.include?(:no_glue_widows) @buffer end |
#preserve_html ⇒ Object (private)
Экранирует параметры тегов HTML
54 55 56 57 58 |
# File 'lib/typefactory/processor.rb', line 54 def preserve_html @buffer.gsub!(/<([^\/].*?)>/) do |tag| tag.gsub(/"/, '\'') end end |
#process_dashes ⇒ Object (private)
Расставляет длинные тире
192 193 194 195 196 |
# File 'lib/typefactory/processor.rb', line 192 def process_dashes @buffer.gsub!(/\s\-\s/) do "#{Typefactory::glyphs[:nbsp][Typefactory::mode]}#{Typefactory::glyphs[:mdash][Typefactory::mode]} " end end |
#process_quotes ⇒ Object (private)
Расставляет правильные многоуровневые кавычки
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/typefactory/processor.rb', line 68 def process_quotes result = '' depth = -1 @buffer.split('').each_with_index do |char, index| if char == '"' side = quote_mark_side index if side == :left depth += 1 result += Typefactory::quote(depth, side) else result += Typefactory::quote(depth, side) depth -= 1 end else result += char end end @buffer.replace result end |
#process_widows ⇒ Object (private)
Расставляет неразрывные пробелы после слов длиной до трех символов
185 186 187 188 189 |
# File 'lib/typefactory/processor.rb', line 185 def process_widows @buffer.gsub!(/(\s)([a-z,A-Z,а-я,А-Я]{1,2})\s(\S)/) do "#{$1}#{$2}#{Typefactory::glyphs[:nbsp][Typefactory::mode]}#{$3}" end end |
#quote_mark_side(index) ⇒ Object (private)
Определяет сторону кавычки на указаной позиции
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/typefactory/processor.rb', line 91 def quote_mark_side(index) before = space_before_position index after = space_after_position index if !before.nil? and !after.nil? if before == after if index == 0 :left elsif index == @buffer.length-1 :right else if @buffer[index+1..index+1].scan(/[A-Za-zА-Яа-я0-9]/).any? :left else :right end end elsif before < after :left else :right end elsif !before.nil? and after.nil? :left elsif !after.nil? and before.nil? :right end end |
#revive_html ⇒ Object (private)
Снимает экранирование с тегов HTML
61 62 63 64 65 |
# File 'lib/typefactory/processor.rb', line 61 def revive_html @buffer.gsub!(/<([^\/].*?)>/) do |tag| tag.gsub(/'/, '"') end end |
#space_after_position(index, length = 4) ⇒ Integer, Nil (private)
Расстояние до ближайшего пробельного символа справа от текущей позиции
169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/typefactory/processor.rb', line 169 def space_after_position(index, length = 4) expression = /\s|\-|<|\.|,|!|\?|:|;|\)/ space_at = characters_after(index, length).index(expression) if space_at.nil? if index > @buffer.length-length-1 0 else nil end else space_at + 1 end end |
#space_before_position(index, length = 4) ⇒ Integer, Nil (private)
Расстояние до ближайшего пробельного символа слева от текущей позиции
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/typefactory/processor.rb', line 146 def space_before_position(index, length = 4) expression = /\s|\-|>|\(/ space_at = characters_before(index, length).rindex(expression) if space_at.nil? if index < length 0 else nil end else if index < length space_at + 1 else length - space_at end end end |