module Padrino::Helpers::FormatHelpers
def distance_of_time_in_words(from_time, to_time = 0, include_seconds = false, options = {})
-
(String)
- The time formatted as a relative string.
Options Hash:
(**options)
-
:locale
(String
) --
Parameters:
-
options
(Hash
) -- -
include_seconds
(Boolean
) -- -
to_time
(Time
) -- -
from_time
(Time
) --
def distance_of_time_in_words(from_time, to_time = 0, include_seconds = false, options = {}) from_time = from_time.to_time if from_time.respond_to?(:to_time) to_time = to_time.to_time if to_time.respond_to?(:to_time) distance_in_minutes = (((to_time.to_i - from_time.to_i).abs)/60).round distance_in_seconds = ((to_time.to_i - from_time.to_i).abs).round phrase, locals = case distance_in_minutes when 0..1 if include_seconds case distance_in_seconds when 0..4 then [:less_than_x_seconds, :count => 5 ] when 5..9 then [:less_than_x_seconds, :count => 10] when 10..19 then [:less_than_x_seconds, :count => 20] when 20..39 then [:half_a_minute ] when 40..59 then [:less_than_x_minutes, :count => 1 ] else [:x_minutes, :count => 1 ] end else distance_in_minutes == 0 ? [:less_than_x_minutes, :count => 1] : [:x_minutes, :count => distance_in_minutes] end when 2..44 then [:x_minutes, :count => distance_in_minutes ] when 45..89 then [:about_x_hours, :count => 1 ] when 90..1439 then [:about_x_hours, :count => (distance_in_minutes.to_f / 60.0).round ] when 1440..2529 then [:x_days, :count => 1 ] when 2530..43199 then [:x_days, :count => (distance_in_minutes.to_f / 1440.0).round ] when 43200..86399 then [:about_x_months, :count => 1 ] when 86400..525599 then [:x_months, :count => (distance_in_minutes.to_f / 43200.0).round] else distance_in_years = distance_in_minutes / 525600 minute_offset_for_leap_year = (distance_in_years / 4) * 1440 remainder = ((distance_in_minutes - minute_offset_for_leap_year) % 525600) if remainder < 131400 [:about_x_years, :count => distance_in_years] elsif remainder < 394200 [:over_x_years, :count => distance_in_years] else [:almost_x_years, :count => distance_in_years + 1] end end I18n.translate phrase, :count => locals[:count], :locale => options[:locale], :scope => :'datetime.distance_in_words' end
def escape_html(text)
-
(String)
- HTML with escaped characters.
Parameters:
-
text
(String
) --
def escape_html(text) Rack::Utils.escape_html(text).html_safe end
def h!(text, blank_text = ' ')
-
(String)
- HTML with escaped characters or the value specified if blank.
Parameters:
-
blank_text
(String
) -- -
text
(String
) --
def h!(text, blank_text = ' ') return blank_text.html_safe if text.nil? || text.empty? h(text) end
def highlight(text, words, *args)
-
(String)
- The text with the words specified wrapped with highlighted spans.
Options Hash:
(**options)
-
:highlighter
(String
) --
Parameters:
-
options
(Hash
) -- -
words
(String
) -- -
text
(String
) --
Overloads:
-
highlight(text, words, options={})
def highlight(text, words, *args) options = { :highlighter => '<strong class="highlight">\1</strong>' }.update(args.last.is_a?(Hash) ? args.pop : {}) if text.empty? || words.empty? text else match = Array(words).map { |p| Regexp.escape(p) }.join('|') text.gsub(/(#{match})(?!(?:[^<]*?)(?:["'])[^<>]*>)/i, options[:highlighter]) end end
def js_escape_html(html_content)
-
(String)
- The html escaped for javascript passing.
Parameters:
-
html
(String
) --
def js_escape_html(html_content) return '' unless html_content javascript_mapping = { '\\' => '\\\\', '</' => '<\/', "\r\n" => '\n', "\n" => '\n', "\r" => '\n', '"' => '\\"', "'" => "\\'" } escaped_content = html_content.gsub(/(\\|<\/|\r\n|[\n\r"'])/){ |m| javascript_mapping[m] } escaped_content = escaped_content.html_safe if html_content.html_safe? escaped_content end
def pluralize(count, singular, plural = nil)
-
(String)
- The properly pluralized word.
Parameters:
-
plural
(String
) -- -
singular
(String
) -- -
count
(Integer
) --
def pluralize(count, singular, plural = nil) "#{count || 0} " + ((count == 1 || count == '1') ? singular : (plural || singular.pluralize)) end
def simple_format(text, options={})
-
(String)
- The text formatted as simple HTML.
Options Hash:
(**options)
-
:tag
(Symbol
) --
Parameters:
-
options
(Hash
) -- -
text
(String
) --
def simple_format(text, options={}) t = options.delete(:tag) || :p start_tag = tag(t, options, true) text = escape_html(text.to_s.dup) unless text.html_safe? text.gsub!(/\r\n?/, "\n") # \r\n and \r -> \n text.gsub!(/\n\n+/, "</#{t}>\n\n#{start_tag}") # 2+ newline -> paragraph text.gsub!(/([^\n]\n)(?=[^\n])/, '\1<br />') # 1 newline -> br text.insert 0, start_tag text << "</#{t}>" text.html_safe end
def strip_tags(html)
-
(String)
- HTML with tags stripped.
Parameters:
-
html
(String
) --
def strip_tags(html) html.gsub(/<\/?[^>]*>/, "") if html end
def time_ago_in_words(from_time, include_seconds = false)
-
(String)
- The time formatted as a relative string.
Parameters:
-
include_seconds
(Boolean
) -- -
from_time
(Time
) --
def time_ago_in_words(from_time, include_seconds = false) distance_of_time_in_words(from_time, Time.now, include_seconds) end
def truncate(text, options={})
-
(String)
- The text truncated after the given number of characters.
Options Hash:
(**options)
-
:omission
(String
) -- -
:length
(Integer
) --
Parameters:
-
options
(Hash
) -- -
text
(String
) --
def truncate(text, options={}) options = { :length => 30, :omission => "..." }.update(options) if text len = options[:length] - options[:omission].length chars = text (chars.length > options[:length] ? chars[0...len] + options[:omission] : text).to_s end end
def truncate_words(text, options={})
-
(String)
- The text truncated after the given number of words.
Options Hash:
(**options)
-
:omission
(String
) -- -
:length
(Integer
) --
Parameters:
-
options
(Hash
) -- -
text
(String
) --
def truncate_words(text, options={}) options = { :length => 30, :omission => "..." }.update(options) if text words = text.split() words[0..(options[:length]-1)].join(' ') + (words.length > options[:length] ? options[:omission] : '') end end
def word_wrap(text, *args)
-
(String)
- The text with line wraps for lines longer then +line_width+.
Options Hash:
(**options)
-
:line_width
(Integer
) --
Parameters:
-
options
(Hash
) -- -
text
(String
) --
Overloads:
-
word_wrap(text, options={})
def word_wrap(text, *args) options = args.last.is_a?(Hash) ? args.pop : {} unless args.empty? options[:line_width] = args[0] || 80 end options = { :line_width => 80 }.update(options) text.split("\n").map do |line| line.length > options[:line_width] ? line.gsub(/(.{1,#{options[:line_width]}})(\s+|$)/, "\\1\n").strip : line end * "\n" end