Class: Creeker::SharedStrings

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(book, multi_thread = false) ⇒ SharedStrings

Returns a new instance of SharedStrings.



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

def initialize book, multi_thread = false
  @book = book
  parse_shared_shared_strings(multi_thread)
end

Instance Attribute Details

#bookObject (readonly)

Returns the value of attribute book.



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

def book
  @book
end

#dictionaryObject (readonly)

Returns the value of attribute dictionary.



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

def dictionary
  @dictionary
end

Class Method Details

.parse_shared_string_from_document(xml, multi_thread) ⇒ Object



28
29
30
31
32
33
34
35
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
# File 'lib/creeker/shared_strings.rb', line 28

def self.parse_shared_string_from_document(xml, multi_thread)
  dictionary = Hash.new
  # (1..10).each do |i|
  #   thread = Thread.new do
  #     xml.css('si').first(i * 10000).each_with_index do |si, idx|
  #       text_nodes = si.css('t')
  #       if text_nodes.count == 1 # plain text node
  #         dictionary[idx] = text_nodes.first.content
  #       else # rich text nodes with text fragments
  #         dictionary[idx] = text_nodes.map(&:content).join('')
  #       end
  #     end
  #   end

  #   sleep 1*i
  #   GC.start
  # end

  # Creeker::Book.new(Upload.last.document.path)
  if multi_thread
    xml.css('si').to_a.in_groups_of(10000).each_with_index do |group, index|
      thread = Thread.new do
        group.each_with_index do |si, idx|
          text_nodes = si.css('t')
          if text_nodes.count == 1 # plain text node
            dictionary[idx] = text_nodes.first.content
          else # rich text nodes with text fragments
            dictionary[idx] = text_nodes.map(&:content).join('')
          end
        end
      end

      sleep index
      GC.start
    end
  else
    xml.css('si').first(20).each_with_index do |si, idx|
      text_nodes = si.css('t')
      if text_nodes.count == 1 # plain text node
        dictionary[idx] = text_nodes.first.content
      else # rich text nodes with text fragments
        dictionary[idx] = text_nodes.map(&:content).join('')
      end
    end
    sleep 1
    GC.start
  end
  dictionary
end

Instance Method Details

#parse_shared_shared_strings(multi_thread) ⇒ Object



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

def parse_shared_shared_strings multi_thread
  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, multi_thread)
  end
end

#parse_shared_string_from_document(xml, multi_thread) ⇒ Object



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

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