lib/lucky_sneaks/string_extensions.rb in rsl-stringex-0.9.3 vs lib/lucky_sneaks/string_extensions.rb in rsl-stringex-0.9.4

- old
+ new

@@ -3,12 +3,12 @@ module StringExtensions def self.included(base) # :nodoc: base.extend(ClassMethods) end - # Returns the string converted (via Textile/RedCloth) to HTML format or - # self if Redcloth is not available. + # Returns the string converted (via Textile/RedCloth) to HTML format + # or self [with a friendly warning] if Redcloth is not available. # # Using <tt>:lite</tt> 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 <tt>textilize_without_paragraph</tt> # except that it makes RedCloth do all the work instead of just gsubbing the return @@ -23,10 +23,11 @@ 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 # Create a URI-friendly representation of the string. This is used internally by @@ -37,11 +38,11 @@ end # Performs multiple text manipulations. Essentially a shortcut for typing them all. View source # below to see which methods are run. def remove_formatting - to_ascii.strip_html_tags.convert_accented_entities.convert_misc_entities.convert_misc_characters.collapse + strip_html_tags.convert_accented_entities.convert_misc_entities.convert_misc_characters.to_ascii.collapse end # Removes HTML tags from text. This code is simplified from Tobias Luettke's regular expression # in Typo[http://typosphere.org]. def strip_html_tags(leave_whitespace = false) @@ -114,15 +115,26 @@ # Note: Because this method will convert any & symbols to the string "and", # you should run any methods which convert HTML entities (convert_html_entities and convert_misc_entities) # before running this method. def convert_misc_characters dummy = dup.gsub(/\.{3,}/, " dot dot dot ") # Catch ellipses before single dot rule! + # Special rules for money { + /(\s|^)\$(\d+)\.(\d+)(\s|$)/ => '\2 dollars \3 cents', + /(\s|^)£(\d+)\.(\d+)(\s|$)/u => '\2 pounds \3 pence', + }.each do |found, replaced| + replaced = " #{replaced} " unless replaced =~ /\\1/ + dummy.gsub!(found, replaced) + end + # Back to normal rules + { /\s*&\s*/ => "and", /\s*#/ => "number", /\s*@\s*/ => "at", /(\S|^)\.(\S)/ => '\1 dot \2', /(\s|^)\$(\d*)(\s|$)/ => '\2 dollars', + /(\s|^)£(\d*)(\s|$)/u => '\2 pounds', + /(\s|^)¥(\d*)(\s|$)/u => '\2 yen', /\s*\*\s*/ => "star", /\s*%\s*/ => "percent", /\s*(\\|\/)\s*/ => "slash", }.each do |found, replaced| replaced = " #{replaced} " unless replaced =~ /\\1/ \ No newline at end of file