Module: Kontrol::Helpers

Included in:
Application, Template
Defined in:
lib/kontrol/helpers.rb

Constant Summary collapse

HTML_ESCAPE =
{ '&' => '&amp;', '"' => '&quot;', '>' => '&gt;', '<' => '&lt;', ' ' => '&nbsp;' }

Instance Method Summary collapse

Instance Method Details

#h(s) ⇒ Object



35
36
37
# File 'lib/kontrol/helpers.rb', line 35

def h(s)
  s.to_s.gsub(/[ &"><]/) { |special| HTML_ESCAPE[special] }
end

Render a link



15
16
17
# File 'lib/kontrol/helpers.rb', line 15

def link_to(text, path, options = {})
  tag :a, text, options.merge(:href => path)
end

#markdown(text, *args) ⇒ Object



19
20
21
22
23
# File 'lib/kontrol/helpers.rb', line 19

def markdown(text, *args)
  BlueCloth.new(text, *args).to_html
rescue => e      
  "#{text}<br/><br/><strong style='color:red'>#{e.message}</strong>"
end

#strip_tags(str) ⇒ Object



25
26
27
# File 'lib/kontrol/helpers.rb', line 25

def strip_tags(str)
  str.to_s.gsub(/<\/?[^>]*>/, "")
end

#tag(name, *args) ⇒ Object

Render a HTML tag with given name. The last argument specifies the attributes of the tag. The second argument may be the content of the tag.



8
9
10
11
12
# File 'lib/kontrol/helpers.rb', line 8

def tag(name, *args)
  text, attr = args.first.is_a?(Hash) ? [nil, args.first] : args
  attributes = attr.map { |k, v| %Q{#{k}="#{v}"} }.join(' ')
  "<#{name} #{attributes}>#{text}</#{name}>"
end

#urlify(string) ⇒ Object



29
30
31
# File 'lib/kontrol/helpers.rb', line 29

def urlify(string)
  string.downcase.gsub(/[ -]+/, '-').gsub(/[^-a-z0-9_]+/, '')
end