Class: Smarky::Element
- Inherits:
-
Object
- Object
- Smarky::Element
- Defined in:
- lib/smarky/element.rb
Instance Method Summary collapse
- #[]=(attribute, value) ⇒ Object
- #add_child(child) ⇒ Object
- #add_next_sibling(sibling) ⇒ Object
- #add_section ⇒ Object
- #attributes ⇒ Object
- #children ⇒ Object
- #content ⇒ Object
-
#initialize(*args) ⇒ Element
constructor
A new instance of Element.
- #inner_html ⇒ Object
- #name ⇒ Object
- #parent ⇒ Object
- #sections ⇒ Object
- #title ⇒ Object
- #to_html ⇒ Object
Constructor Details
#initialize(*args) ⇒ Element
Returns a new instance of Element.
3 4 5 6 7 8 9 10 11 12 13 14 15 |
# File 'lib/smarky/element.rb', line 3 def initialize(*args) node_or_name, @title = args case node_or_name when String @document = Nokogiri::HTML::Document.new @node = Nokogiri::XML::Node.new(node_or_name, @document) else @document = node_or_name.document @node = node_or_name end end |
Instance Method Details
#[]=(attribute, value) ⇒ Object
36 37 38 |
# File 'lib/smarky/element.rb', line 36 def []=(attribute, value) @node[attribute] = value end |
#add_child(child) ⇒ Object
52 53 54 55 56 |
# File 'lib/smarky/element.rb', line 52 def add_child(child) @node.add_child(child.node) dirty! child end |
#add_next_sibling(sibling) ⇒ Object
58 59 60 61 62 |
# File 'lib/smarky/element.rb', line 58 def add_next_sibling(sibling) @node.add_next_sibling(sibling.node) dirty! sibling end |
#add_section ⇒ Object
64 65 66 |
# File 'lib/smarky/element.rb', line 64 def add_section add_child(Element.new('section')) end |
#attributes ⇒ Object
32 33 34 |
# File 'lib/smarky/element.rb', line 32 def attributes @node.attributes end |
#children ⇒ Object
44 45 46 |
# File 'lib/smarky/element.rb', line 44 def children @children ||= @node.children.map { |node| Element.new(node) } end |
#content ⇒ Object
40 41 42 |
# File 'lib/smarky/element.rb', line 40 def content @node.content end |
#inner_html ⇒ Object
72 73 74 |
# File 'lib/smarky/element.rb', line 72 def inner_html @node.inner_html end |
#name ⇒ Object
28 29 30 |
# File 'lib/smarky/element.rb', line 28 def name @node.name end |
#parent ⇒ Object
24 25 26 |
# File 'lib/smarky/element.rb', line 24 def parent @parent ||= Element.new(@node.parent) end |
#sections ⇒ Object
48 49 50 |
# File 'lib/smarky/element.rb', line 48 def sections @sections ||= @node.css('> section').map { |node| Element.new(node) } end |
#title ⇒ Object
17 18 19 20 21 22 |
# File 'lib/smarky/element.rb', line 17 def title @title ||= begin first_child = @node.children.first first_child && first_child.name =~ /^h[1-6]$/ && first_child.content end end |
#to_html ⇒ Object
68 69 70 |
# File 'lib/smarky/element.rb', line 68 def to_html @node.to_html end |