Module: Mack::ViewHelpers::StringHelpers

Defined in:
lib/mack/view_helpers/string_helpers.rb

Instance Method Summary collapse

Instance Method Details

#pluralize_word(count, word) ⇒ Object

Takes a count integer and a word and returns a phrase containing the count and the correct inflection of the word.

Examples:

pluralize_word(0, "error") # => "0 errors"
pluralize_word(1, "error") # => "1 error"
pluralize_word(2, "error") # => "2 errors"


12
13
14
15
16
17
18
# File 'lib/mack/view_helpers/string_helpers.rb', line 12

def pluralize_word(count, word)
  if count.to_i == 1
    "#{count} #{word.singular}"
  else
    "#{count} #{word.plural}"
  end
end

#sanitize_html(html, options = {}) ⇒ Object

By this will convert all ‘<’ tags to &lt;. You can specify specific tags with the :tags => […] option.

Examples:

sanitize_html("<script>foo;</script>hello <b>mark</b>") # => "&lt;script>foo;&lt;/script>hello &lt;b>mark&lt;/b>"
sanitize_html("<script>foo;</script>hello <b>mark</b>", :tags => :script) # =>  "&lt;script>foo;&lt;/script>hello <b>mark</b>"
sanitize_html("< script>foo;</ script>hello <b>mark</b>", :tags => :script) # => "&lt;script>foo;&lt;/script>hello <b>mark</b>"


46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/mack/view_helpers/string_helpers.rb', line 46

def sanitize_html(html, options = {})
  h = html.to_s.dup
  unless options[:tags]
    return h.gsub("<", "&lt;")
  else
    [options[:tags]].flatten.each do |tag|
      h.gsub!(/<\s*#{tag}/i, "&lt;#{tag}")
      h.gsub!(/<\/\s*#{tag}/i, "&lt;/#{tag}")
    end
    return h
  end
end

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

Applies a simple HTML formatting scheme to the text. It first wraps the whole string in a p tag. Then it converts any double carriage returns to new p tags, and any single break tags to br tags.

Examples:

simple_format("hello\n\ngoodbye\nhello, goodbye") # => "<p>hello</p>\n\n<p>goodbye\n<br />\nhello, goodbye</p>"


26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/mack/view_helpers/string_helpers.rb', line 26

def simple_format(text, options = {})
  if options.empty?
    p = "<p>"
  else
    p = "<p #{options.join("%s=\"%s\"", " ")}>"
  end
  x = text.to_s.dup
  x.gsub!(/\r\n?/, "\n")                    # \r\n and \r -> \n
  x.gsub!(/\n\n+/, "</p>\n\n#{p}")  # 2+ newline  -> paragraph
  x.gsub!(/([^\n]\n)(?=[^\n])/, "\\1<br />\n") # 1 newline   -> br
  "#{p}#{x}</p>"
end