Class: ODFWriter::Bookmark

Inherits:
Field
  • Object
show all
Defined in:
lib/odf_writer/bookmark.rb

Overview

Bookmark: replace bookmarks with given name

Constant Summary

Constants inherited from Field

Field::DELIMITERS

Instance Attribute Summary

Attributes inherited from Field

#name

Instance Method Summary collapse

Methods inherited from Field

#field, #initialize, #value

Constructor Details

This class inherits a constructor from ODFWriter::Field

Instance Method Details

#replace!(doc, item = nil) ⇒ Object

replace!



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/odf_writer/bookmark.rb', line 36

def replace!(doc, item = nil)

  nodes = find_bookmark_nodes(doc)
  return if nodes.blank?
  
  text = value(item)
  text = text.encode(universal_newline: true)
  
  text_node_array = text.split(/\n/).map{|a| Nokogiri::XML::Text.new(a, doc) }
  unless text_node_array.length == 1
    text_node_array = text_node_array.inject([]) do |collector, node| 
      collector << Nokogiri::XML::Node.new("line-break", doc) unless collector.empty?
      collector << node
    end 
  end
  
  nodes.each do |node|
  
    case node.name
    
    when "bookmark"
      text_node_array.each {|tn| node.before(tn)}
      
      #
      # find bookmark
      #
      bm  =  "text:bookmark[@text:name='#{@name}']"
      bmn = doc.xpath(".//.//*[self::#{bm}]")
      #
      # delete bookmark
      #
      bmn.each  {|b| b.remove }
      
    when "bookmark-start"
      text_node_array.each {|tn| node.before(tn)}
      
      #
      # find bookmark-start 
      #
      bms  =  "text:bookmark-start[@text:name='#{@name}']"
      bmsn = doc.xpath(".//.//*[self::#{bms}]")
      #
      # find bookmark text 
      #
      bme  = "text:bookmark-end[@text:name='#{@name}']"
      bmen = doc.xpath(".//.//*[self::#{bme}]")
      #
      # find bookmark-end 
      #
      bmn  = doc.xpath(".//text()[preceding-sibling::#{bms} and following-sibling::#{bme}]")
      #
      # delete bookmark -start, text and -end
      #
      bmn.each  {|b| b.remove }
      bmsn.each {|b| b.remove }
      bmen.each {|b| b.remove }
      
    end #case
  end #each
  
end