Module: Kontrol::Helpers
- Included in:
- Application, Template
- Defined in:
- lib/kontrol/helpers.rb
Constant Summary collapse
- HTML_ESCAPE =
{ '&' => '&', '"' => '"', '>' => '>', '<' => '<', ' ' => ' ' }
Instance Method Summary collapse
- #h(s) ⇒ Object
-
#link_to(text, path, options = {}) ⇒ Object
Render a link.
- #markdown(text, *args) ⇒ Object
- #strip_tags(str) ⇒ Object
-
#tag(name, *args) ⇒ Object
Render a HTML tag with given name.
- #urlify(string) ⇒ Object
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 |
#link_to(text, path, options = {}) ⇒ Object
Render a link
15 16 17 |
# File 'lib/kontrol/helpers.rb', line 15 def link_to(text, path, = {}) tag :a, text, .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.}</strong>" end |
#strip_tags(str) ⇒ Object
25 26 27 |
# File 'lib/kontrol/helpers.rb', line 25 def (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 |