Class: AsciidoctorVaped::AST::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/asciidoctor_vaped/ast/node.rb

Direct Known Subclasses

Document, Element, Text

Constant Summary collapse

TEXT_CONTEXTS =
%i[text link strong emphasis monospace].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(children: []) ⇒ Node

Returns a new instance of Node.



11
12
13
14
# File 'lib/asciidoctor_vaped/ast/node.rb', line 11

def initialize(children: [])
  @children = []
  append_children(children)
end

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children.



8
9
10
# File 'lib/asciidoctor_vaped/ast/node.rb', line 8

def children
  @children
end

#parentObject

Returns the value of attribute parent.



9
10
11
# File 'lib/asciidoctor_vaped/ast/node.rb', line 9

def parent
  @parent
end

Instance Method Details

#<<(node) ⇒ Object



16
17
18
# File 'lib/asciidoctor_vaped/ast/node.rb', line 16

def <<(node)
  append(node)
end

#append(node) ⇒ Object



20
21
22
23
24
# File 'lib/asciidoctor_vaped/ast/node.rb', line 20

def append(node)
  node.parent = self
  children << node
  node
end

#append_children(nodes) ⇒ Object



26
27
28
29
# File 'lib/asciidoctor_vaped/ast/node.rb', line 26

def append_children(nodes)
  nodes.each { |node| append(node) }
  self
end

#parse_inlineObject



35
36
37
# File 'lib/asciidoctor_vaped/ast/node.rb', line 35

def parse_inline
  append_children(Parser::Inline.parse(text))
end

#sectionsObject



39
40
41
# File 'lib/asciidoctor_vaped/ast/node.rb', line 39

def sections
  children.select { |child| child.context == :section }
end

#textObject



31
32
33
# File 'lib/asciidoctor_vaped/ast/node.rb', line 31

def text
  children.select { |child| TEXT_CONTEXTS.include?(child.context) }.map(&:text).join
end

#to_hObject



43
44
45
46
47
48
49
50
# File 'lib/asciidoctor_vaped/ast/node.rb', line 43

def to_h
  {
    context:,
    text:,
    attributes:,
    children: children.map(&:to_h)
  }
end