Class: Creek::SharedStrings

Inherits:
Object
  • Object
show all
Defined in:
lib/creek/shared_strings.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(book) ⇒ SharedStrings

Returns a new instance of SharedStrings.


10
11
12
13
# File 'lib/creek/shared_strings.rb', line 10

def initialize book
  @book = book
  parse_shared_shared_strings
end

Instance Attribute Details

#bookObject (readonly)

Returns the value of attribute book


8
9
10
# File 'lib/creek/shared_strings.rb', line 8

def book
  @book
end

#dictionaryObject (readonly)

Returns the value of attribute dictionary


8
9
10
# File 'lib/creek/shared_strings.rb', line 8

def dictionary
  @dictionary
end

Class Method Details

.parse_shared_string_from_document(xml) ⇒ Object


28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/creek/shared_strings.rb', line 28

def self.parse_shared_string_from_document(xml)
  dictionary = Hash.new

  xml.css('si').each_with_index do |si, idx|
    text_nodes = si.css('t')
    if text_nodes.count == 1 # plain text node
      dictionary[idx] = Creek::Styles::Converter.unescape_string(text_nodes.first.content)
    else # rich text nodes with text fragments
      dictionary[idx] = text_nodes.map { |n| Creek::Styles::Converter.unescape_string(n.content) }.join('')
    end
  end

  dictionary
end

Instance Method Details

#parse_shared_shared_stringsObject


15
16
17
18
19
20
21
22
# File 'lib/creek/shared_strings.rb', line 15

def parse_shared_shared_strings
  path = "xl/sharedStrings.xml"
  if @book.files.file.exist?(path)
    doc = @book.files.file.open path
    xml = Nokogiri::XML::Document.parse doc
    parse_shared_string_from_document(xml)
  end
end

#parse_shared_string_from_document(xml) ⇒ Object


24
25
26
# File 'lib/creek/shared_strings.rb', line 24

def parse_shared_string_from_document(xml)
  @dictionary = self.class.parse_shared_string_from_document(xml)
end