Class: String
- Inherits:
-
Object
- Object
- String
- Defined in:
- lib/typogrowth/string.rb
Constant Summary collapse
- PUNCTUATION =
'¿?¡!()„“”‚‘’«».,:;'.split //
Instance Method Summary collapse
- #defuse(elements = nil, shadows: []) ⇒ Object
- #is_ru?(shadows: []) ⇒ Boolean
- #psub(pattern, exclusion, replacement) ⇒ Object
-
#safe_delimiters ⇒ Object
private.
-
#typo(lang: nil, sections: nil, shadows: nil) ⇒ Object
Typographyes the string and returns a result See Typogrowth::Parser#parse.
-
#typo!(lang: nil, sections: nil, shadows: nil) ⇒ Object
Typographyes the string inplace See Typogrowth::Parser#parse!.
Instance Method Details
#defuse(elements = nil, shadows: []) ⇒ Object
35 36 37 |
# File 'lib/typogrowth/string.rb', line 35 def defuse elements = nil, shadows: [] Typogrowth.defuse self, elements || PUNCTUATION, shadows: shadows end |
#is_ru?(shadows: []) ⇒ Boolean
31 32 33 |
# File 'lib/typogrowth/string.rb', line 31 def is_ru? shadows: [] Typogrowth.is_ru? self, shadows: shadows end |
#psub(pattern, exclusion, replacement) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/typogrowth/string.rb', line 39 def psub pattern, exclusion, replacement delims = self.safe_delimiters s = self.dup [*exclusion].each { |re| re = /#{re}/ unless Regexp === re s.gsub!(re) { |m| "#{delims.first}#{Base64.encode64 m}#{delims.last}" } } s.gsub! pattern, replacement s.gsub!(/#{delims.first}(.*?)#{delims.last}/m) { |m| Base64.decode64(m).force_encoding('UTF-8') } s end |
#safe_delimiters ⇒ Object
private
55 56 57 58 59 60 61 |
# File 'lib/typogrowth/string.rb', line 55 def safe_delimiters delimiters = ['❮', '❯'] loop do break delimiters unless self.match(/#{delimiters.join('|')}/) delimiters.map! {|d| d*2} end end |
#typo(lang: nil, sections: nil, shadows: nil) ⇒ Object
Typographyes the string and returns a result See Typogrowth::Parser#parse
12 13 14 15 16 17 18 19 |
# File 'lib/typogrowth/string.rb', line 12 def typo lang: nil, sections: nil, shadows: nil Typogrowth.parse( self, lang: lang ? lang : is_ru? ? "ru" : I18n.locale, shadows: shadows, sections: sections ) end |
#typo!(lang: nil, sections: nil, shadows: nil) ⇒ Object
Typographyes the string inplace See Typogrowth::Parser#parse!
22 23 24 25 26 27 28 29 |
# File 'lib/typogrowth/string.rb', line 22 def typo! lang: nil, sections: nil, shadows: nil Typogrowth.parse!( self, lang: lang ? lang : is_ru? ? "ru" : I18n.locale, shadows: shadows, sections: sections ) end |