module Stringex::StringExtensions::PublicInstanceMethods
def collapse(character = " ")
String#squeeze(character), condensing runs of the character within the string.
Removes specified character from the beginning and/or end of the string and then performs
def collapse(character = " ") sub(/^#{character}*/, "").sub(/#{character}*$/, "").squeeze(character) end
def convert_accented_html_entities
Note: This does not do any conversion of Unicode/ASCII accented-characters. For that
"ü".convert_accented_entities # => "u"
"ø".convert_accented_entities # => "o"
"î".convert_accented_entities # => "i"
"è".convert_accented_entities # => "e"
"ç".convert_accented_entities # => "c"
"á".convert_accented_entities # => "a"
Converts HTML entities into the respective non-accented letters. Examples:
def convert_accented_html_entities stringex_convert do cleanup_accented_html_entities! end end
def convert_miscellaneous_characters(options = {})
you should run any methods which convert HTML entities (convert_accented_html_entities and convert_miscellaneous_html_entities)
Note: Because this method will convert any & symbols to the string "and",
"ich & dich".convert_misc_characters # => "ich und dich"
I18n.locale = :de
I18n.backend.store_translations :de, { stringex: { characters: { and: "und" } } }
Example:
It allows localization of conversions so you can use it to convert characters into your own language.
"windows/mac/linux".convert_misc_characters # => "windows slash mac slash linux"
"100%".convert_misc_characters # => "100 percent"
"*69".convert_misc_characters # => "star 69"
"$10".convert_misc_characters # => "10 dollars"
"google.com".convert_misc_characters # => "google dot com"
"user@host".convert_misc_characters # => "user at host"
"Chanel #9".convert_misc_characters # => "Chanel number nine"
"foo & bar".convert_misc_characters # => "foo and bar"
Examples:
Converts various common plaintext characters to a more URI-friendly representation.
def convert_miscellaneous_characters(options = {}) stringex_convert(options) do normalize_currency! translate! :ellipses, :currencies, :abbreviations, :characters, :apostrophes cleanup_characters! end end
def convert_miscellaneous_html_entities
Note: This isn't an attempt at complete conversion of HTML entities, just those most likely
Converts HTML entities (taken from common Textile/RedCloth formattings) into plain text formats.
def convert_miscellaneous_html_entities stringex_convert do translate! :html_entities cleanup_html_entities! end end
def convert_smart_punctuation
Converts MS Word 'smart punctuation' to ASCII
def convert_smart_punctuation stringex_convert do cleanup_smart_punctuation! end end
def convert_unreadable_control_characters
def convert_unreadable_control_characters stringex_convert do translate! :unreadable_control_characters end end
def convert_vulgar_fractions
def convert_vulgar_fractions stringex_convert do translate! :vulgar_fractions end end
def limit(limit = nil, truncate_words = true, whitespace_replacement_token = "-")
def limit(limit = nil, truncate_words = true, whitespace_replacement_token = "-") if limit.nil? self else truncate_words == false ? self.whole_word_limit(limit, whitespace_replacement_token) : self[0...limit] end end
def remove_formatting(options = {})
Performs multiple text manipulations. Essentially a shortcut for typing them all. View source
def remove_formatting(options = {}) strip_html_tags. convert_smart_punctuation. convert_accented_html_entities. convert_vulgar_fractions. convert_unreadable_control_characters. convert_miscellaneous_html_entities. convert_miscellaneous_characters(options). 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(options). collapse end
def replace_whitespace(replacement = " ")
"Foo bar".replace_whitespace # => "Foo bar"
string may be specified as an argument. Examples:
Replace runs of whitespace in string. Defaults to a single space but any replacement
def replace_whitespace(replacement = " ") gsub(/\s+/, replacement) end
def stringex_convert(options = {}, &block)
def stringex_convert(options = {}, &block) Localization.convert self, options, &block end
def stringex_default_options
def stringex_default_options Stringex::Configuration::StringExtensions.new.settings.marshal_dump end
def strip_html_tags(leave_whitespace = false)
Removes HTML tags from text.
def strip_html_tags(leave_whitespace = false) string = stringex_convert do strip_html_tags! end leave_whitespace ? string : string.replace_whitespace(' ') end
def to_ascii
Returns string with its UTF-8 characters transliterated to ASCII ones. Example:
def to_ascii Stringex::Unidecoder.decode(self) end
def to_html(lite_mode = false)
except that it makes RedCloth do all the work instead of just gsubbing the return
This is roughly equivalent to ActionView's textilize_without_paragraph
P element, which is useful behavior for generating header element text, etc.
Using :lite argument will cause RedCloth to not wrap the HTML in a container
or self [with a friendly warning] if Redcloth is not available.
Returns the string converted (via Textile/RedCloth) to HTML format
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
def to_url(options = {})
acts_as_url[link:classes/Stringex/ActsAsUrl/ClassMethods.html#M000012]
Create a URI-friendly representation of the string. This is used internally by
def to_url(options = {}) return self if options[:exclude] && options[:exclude].include?(self) options = stringex_default_options.merge(options) whitespace_replacement_token = options[:replace_whitespace_with] dummy = remove_formatting(options). replace_whitespace(whitespace_replacement_token). collapse(whitespace_replacement_token). limit(options[:limit], options[:truncate_words], whitespace_replacement_token) dummy.downcase! unless options[:force_downcase] == false dummy end
def whole_word_limit(limit, whitespace_replacement_token = "-")
def whole_word_limit(limit, whitespace_replacement_token = "-") whole_words = [] words = self.split(whitespace_replacement_token) words.each do |word| if word.size > limit break else whole_words << word limit -= (word.size + 1) end end whole_words.join(whitespace_replacement_token) end