class MaRuKu::Out::HTML::HTMLElement
A simple class to represent an HTML
element for output.
Constants
- SELF_CLOSING
These elements have no children and should be rendered with a self-closing tag. It’s not an exhaustive list, but they cover everything we use.
Attributes
attributes[RW]
children[RW]
name[RW]
Public Class Methods
new(name, attr={}, children=[]) { || ... }
click to toggle source
# File lib/maruku/output/to_html.rb, line 21 def initialize(name, attr={}, children=[]) self.name = name self.attributes = attr || {} self.children = Array(children) children << yield if block_given? end
Public Instance Methods
<<(child)
click to toggle source
# File lib/maruku/output/to_html.rb, line 28 def <<(child) children << child if children self end
[](key)
click to toggle source
# File lib/maruku/output/to_html.rb, line 33 def [](key) attributes[key.to_s] end
[]=(key, value)
click to toggle source
# File lib/maruku/output/to_html.rb, line 37 def []=(key, value) attributes[key.to_s] = value end
add_class(class_name)
click to toggle source
# File lib/maruku/output/to_html.rb, line 41 def add_class(class_name) attributes['class'] = ((attributes['class']||'').split(' ') + [class_name]).join(' ') end
to_html()
click to toggle source
# File lib/maruku/output/to_html.rb, line 49 def to_html m = "<#{name}" attributes.each do |k, v| m << " #{k.to_s}=\"#{v.to_s}\"" end if SELF_CLOSING.include? name m << " />" else content = children.map(&:to_s) m << ">" << content.join('') << "</#{name}>" end end