Module: Stringex::StringExtensions::PublicInstanceMethods
- Defined in:
- lib/stringex/string_extensions.rb
Overview
These methods are all included into the String class.
Instance Method Summary collapse
-
#collapse(character = " ") ⇒ Object
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. -
#convert_accented_html_entities ⇒ Object
Converts HTML entities into the respective non-accented letters.
-
#convert_miscellaneous_characters(options = {}) ⇒ Object
Converts various common plaintext characters to a more URI-friendly representation.
-
#convert_miscellaneous_html_entities ⇒ Object
Converts HTML entities (taken from common Textile/RedCloth formattings) into plain text formats.
-
#convert_smart_punctuation ⇒ Object
Converts MS Word ‘smart punctuation’ to ASCII.
-
#convert_vulgar_fractions ⇒ Object
Converts vulgar fractions from supported HTML entities and Unicode to plain text formats.
-
#limit(limit = nil) ⇒ Object
Returns the string limited in size to the value of limit.
-
#remove_formatting(options = {}) ⇒ Object
Performs multiple text manipulations.
-
#replace_whitespace(replacement = " ") ⇒ Object
Replace runs of whitespace in string.
-
#strip_html_tags(leave_whitespace = false) ⇒ Object
Removes HTML tags from text.
-
#to_html(lite_mode = false) ⇒ Object
Returns the string converted (via Textile/RedCloth) to HTML format or self [with a friendly warning] if Redcloth is not available.
-
#to_url(options = {}) ⇒ Object
Create a URI-friendly representation of the string.
Instance Method Details
#collapse(character = " ") ⇒ Object
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.
11 12 13 |
# File 'lib/stringex/string_extensions.rb', line 11 def collapse(character = " ") sub(/^#{character}*/, "").sub(/#{character}*$/, "").squeeze(character) end |
#convert_accented_html_entities ⇒ Object
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
.
26 27 28 29 30 |
# File 'lib/stringex/string_extensions.rb', line 26 def convert_accented_html_entities stringex_convert do cleanup_accented_html_entities! end end |
#convert_miscellaneous_characters(options = {}) ⇒ Object
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.
54 55 56 57 58 59 60 |
# File 'lib/stringex/string_extensions.rb', line 54 def convert_miscellaneous_characters( = {}) stringex_convert() do normalize_currency! translate! :ellipses, :currencies, :abbreviations, :characters, :apostrophes cleanup_characters! end end |
#convert_miscellaneous_html_entities ⇒ Object
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.
66 67 68 69 70 71 |
# File 'lib/stringex/string_extensions.rb', line 66 def convert_miscellaneous_html_entities stringex_convert do translate! :html_entities cleanup_html_entities! end end |
#convert_smart_punctuation ⇒ Object
Converts MS Word ‘smart punctuation’ to ASCII
75 76 77 78 79 |
# File 'lib/stringex/string_extensions.rb', line 75 def convert_smart_punctuation stringex_convert do cleanup_smart_punctuation! end end |
#convert_vulgar_fractions ⇒ Object
Converts vulgar fractions from supported HTML entities and Unicode to plain text formats.
82 83 84 85 86 |
# File 'lib/stringex/string_extensions.rb', line 82 def convert_vulgar_fractions stringex_convert do translate! :vulgar_fractions end end |
#limit(limit = nil) ⇒ Object
Returns the string limited in size to the value of limit.
89 90 91 |
# File 'lib/stringex/string_extensions.rb', line 89 def limit(limit = nil) limit.nil? ? self : self[0...limit] end |
#remove_formatting(options = {}) ⇒ Object
Performs multiple text manipulations. Essentially a shortcut for typing them all. View source below to see which methods are run.
95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/stringex/string_extensions.rb', line 95 def remove_formatting( = {}) . convert_smart_punctuation. convert_accented_html_entities. convert_vulgar_fractions. convert_miscellaneous_html_entities. convert_miscellaneous_characters(). to_ascii. # NOTE: String#to_ascii may convert some Unicode characters to ascii we'd already transliterated # so we need to do it again just to be safe convert_miscellaneous_characters(). collapse end |
#replace_whitespace(replacement = " ") ⇒ Object
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"
114 115 116 |
# File 'lib/stringex/string_extensions.rb', line 114 def replace_whitespace(replacement = " ") gsub(/\s+/, replacement) end |
#strip_html_tags(leave_whitespace = false) ⇒ Object
Removes HTML tags from text. NOTE: This code is simplified from Tobias Luettke’s regular expression in Typo.
120 121 122 123 124 125 |
# File 'lib/stringex/string_extensions.rb', line 120 def (leave_whitespace = false) string = stringex_convert do end leave_whitespace ? string : string.replace_whitespace(' ') end |
#to_html(lite_mode = false) ⇒ Object
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.
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/stringex/string_extensions.rb', line 135 def to_html(lite_mode = false) if defined?(RedCloth) if lite_mode RedCloth.new(self, [:lite_mode]).to_html else if self =~ /<pre>/ RedCloth.new(self).to_html.tr("\t", "") else RedCloth.new(self).to_html.tr("\t", "").gsub(/\n\n/, "") end end else warn "String#to_html was called without RedCloth being successfully required" self end end |
#to_url(options = {}) ⇒ Object
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.
155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/stringex/string_extensions.rb', line 155 def to_url( = {}) return self if [:exclude] && [:exclude].include?(self) = .merge() whitespace_replacement_token = [:replace_whitespace_with] dummy = remove_formatting(). replace_whitespace(whitespace_replacement_token). collapse("-"). limit([:limit]) dummy.downcase! unless [:force_downcase] == false dummy end |