module Padrino::Helpers::FormatHelpers

def distance_of_time_in_words(from_time, to_time = 0, include_seconds = false, options = {})

Other tags:
    Api: - public

Returns:
  • (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
  I18n.with_options :locale => options[:locale], :scope => :'datetime.distance_in_words' do |locale|
    case distance_in_minutes
      when 0..1
        return distance_in_minutes == 0 ?
               locale.t(:less_than_x_minutes, :count => 1) :
               locale.t(:x_minutes, :count => distance_in_minutes) unless include_seconds
        case distance_in_seconds
          when 0..4   then locale.t :less_than_x_seconds, :count => 5
          when 5..9   then locale.t :less_than_x_seconds, :count => 10
          when 10..19 then locale.t :less_than_x_seconds, :count => 20
          when 20..39 then locale.t :half_a_minute
          when 40..59 then locale.t :less_than_x_minutes, :count => 1
          else             locale.t :x_minutes,           :count => 1
        end
      when 2..44           then locale.t :x_minutes,      :count => distance_in_minutes
      when 45..89          then locale.t :about_x_hours,  :count => 1
      when 90..1439        then locale.t :about_x_hours,  :count => (distance_in_minutes.to_f / 60.0).round
      when 1440..2529      then locale.t :x_days,         :count => 1
      when 2530..43199     then locale.t :x_days,         :count => (distance_in_minutes.to_f / 1440.0).round
      when 43200..86399    then locale.t :about_x_months, :count => 1
      when 86400..525599   then locale.t :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
          locale.t(:about_x_years,  :count => distance_in_years)
        elsif remainder < 394200
          locale.t(:over_x_years,   :count => distance_in_years)
        else
          locale.t(:almost_x_years, :count => distance_in_years + 1)
        end
    end
  end
end

def escape_html(text)

Other tags:
    Api: - public

Returns:
  • (String) - HTML with escaped characters.

Parameters:
  • text (String) --
def escape_html(text)
  Rack::Utils.escape_html(text)
end

def h!(text, blank_text = '&nbsp;')

Other tags:
    Api: - public

Returns:
  • (String) - HTML with escaped characters or the value specified if blank.

Parameters:
  • blank_text (String) --
  • text (String) --
def h!(text, blank_text = '&nbsp;')
  return blank_text if text.nil? || text.empty?
  h text
end

def highlight(text, words, *args)

Other tags:
    Api: - public

Returns:
  • (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 = args.extract_options!
  options.reverse_merge!(:highlighter => '<strong class="highlight">\1</strong>')
  if text.blank? || words.blank?
    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)

Other tags:
    Api: - public

Returns:
  • (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', '"' => '\\"', "'" => "\\'" }
  html_content.gsub(/(\\|<\/|\r\n|[\n\r"'])/) { javascript_mapping[$1] }
end

def pluralize(count, singular, plural = nil)

Other tags:
    Api: - public

Returns:
  • (String) - The properly pluralized word.

Parameters:
  • plural (String) --
  • singular (String) --
  • count (Fixnum) --
def pluralize(count, singular, plural = nil)
  "#{count || 0} " + ((count == 1 || count == '1') ? singular : (plural || singular.pluralize))
end

def simple_format(text, options={})

Other tags:
    Api: - public

Returns:
  • (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 = text.to_s.dup
  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}>"
end

def strip_tags(html)

Other tags:
    Api: - public

Returns:
  • (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)

Other tags:
    Api: - public

Returns:
  • (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={})

Other tags:
    Api: - public

Returns:
  • (String) - The text truncated after the given number of characters.

Options Hash: (**options)
  • :omission (String) --
  • :length (Fixnum) --

Parameters:
  • options (Hash) --
  • text (String) --
def truncate(text, options={})
  options.reverse_merge!(:length => 30, :omission => "...")
  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={})

Other tags:
    Api: - public

Returns:
  • (String) - The text truncated after the given number of words.

Options Hash: (**options)
  • :omission (String) --
  • :length (Fixnum) --

Parameters:
  • options (Hash) --
  • text (String) --
def truncate_words(text, options={})
  options.reverse_merge!(:length => 30, :omission => "...")
  if text
    words = text.split()
    words[0..(options[:length]-1)].join(' ') + (words.length > options[:length] ? options[:omission] : '')
  end
end

def word_wrap(text, *args)

Other tags:
    Api: - public

Returns:
  • (String) - The text with line wraps for lines longer then +line_width+

Options Hash: (**options)
  • :line_width (Fixnum) --

Parameters:
  • options (Hash) --
  • text (String) --

Overloads:
  • word_wrap(text, options={})
def word_wrap(text, *args)
  options = args.extract_options!
  unless args.blank?
    options[:line_width] = args[0] || 80
  end
  options.reverse_merge!(:line_width => 80)
  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