Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/webby/core_ext/string.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.small_wordsObject



4
5
6
# File 'lib/webby/core_ext/string.rb', line 4

def self.small_words
  @small_words ||= %w(a an and as at but by en for if in of on or the to v[.]? via vs[.]?)
end

Instance Method Details

#/(path) ⇒ Object



8
9
10
# File 'lib/webby/core_ext/string.rb', line 8

def /( path )
  ::File.join(self, path)
end

#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.



157
158
159
# File 'lib/webby/core_ext/string.rb', line 157

def collapse(character = " ")
  sub(/^#{character}*/, "").sub(/#{character}*$/, "").squeeze(character)
end

#convert_accented_entitiesObject

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.



75
76
77
# File 'lib/webby/core_ext/string.rb', line 75

def convert_accented_entities
  gsub(/&([A-Za-z])(grave|acute|circ|tilde|uml|ring|cedil|slash);/, '\1')
end

#convert_misc_charactersObject

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"

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.



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/webby/core_ext/string.rb', line 126

def convert_misc_characters
  dummy = dup.gsub(/\.{3,}/, " dot dot dot ") # Catch ellipses before single dot rule!
  {
    /\s*&\s*/ => "and",
    /\s*#/ => "number",
    /\s*@\s*/ => "at",
    /(\S|^)\.(\S)/ => '\1 dot \2',
    /(\s|^)\$(\d*)(\s|$)/ => '\2 dollars',
    /\s*\*\s*/ => "star",
    /\s*%\s*/ => "percent",
    /\s*(\\|\/)\s*/ => "slash",
  }.each do |found, replaced|
    replaced = " #{replaced} " unless replaced =~ /\\1/
    dummy.gsub!(found, replaced)
  end
  dummy = dummy.gsub(/(^|\w)'(\w|$)/, '\1\2').gsub(/[\.,:;()\[\]\/\?!\^'"_]/, " ")
end

#convert_misc_entitiesObject

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.



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/webby/core_ext/string.rb', line 83

def convert_misc_entities
  dummy = dup
  {
    "#822[01]" => "\"",
    "#821[67]" => "'",
    "#8230" => "...",
    "#8211" => "-",
    "#8212" => "--",
    "#215" => "x",
    "gt" => ">",
    "lt" => "<",
    "(#8482|trade)" => "(tm)",
    "(#174|reg)" => "(r)",
    "(#169|copy)" => "(c)",
    "(#38|amp)" => "and",
    "nbsp" => " ",
    "(#162|cent)" => " cent",
    "(#163|pound)" => " pound",
    "(#188|frac14)" => "one fourth",
    "(#189|frac12)" => "half",
    "(#190|frac34)" => "three fourths",
    "(#176|deg)" => " degrees"
  }.each do |textiled, normal|
    dummy.gsub!(/&#{textiled};/, normal)
  end
  dummy.gsub(/&[^;]+;/, "")
end

#remove_formattingObject

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



50
51
52
# File 'lib/webby/core_ext/string.rb', line 50

def remove_formatting
  strip_html_tags.convert_accented_entities.convert_misc_entities.convert_misc_characters.collapse
end

#replace_whitespace(replace = " ") ⇒ 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"


149
150
151
# File 'lib/webby/core_ext/string.rb', line 149

def replace_whitespace(replace = " ")
  gsub(/\s+/, replace)
end

#strip_html_tags(leave_whitespace = false) ⇒ Object

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



56
57
58
59
60
61
62
# File 'lib/webby/core_ext/string.rb', line 56

def strip_html_tags(leave_whitespace = false)
  name = /[\w:_-]+/
  value = /([A-Za-z0-9]+|('[^']*?'|"[^"]*?"))/
  attr = /(#{name}(\s*=\s*#{value})?)/
  rx = /<[!\/?\[]?(#{name}|--)(\s+(#{attr}(\s+#{attr})*))?\s*([!\/?\]]+|--)?>/
  (leave_whitespace) ?  gsub(rx, "").strip : gsub(rx, "").gsub(/\s+/, " ").strip
end

#titlecaseObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/webby/core_ext/string.rb', line 12

def titlecase
  swrgxp = self.class.small_words.join('|')

  parts = self.split( %r/( [:.;?!][ ] | (?:[ ]|^)["“] )/x )
  parts.each do |part|
    part.gsub!(%r/\b[[:alpha:]][[:lower:].'’]*\b/) do |s|
      s =~ %r/\w+\.\w+/ ? s : s.capitalize
    end

    # Lowercase the small words
    part.gsub!(%r/\b(#{swrgxp})\b/i) {|w| w.downcase}

    # If the first word is a small word, then capitalize it
    part.gsub!(%r/\A([[:punct:]]*)(#{swrgxp})\b/) {$1 + $2.capitalize}

    # If the last word is a small word, then capitalize it
    part.gsub!(%r/\b(#{swrgxp})([^\w\s]*)\z/) {$1.capitalize + $2}
  end

  str = parts.join

  # Special cases:
  str.gsub!(/ V(s?)\. /, ' v\1. ')               # "v." and "vs."
  str.gsub!(/(['’])S\b/, '\1s')                  # 'S (otherwise you get "the SEC'S decision")
  str.gsub!(/\b(AT&T|Q&A)\b/i) { |w| w.upcase }  # "AT&T" and "Q&A", which get tripped up.

  str
end

#to_urlObject

Create a URI-friendly representation of the string.



44
45
46
# File 'lib/webby/core_ext/string.rb', line 44

def to_url
  remove_formatting.downcase.replace_whitespace("-").collapse("-")
end