Class: I18nTemplate::Node

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

Overview

Document processing node

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent, line = 0, position = 0, token = nil, tag = nil, &block) ⇒ Node

Create a new node as a child of the given parent.


25
26
27
28
29
30
31
32
# File 'lib/i18n_template/node.rb', line 25

def initialize(parent, line = 0, position = 0, token = nil, tag = nil, &block)
  @parent = parent
  @children = []
  @line, @position = line, position
  @token = token
  @tag = tag
  instance_eval(&block) if block_given?
end

Instance Attribute Details

#childrenObject (readonly)

The array of children of this node. Not all nodes have children.


6
7
8
# File 'lib/i18n_template/node.rb', line 6

def children
  @children
end

#lineObject (readonly)

The line number of the input where this node was begun


13
14
15
# File 'lib/i18n_template/node.rb', line 13

def line
  @line
end

#parentObject (readonly)

The parent node of this node. All nodes have a parent, except for the root node.


10
11
12
# File 'lib/i18n_template/node.rb', line 10

def parent
  @parent
end

#phraseObject

Returns the value of attribute phrase


22
23
24
# File 'lib/i18n_template/node.rb', line 22

def phrase
  @phrase
end

#positionObject (readonly)

The byte position in the input where this node was begun


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

def position
  @position
end

#tagObject

Returns the value of attribute tag


21
22
23
# File 'lib/i18n_template/node.rb', line 21

def tag
  @tag
end

#tokenObject (readonly)

Node token


19
20
21
# File 'lib/i18n_template/node.rb', line 19

def token
  @token
end

Instance Method Details

#descendants_textObject


63
64
65
66
67
68
69
70
71
72
# File 'lib/i18n_template/node.rb', line 63

def descendants_text
  output = ""
  children.each do |child|
    output << child.token if child.text?
    child.children.each do |node|
      output << node.descendants_text
    end
  end
  output
end

#root?Boolean

root node?

Returns:

  • (Boolean)

45
46
47
# File 'lib/i18n_template/node.rb', line 45

def root?
  parent == self
end

#tag?Boolean

tag node?

Returns:

  • (Boolean)

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

def tag?
  !@tag.nil?
end

#text?Boolean

text node?

Returns:

  • (Boolean)

40
41
42
# File 'lib/i18n_template/node.rb', line 40

def text?
  @tag.nil?
end

#wrapped_node_textObject

Return descendant text if tag node is wrapper node for some text node <div><span>text</span><div> <div><span>text</span><p>data</p><div>

Returns:

  • nil for


54
55
56
57
58
59
60
61
# File 'lib/i18n_template/node.rb', line 54

def wrapped_node_text
  node = self
  while node.children.size == 1
    node = node.children.first
    return node.token if node.text?
  end
  return nil
end