Class: HTMLParser::Tag

Inherits:
Object
  • Object
show all
Defined in:
lib/html-renderer/html_parser.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, attrs = {}, children = []) ⇒ Tag

Returns a new instance of Tag.



36
37
38
39
40
# File 'lib/html-renderer/html_parser.rb', line 36

def initialize(name, attrs={}, children=[])
  @name     = name
  @attrs    = attrs
  @children = children
end

Instance Attribute Details

#attrsObject

Returns the value of attribute attrs.



23
24
25
# File 'lib/html-renderer/html_parser.rb', line 23

def attrs
  @attrs
end

#childrenObject

Returns the value of attribute children.



23
24
25
# File 'lib/html-renderer/html_parser.rb', line 23

def children
  @children
end

#nameObject

Returns the value of attribute name.



23
24
25
# File 'lib/html-renderer/html_parser.rb', line 23

def name
  @name
end

Class Method Details

.from_str(s) ⇒ Object



25
26
27
28
29
30
31
32
33
34
# File 'lib/html-renderer/html_parser.rb', line 25

def self.from_str(s)
  name, rest = s.split(/\s+/, 2)
  
  if rest
    attrs = rest.scan(HTMLParser::ATTR_RE).flatten.compact.each_slice(2).to_h
  else
    attrs = {}
  end
  new(name, attrs)
end

Instance Method Details

#recursive_inspect(depth = 0) ⇒ Object



42
43
44
45
46
# File 'lib/html-renderer/html_parser.rb', line 42

def recursive_inspect(depth=0)
  curdent = "  "*depth
  indent = "  "*(depth+1)
  "#{curdent}<#{name} #{attrs}>\n#{indent}#{children.map{|c| c.recursive_inspect(depth+1)}}\n#{curdent}</#{name}>"
end