Files

Class Index [+]

Quicksearch

Ubiquo::Extensions::String

Public Instance Methods

truncate_words(options = {}) click to toggle source

allowed options: :max_chars - the maximum length of the result. Includes the omissions :omission - the string to show when the text is truncated. Default “…” :center - A piece of text where the truncate will be centered. :highlight - some words or pieces of text that will be highlighted with an span :highlight_class - The CSS class to include in the highlighing span.

Look the StringTest file to see examples of use.

    # File vendor/plugins/ubiquo_core/lib/ubiquo/extensions/string.rb, line 33
33:       def truncate_words(options = {})
34:         stripped = ActionController::Base.helpers.strip_tags(self)
35:         
36:         max_length = options[:max_chars] || 100
37:         omission = options[:omission] || "..."
38:         center = options[:center]
39:         highlight = [options[:highlight]].flatten.compact
40:         highlight_class = options[:highlight_class] || "highlight"
41:         
42:         if max_length < stripped.length
43:           if center
44:             r_limit = stripped.index(center) + center.length + ((max_length - center.length) / 2) - 1 - omission.length
45:             l_limit = stripped.index(center) - ((max_length - center.length) / 2) + omission.length
46:             
47:             if l_limit < 0
48:               r_limit -= l_limit
49:               r_limit += omission.length
50:               l_limit = 0
51:             end
52:             if r_limit > stripped.length
53:               l_limit -= r_limit - stripped.length
54:               l_limit -= omission.length
55:               r_limit = stripped.length
56:             end
57:             result = stripped[l_limit..r_limit]
58:             if l_limit >0 && stripped[l_limit-1,1] != " "
59:               result = result[result.index(" ")+1..-1]
60:             end
61:             if r_limit < stripped.length && stripped[r_limit + 1,1] != " "
62:               result = result[0..(result.rindex(" ")-1)]
63:             end
64:             
65:             result = omission + result + omission
66:           else
67:             limit = max_length - 1 - omission.length
68:             result = stripped[0..limit]
69:             if stripped[limit + 1,1] != " "
70:               if result.rindex(" ")
71:                 result = result[0..(result.rindex(" ")-1)]
72:               else
73:                 result = ""
74:               end
75:             end
76:             result += omission
77:           end
78:         else
79:           result = stripped
80:         end
81:         
82:         highlight.each do |h|
83:           result = result.gsub(h, "<span class=\"#{highlight_class}\">#{h}</span>")
84:         end
85:         result
86:         
87:         
88:       end
urilize() click to toggle source

Remove all non-alphanumeric characters from the string, trying to do a wide conversion (though non complete) of non-english characters

    # File vendor/plugins/ubiquo_core/lib/ubiquo/extensions/string.rb, line 7
 7:       def urilize    
 8:         pattern_replacements = [
 9:                                 [/[àáâäÀÁÂÄ]/, "a"],
10:                                 [/[èéêëÈÉÊË]/, "e"],      
11:                                 [/[ìíîïÌÍÎÏ]/, "i"],
12:                                 [/[òóôöÒÓÔÖ]/, "o"],
13:                                 [/[ùúûüÙÚÛÜ]/, "u"],
14:                                 [/[\/(){}<>]/, "_"],
15:                                 [/[ñÑ]/, "n"],
16:                                 [/[çÇ]/, "c"],
17:                                 [/[^\w]/, ''], # finally, discard all non-alphanumeric characters 
18:                                ]
19:         start_string = self.downcase.strip.gsub(" ", "")    
20:         pattern_replacements.inject(start_string) do |s, (pattern, replacement)| 
21:           s.gsub(pattern, replacement)
22:         end      
23:       end

Disabled; run with $DEBUG to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.