Class: I18nTemplate::Node
- Inherits:
-
Object
- Object
- I18nTemplate::Node
- Defined in:
- lib/i18n_template/node.rb
Overview
Document processing node
Instance Attribute Summary collapse
-
#children ⇒ Object
readonly
The array of children of this node.
-
#line ⇒ Object
readonly
The line number of the input where this node was begun.
-
#parent ⇒ Object
readonly
The parent node of this node.
-
#phrase ⇒ Object
Returns the value of attribute phrase.
-
#position ⇒ Object
readonly
The byte position in the input where this node was begun.
-
#tag ⇒ Object
Returns the value of attribute tag.
-
#token ⇒ Object
readonly
Node token.
Instance Method Summary collapse
- #descendants_text ⇒ Object
-
#initialize(parent, line = 0, position = 0, token = nil, tag = nil, &block) ⇒ Node
constructor
Create a new node as a child of the given parent.
-
#root? ⇒ Boolean
root node?.
-
#tag? ⇒ Boolean
tag node?.
-
#text? ⇒ Boolean
text node?.
-
#wrapped_node_text ⇒ Object
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>.
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
#children ⇒ Object (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 |
#line ⇒ Object (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 |
#parent ⇒ Object (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 |
#phrase ⇒ Object
Returns the value of attribute phrase.
22 23 24 |
# File 'lib/i18n_template/node.rb', line 22 def phrase @phrase end |
#position ⇒ Object (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 |
#tag ⇒ Object
Returns the value of attribute tag.
21 22 23 |
# File 'lib/i18n_template/node.rb', line 21 def tag @tag end |
#token ⇒ Object (readonly)
Node token
19 20 21 |
# File 'lib/i18n_template/node.rb', line 19 def token @token end |
Instance Method Details
#descendants_text ⇒ Object
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?
45 46 47 |
# File 'lib/i18n_template/node.rb', line 45 def root? parent == self end |
#tag? ⇒ Boolean
tag node?
35 36 37 |
# File 'lib/i18n_template/node.rb', line 35 def tag? !@tag.nil? end |
#text? ⇒ Boolean
text node?
40 41 42 |
# File 'lib/i18n_template/node.rb', line 40 def text? @tag.nil? end |
#wrapped_node_text ⇒ Object
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>
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 |