Copied from actionpack, and revised by insane-dreamer to fix a bug (original fails on some URLs)
Turns all urls into clickable links. If a block is given, each url is yielded and the result is used as the link text.
# File lib/ramaze/helper/formatting.rb, line 138 138: def auto_link(text, opts = {}) 139: html_options = ' ' + opts.map{|k,v| "#{k}='#{v}'"}.join(' ') if opts.any? 140: text.gsub(AUTO_LINK_RE) do 141: all, a, b, c, d = $&, $1, $2, $3, $4 142: if a =~ /<a\s/ # don't replace URL's that are already linked 143: all 144: else 145: text = b + c 146: text = yield(text) if block_given? 147: %(#{a}<a href="#{b=="www."?"http://www.":b}#{c}"#{html_options}>#{text}</a>#{d}) 148: end 149: end 150: end
takes a string and optional argument for outputting compliance HTML instead of XHTML.
@example
nl2br "a\nb\n\c" #=> 'a<br />b<br />c'
# File lib/ramaze/helper/formatting.rb, line 159 159: def nl2br(string, xhtml = true) 160: br = xhtml ? '<br />' : '<br>' 161: string.gsub(/\n/, br) 162: end
Answers with a representation of given count with correct grammar. If no items argument is given, and the count argument is not 1, then we first check whether the item argument responds to ``#`` (for example if you are using Sequel). If this doesn’t work we append ’s’ to the item argument.
@example usage
number_counter(0, 'comment') # => 'no comments' number_counter(1, 'comment') # => 'one comment' number_counter(2, 'comment') # => '2 comments'
# File lib/ramaze/helper/formatting.rb, line 33 33: def number_counter(count, item, items = nil) 34: count, item = count.to_i, item.to_s 35: 36: if count == 1 37: "one #{item}" 38: else 39: items ||= item.respond_to?(:pluralize) ? item.pluralize : "#{item}s" 40: prefix = FORMATTING_NUMBER_COUNTER[count] || count 41: "#{prefix} #{items}" 42: end 43: end
Format a floating number nicely for display.
@example
number_format(123.123) # => '123.123' number_format(123456.12345) # => '123,456.12345' number_format(123456.12345, '.') # => '123.456,12345'
# File lib/ramaze/helper/formatting.rb, line 52 52: def number_format(n, delimiter = ',') 53: delim_l, delim_r = delimiter == ',' ? ], .] : ]. ,] 54: h, r = n.to_s.split('.') 55: [h.reverse.scan(/\d{1,3}/).join(delim_l).reverse, r].compact.join(delim_r) 56: end
# File lib/ramaze/helper/formatting.rb, line 164 164: def obfuscate_email(email, text = nil) 165: obfuscated = [] 166: email.to_s.each_byte{|c| obfuscated << "&#%03d" % c } 167: joined = obfuscated.join 168: 169: %(<a href="mailto:#{joined}">#{text || joined}</a>) 170: end
Answer with the ordinal version of a number.
@example
ordinal(1) # => "1st" ordinal(2) # => "2nd" ordinal(3) # => "3rd" ordinal(13) # => "13th" ordinal(33) # => "33rd" ordinal(100) # => "100th" ordinal(133) # => "133rd"
# File lib/ramaze/helper/formatting.rb, line 69 69: def ordinal(number) 70: number = number.to_i 71: 72: case number % 100 73: when 11..13; "#{number}th" 74: else 75: case number % 10 76: when 1; "#{number}st" 77: when 2; "#{number}nd" 78: when 3; "#{number}rd" 79: else "#{number}th" 80: end 81: end 82: end
Returns Hash with tags as keys and their weight as value.
Example:
tags = %w[ruby ruby code ramaze] tagcloud(tags) # => {"code"=>0.75, "ramaze"=>0.75, "ruby"=>1.0}
The weight can be influenced by adjusting the min and max parameters, please make sure that max is larger than min to get meaningful output.
This is not thought as immediate output to your template but rather to help either implementing your own algorithm or using the result as input for your tagcloud.
@example
tagcloud(tags).each do |tag, weight| style = "font-size: %0.2fem" % weight %a{:style => style, :href => Rs(tag)}= h(tag) end
# File lib/ramaze/helper/formatting.rb, line 193 193: def tagcloud(tags, min = 0.5, max = 1.5) 194: result = {} 195: total = tags.size.to_f 196: diff = max - min 197: 198: tags.uniq.each do |tag| 199: count = tags.respond_to?(:count) ? tags.count(tag) : tags.select{|t| t==tag }.size 200: result[tag] = ((count / total) * diff) + min 201: end 202: 203: result 204: end
stolen and adapted from rails
# File lib/ramaze/helper/formatting.rb, line 85 85: def time_diff(from_time, to_time = Time.now, include_seconds = false) 86: distance_in_minutes = (((to_time - from_time).abs)/60).round 87: distance_in_seconds = ((to_time - from_time).abs).round if include_seconds 88: 89: case distance_in_minutes 90: when 0..1 91: return (distance_in_minutes == 0) ? 'less than a minute' : '1 minute' unless include_seconds 92: case distance_in_seconds 93: when 0..4 then 'less than 5 seconds' 94: when 5..9 then 'less than 10 seconds' 95: when 10..19 then 'less than 20 seconds' 96: when 20..39 then 'half a minute' 97: when 40..59 then 'less than a minute' 98: else '1 minute' 99: end 100: 101: when 2..44 then "#{distance_in_minutes} minutes" 102: when 45..89 then 'about 1 hour' 103: when 90..1439 then "about #{(distance_in_minutes.to_f / 60.0).round} hours" 104: when 1440..2879 then '1 day' 105: when 2880..43199 then "#{(distance_in_minutes / 1440).round} days" 106: when 43200..86399 then 'about 1 month' 107: when 86400..525959 then "#{(distance_in_minutes / 43200).round} months" 108: when 525960..1051919 then 'about 1 year' 109: else "over #{(distance_in_minutes / 525960).round} years" 110: end 111: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.