Class: Goat::DOMTools::HTMLBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/goat/html.rb

Defined Under Namespace

Classes: TagBuilder

Constant Summary collapse

ARRAY_ATTRS =
[:class]

Instance Method Summary collapse

Constructor Details

#initialize(input) ⇒ HTMLBuilder

Returns a new instance of HTMLBuilder.



260
261
262
# File 'lib/goat/html.rb', line 260

def initialize(input)
  @input = input
end

Instance Method Details

#array_to_html(ar) ⇒ Object



240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/goat/html.rb', line 240

def array_to_html(ar)
  if ar.first.kind_of?(Symbol)
    tag = ar[0]
    have_attrs = ar[1].is_a?(Hash)
    attrs = have_attrs ? ar[1] : {}
    body = ar[(have_attrs ? 2 : 1)..-1]

    tag, attrs, body = TagBuilder.build(tag, attrs, body)
    lonely = standalone_tags.include?(tag.to_s)

    open_tag = "<#{tag}#{attrs.empty? ? '' : (' ' + attrs_to_html(attrs))}>"
    body_html = body.empty? ? '' : body.map{|x| x.to_html(self)}.join
    close_tag = (lonely ? '' : "</#{tag}>")

    "#{open_tag}#{body_html}#{close_tag}"
  else
    ar.map{|x| x.to_html(self)}.join
  end
end

#attrs_to_html(attrs) ⇒ Object



236
237
238
# File 'lib/goat/html.rb', line 236

def attrs_to_html(attrs)
  attrs.map {|k, v| "#{k}=\"#{v}\""}.join(' ')
end

#htmlObject



264
265
266
# File 'lib/goat/html.rb', line 264

def html
  @input.to_html(self)
end

#standalone_tagsObject



232
233
234
# File 'lib/goat/html.rb', line 232

def standalone_tags
  %w{br img input}
end