Class: Docx::NodesToFix

Inherits:
Object
  • Object
show all
Defined in:
lib/docx/nodes_to_fix.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeNodesToFix

Returns a new instance of NodesToFix.



4
5
6
# File 'lib/docx/nodes_to_fix.rb', line 4

def initialize
  forget
end

Instance Attribute Details

#current_nodeObject

Returns the value of attribute current_node.



3
4
5
# File 'lib/docx/nodes_to_fix.rb', line 3

def current_node
  @current_node
end

#node_listObject

Returns the value of attribute node_list.



3
4
5
# File 'lib/docx/nodes_to_fix.rb', line 3

def node_list
  @node_list
end

#valueObject

Returns the value of attribute value.



3
4
5
# File 'lib/docx/nodes_to_fix.rb', line 3

def value
  @value
end

Instance Method Details

#fixObject



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/docx/nodes_to_fix.rb', line 24

def fix
  @node_list.each do |obj|
    node = obj[:node]
    range = obj[:range]
    new_val = node.value
    new_val[range] = value.to_s || ''
    node.value = new_val
    if new_val =~ /^\s+/ && node.parent
      node.parent.add_attribute('xml:space', 'preserve')
    end
    self.value = nil
  end
end

#forgetObject



8
9
10
11
12
# File 'lib/docx/nodes_to_fix.rb', line 8

def forget
  @current_node = nil
  @node_list = []
  @value = ''
end

#remember(node, index) ⇒ Object



14
15
16
17
18
19
20
21
22
# File 'lib/docx/nodes_to_fix.rb', line 14

def remember(node, index)
  new_node = current_node.nil? || current_node != node
  if new_node
    @current_node = node
    @node_list << {:node => node, :range => index..index}
  else
    @node_list.last[:range] = (node_list.last[:range].min)..index
  end
end