Module: Clamsy::Tenjin::HtmlHelper
- Included in:
- Context
- Defined in:
- lib/clamsy/tenjin.rb
Overview
helper module for Context class
Constant Summary collapse
- XML_ESCAPE_TABLE =
{ '&'=>'&', '<'=>'<', '>'=>'>', '"'=>'"', "'"=>''' }
Class Method Summary collapse
-
.checked(expr) ⇒ Object
return ‘ checked=“checked”’ if expr is not false or nil.
-
.disabled(expr) ⇒ Object
return ‘ disabled=“disabled”’ if expr is not false or nil.
- .escape_xml(s) ⇒ Object (also: escape)
-
.nl2br(text) ⇒ Object
convert “n” into “<br />n”.
-
.selected(expr) ⇒ Object
return ‘ selected=“selected”’ if expr is not false or nil.
-
.tagattr(name, expr, value = nil, escape = true) ⇒ Object
(experimental) return ‘ name=“value”’ if expr is not false nor nil.
-
.text2html(text) ⇒ Object
convert “n” and “ ” into “<br />n” and “ ”.
Class Method Details
.checked(expr) ⇒ Object
return ‘ checked=“checked”’ if expr is not false or nil
80 81 82 |
# File 'lib/clamsy/tenjin.rb', line 80 def checked(expr) return expr ? ' checked="checked"' : '' end |
.disabled(expr) ⇒ Object
return ‘ disabled=“disabled”’ if expr is not false or nil
90 91 92 |
# File 'lib/clamsy/tenjin.rb', line 90 def disabled(expr) return expr ? ' disabled="disabled"' : '' end |
.escape_xml(s) ⇒ Object Also known as: escape
52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/clamsy/tenjin.rb', line 52 def escape_xml(s) #return s.gsub(/[&<>"]/) { XML_ESCAPE_TABLE[$&] } return s.gsub(/[&<>"]/) { |s| XML_ESCAPE_TABLE[s] } ## #s = s.gsub(/&/, '&') #s.gsub!(/</, '<') #s.gsub!(/>/, '>') #s.gsub!(/"/, '"') #return s ## #return s.gsub(/&/, '&').gsub(/</, '<').gsub(/>/, '>').gsub(/"/, '"') end |
.nl2br(text) ⇒ Object
convert “n” into “<br />n”
95 96 97 |
# File 'lib/clamsy/tenjin.rb', line 95 def nl2br(text) return text.to_s.gsub(/\n/, "<br />\n") end |
.selected(expr) ⇒ Object
return ‘ selected=“selected”’ if expr is not false or nil
85 86 87 |
# File 'lib/clamsy/tenjin.rb', line 85 def selected(expr) return expr ? ' selected="selected"' : '' end |
.tagattr(name, expr, value = nil, escape = true) ⇒ Object
(experimental) return ‘ name=“value”’ if expr is not false nor nil. if value is nil or false then expr is used as value.
69 70 71 72 73 74 75 76 77 |
# File 'lib/clamsy/tenjin.rb', line 69 def tagattr(name, expr, value=nil, escape=true) if !expr return '' elsif escape return " #{name}=\"#{escape_xml((value || expr).to_s)}\"" else return " #{name}=\"#{value || expr}\"" end end |
.text2html(text) ⇒ Object
convert “n” and “ ” into “<br />n” and “ ”
100 101 102 |
# File 'lib/clamsy/tenjin.rb', line 100 def text2html(text) return nl2br(escape_xml(text.to_s).gsub(/ /, ' ')) end |