module Stringex::StringExtensions::PublicInstanceMethods

These methods are all included into the String class.

Public Instance Methods

collapse(character = " ") click to toggle source

Removes specified character from the beginning and/or end of the string and then performs String#squeeze(character), condensing runs of the character within the string.

Note: This method has been superceded by ActiveSupport’s squish method.

   # File lib/stringex/string_extensions.rb
19 def collapse(character = " ")
20   sub(/^#{character}*/, "").sub(/#{character}*$/, "").squeeze(character)
21 end
convert_accented_html_entities() click to toggle source

Converts HTML entities into the respective non-accented letters. Examples:

"á".convert_accented_entities # => "a"
"ç".convert_accented_entities # => "c"
"è".convert_accented_entities # => "e"
"î".convert_accented_entities # => "i"
"ø".convert_accented_entities # => "o"
"ü".convert_accented_entities # => "u"

Note: This does not do any conversion of Unicode/ASCII accented-characters. For that functionality please use to_ascii.

   # File lib/stringex/string_extensions.rb
34 def convert_accented_html_entities
35   stringex_convert do
36     cleanup_accented_html_entities!
37   end
38 end
convert_miscellaneous_characters(options = {}) click to toggle source

Converts various common plaintext characters to a more URI-friendly representation. Examples:

"foo & bar".convert_misc_characters # => "foo and bar"
"Chanel #9".convert_misc_characters # => "Chanel number nine"
"user@host".convert_misc_characters # => "user at host"
"google.com".convert_misc_characters # => "google dot com"
"$10".convert_misc_characters # => "10 dollars"
"*69".convert_misc_characters # => "star 69"
"100%".convert_misc_characters # => "100 percent"
"windows/mac/linux".convert_misc_characters # => "windows slash mac slash linux"

It allows localization of conversions so you can use it to convert characters into your own language. Example:

I18n.backend.store_translations :de, { stringex: { characters: { and: "und" } } }
I18n.locale = :de
"ich & dich".convert_misc_characters # => "ich und dich"

Note: Because this method will convert any & symbols to the string “and”, you should run any methods which convert HTML entities (convert_accented_html_entities and convert_miscellaneous_html_entities) before running this method.

   # File lib/stringex/string_extensions.rb
62 def convert_miscellaneous_characters(options = {})
63   stringex_convert(options) do
64     normalize_currency!
65     translate! :ellipses, :currencies, :abbreviations, :characters, :apostrophes
66     cleanup_characters!
67   end
68 end
convert_miscellaneous_html_entities() click to toggle source

Converts HTML entities (taken from common Textile/RedCloth formattings) into plain text formats.

Note: This isn’t an attempt at complete conversion of HTML entities, just those most likely to be generated by Textile.

   # File lib/stringex/string_extensions.rb
74 def convert_miscellaneous_html_entities
75   stringex_convert do
76     translate! :html_entities
77     cleanup_html_entities!
78   end
79 end
convert_smart_punctuation() click to toggle source

Converts MS Word ‘smart punctuation’ to ASCII

   # File lib/stringex/string_extensions.rb
83 def convert_smart_punctuation
84   stringex_convert do
85     cleanup_smart_punctuation!
86   end
87 end
convert_unreadable_control_characters() click to toggle source
    # File lib/stringex/string_extensions.rb
 96 def convert_unreadable_control_characters
 97   stringex_convert do
 98     translate! :unreadable_control_characters
 99   end
100 end
convert_vulgar_fractions() click to toggle source

Converts vulgar fractions from supported HTML entities and Unicode to plain text formats.

   # File lib/stringex/string_extensions.rb
90 def convert_vulgar_fractions
91   stringex_convert do
92     translate! :vulgar_fractions
93   end
94 end
limit(limit = nil, truncate_words = true, whitespace_replacement_token = "-") click to toggle source

Returns the string limited in size to the value of limit.

    # File lib/stringex/string_extensions.rb
103 def limit(limit = nil, truncate_words = true, whitespace_replacement_token = "-")
104   if limit.nil?
105     self
106   else
107     truncate_words == false ? self.whole_word_limit(limit, whitespace_replacement_token) : self[0...limit]
108   end
109 end
remove_formatting(options = {}) click to toggle source

Performs multiple text manipulations. Essentially a shortcut for typing them all. View source below to see which methods are run.

    # File lib/stringex/string_extensions.rb
130 def remove_formatting(options = {})
131   strip_html_tags.
132     convert_smart_punctuation.
133     convert_accented_html_entities.
134     convert_vulgar_fractions.
135     convert_unreadable_control_characters.
136     convert_miscellaneous_html_entities.
137     convert_miscellaneous_characters(options).
138     to_ascii.
139     # NOTE: String#to_ascii may convert some Unicode characters to ascii we'd already transliterated
140     # so we need to do it again just to be safe
141     convert_miscellaneous_characters(options).
142     collapse
143 end
replace_whitespace(replacement = " ") click to toggle source

Replace runs of whitespace in string. Defaults to a single space but any replacement string may be specified as an argument. Examples:

"Foo       bar".replace_whitespace # => "Foo bar"
"Foo       bar".replace_whitespace("-") # => "Foo-bar"
    # File lib/stringex/string_extensions.rb
150 def replace_whitespace(replacement = " ")
151   gsub(/\s+/, replacement)
152 end
strip_html_tags(leave_whitespace = false) click to toggle source

Removes HTML tags from text. NOTE: This code is simplified from Tobias Luettke’s regular expression in Typo.

    # File lib/stringex/string_extensions.rb
156 def strip_html_tags(leave_whitespace = false)
157   string = stringex_convert do
158     strip_html_tags!
159   end
160   leave_whitespace ? string : string.replace_whitespace(' ')
161 end
to_ascii() click to toggle source

Returns string with its UTF-8 characters transliterated to ASCII ones. Example:

"⠋⠗⠁⠝⠉⠑".to_ascii #=> "france"
   # File lib/stringex/unidecoder.rb
77 def to_ascii
78   Stringex::Unidecoder.decode(self)
79 end
to_html(lite_mode = false) click to toggle source

Returns the string converted (via Textile/RedCloth) to HTML format or self [with a friendly warning] if Redcloth is not available.

Using :lite argument will cause RedCloth to not wrap the HTML in a container P element, which is useful behavior for generating header element text, etc. This is roughly equivalent to ActionView’s textilize_without_paragraph except that it makes RedCloth do all the work instead of just gsubbing the return from RedCloth.

    # File lib/stringex/string_extensions.rb
171 def to_html(lite_mode = false)
172   if defined?(RedCloth)
173     if lite_mode
174       RedCloth.new(self, [:lite_mode]).to_html
175     else
176       if self =~ /<pre>/
177         RedCloth.new(self).to_html.tr("\t", "")
178       else
179         RedCloth.new(self).to_html.tr("\t", "").gsub(/\n\n/, "")
180       end
181     end
182   else
183     warn "String#to_html was called without RedCloth being successfully required"
184     self
185   end
186 end
to_url(options = {}) click to toggle source

Create a URI-friendly representation of the string. This is used internally by acts_as_url but can be called manually in order to generate an URI-friendly version of any string.

    # File lib/stringex/string_extensions.rb
191 def to_url(options = {})
192   return self if options[:exclude] && options[:exclude].include?(self)
193   options = stringex_default_options.merge(options)
194   whitespace_replacement_token = options[:replace_whitespace_with]
195   dummy = remove_formatting(options).
196             replace_whitespace(whitespace_replacement_token).
197             collapse(whitespace_replacement_token).
198             limit(options[:limit], options[:truncate_words], whitespace_replacement_token)
199   dummy.downcase! unless options[:force_downcase] == false
200   dummy
201 end
whole_word_limit(limit, whitespace_replacement_token = "-") click to toggle source
    # File lib/stringex/string_extensions.rb
111 def whole_word_limit(limit, whitespace_replacement_token = "-")
112   whole_words = []
113   words = self.split(whitespace_replacement_token)
114 
115   words.each do |word|
116     if word.size > limit
117       break
118     else
119       whole_words << word
120       limit -= (word.size + 1)
121     end
122   end
123 
124   whole_words.join(whitespace_replacement_token)
125 end

Private Instance Methods

stringex_convert(options = {}, &block) click to toggle source
    # File lib/stringex/string_extensions.rb
205 def stringex_convert(options = {}, &block)
206   Localization.convert self, options, &block
207 end
stringex_default_options() click to toggle source
    # File lib/stringex/string_extensions.rb
209 def stringex_default_options
210   Stringex::Configuration::StringExtensions.new.settings.marshal_dump
211 end