Module: AnyView::Helpers::FormatHelpers

Defined in:
lib/any_view/format_helpers.rb

Instance Method Summary collapse

Instance Method Details

#escape_html(text) ⇒ Object Also known as: h, sanitize_html

Returns escaped text to protect against malicious content



6
7
8
# File 'lib/any_view/format_helpers.rb', line 6

def escape_html(text)
  Rack::Utils.escape_html(text)
end

#h!(text, blank_text = ' ') ⇒ Object

Returns escaped text to protect against malicious content Returns blank if the text is empty



14
15
16
17
# File 'lib/any_view/format_helpers.rb', line 14

def h!(text, blank_text = ' ')
  return blank_text if text.nil? || text.empty?
  h text
end

#simple_format(text, html_options = {}) ⇒ Object

Returns text transformed into HTML using simple formatting rules. Two or more consecutive newlines(nn) are considered as a paragraph and wrapped in <p> tags. One newline (n) is considered as a linebreak and a <br /> tag is appended. This method does not remove the newlines from the text. simple_format(“hellonworld”) # => “<p>hello<br/>world</p>”



38
39
40
41
42
43
44
45
46
# File 'lib/any_view/format_helpers.rb', line 38

def simple_format(text, html_options={})
  start_tag = tag('p', html_options.merge(:open => true))
  text = text.to_s.dup
  text.gsub!(/\r\n?/, "\n")                    # \r\n and \r -> \n
  text.gsub!(/\n\n+/, "</p>\n\n#{start_tag}")  # 2+ newline  -> paragraph
  text.gsub!(/([^\n]\n)(?=[^\n])/, '\1<br />') # 1 newline   -> br
  text.insert 0, start_tag
  text << "</p>"
end

#truncate(text, *args) ⇒ Object

Truncates a given text after a given :length if text is longer than :length (defaults to 30). The last characters will be replaced with the :omission (defaults to “…”) for a total length not exceeding :length. truncate(“Once upon a time in a world far far away”, :length => 8) => “Once upon…”



22
23
24
25
26
27
28
29
30
31
# File 'lib/any_view/format_helpers.rb', line 22

def truncate(text, *args)
  options = args.extract_options!
  options = options.dup
  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