Class: Tubby::Renderer
- Inherits:
-
Object
- Object
- Tubby::Renderer
- Defined in:
- lib/tubby.rb
Constant Summary collapse
- TAGS =
%w[ a abbr acronym address applet article aside audio b basefont bdi bdo big blockquote body button canvas caption center cite code colgroup datalist dd del details dfn dir div dl dt em fieldset figcaption figure font footer form frame frameset h1 h2 h3 h4 h5 h6 head header hgroup html i iframe ins kbd label legend li map mark math menu meter nav object ol optgroup option output p pre progress q rp rt ruby s samp section select small span strike strong style sub summary sup svg table tbody td textarea tfoot th thead time title tr tt u ul var video xmp ]- SELF_CLOSING_TAGS =
%w[ base link meta hr br wbr img embed param source track area col input keygen command ]
Instance Method Summary collapse
- #<<(obj) ⇒ Object
- #__attrs!(attrs) ⇒ Object
- #doctype! ⇒ Object
-
#initialize(target) ⇒ Renderer
constructor
A new instance of Renderer.
- #raw!(text) ⇒ Object
- #script(content = nil, **attrs) ⇒ Object
- #self_closing_tag!(name, **attrs) ⇒ Object
- #tag!(name, content = nil, **attrs) ⇒ Object
Constructor Details
#initialize(target) ⇒ Renderer
Returns a new instance of Renderer.
31 32 33 |
# File 'lib/tubby.rb', line 31 def initialize(target) @target = target end |
Instance Method Details
#<<(obj) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/tubby.rb', line 35 def <<(obj) obj = obj.to_tubby if obj.respond_to?(:to_tubby) if obj.is_a?(Tubby::Template) obj.render_with(self) elsif obj.respond_to?(:to_html) @target << obj.to_html elsif obj.respond_to?(:html_safe?) && obj.html_safe? @target << obj else @target << CGI.escape_html(obj.to_s) end self end |
#__attrs!(attrs) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/tubby.rb', line 57 def __attrs!(attrs) if attrs.key?(:data) && attrs[:data].is_a?(Hash) # Flatten present `data: {k1: v1, k2: v2}` attribute, inserting `data-k1: v1, data-k2: v2` # into exact place where the attribute was attrs = Hash[attrs.flat_map do |key, value| if key == :data && value.is_a?(Hash) value.map { |k, v| [:"data-#{k}", v] } else [[key, value]] end end] end attrs.each do |key, value| if value.is_a?(Array) value = value.compact.join(" ") end if value key = key.to_s.tr("_", "-") if value == true @target << " #{key}" else value = CGI.escape_html(value.to_s) @target << " #{key}=\"#{value}\"" end end end end |
#doctype! ⇒ Object
53 54 55 |
# File 'lib/tubby.rb', line 53 def doctype! @target << "<!DOCTYPE html>" end |
#raw!(text) ⇒ Object
49 50 51 |
# File 'lib/tubby.rb', line 49 def raw!(text) @target << text.to_s end |
#script(content = nil, **attrs) ⇒ Object
135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/tubby.rb', line 135 def script(content = nil, **attrs) @target << "<script" __attrs!(attrs) @target << ">" if content if content =~ /<(!--|script|\/script)/ raise "script tags can not contain #$&" end @target << content end @target << "</script>" end |
#self_closing_tag!(name, **attrs) ⇒ Object
97 98 99 100 101 |
# File 'lib/tubby.rb', line 97 def self_closing_tag!(name, **attrs) @target << "<" << name __attrs!(attrs) @target << ">" end |
#tag!(name, content = nil, **attrs) ⇒ Object
88 89 90 91 92 93 94 95 |
# File 'lib/tubby.rb', line 88 def tag!(name, content = nil, **attrs) @target << "<" << name __attrs!(attrs) @target << ">" self << content if content yield if block_given? @target << "</" << name << ">" end |