Class: Nokogiri::XML::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/amber/render/table_of_contents.rb

Instance Method Summary collapse

Instance Method Details

#to_pretty_html(indent = 0) ⇒ Object



274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
# File 'lib/amber/render/table_of_contents.rb', line 274

def to_pretty_html(indent=0)
  indent_str = "  " * indent
  children_html = []
  text_html = nil
  if children.size == 1 && children.first.name == "text"
    text_html = children.first.content
  else
    children.each do |child|
      if child.name == "text"
        children_html << "#{"  " * (indent+1)}#{child.content}" if !child.content.empty?
      else
        children_html << child.to_pretty_html(indent+1)
      end
    end
  end
  attrs = []
  attributes.each do |attribute|
    attrs << %(#{attribute[0]}="#{attribute[1]}")
  end
  if attrs.any?
    attr_html = " " + attrs.join(' ')
  else
    attr_html = ""
  end
  html = []
  if text_html
    html << "#{indent_str}<#{name}#{attr_html}>#{text_html}</#{name}>"
  elsif children_html.any?
    html << "#{indent_str}<#{name}#{attr_html}>"
    html += children_html
    html << "#{indent_str}</#{name}>"
  else
    html << "#{indent_str}<#{name}#{attr_html}></#{name}>"
  end
  html.join("\n")
end