Class: SheetFiller::Doc::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/sheet_filler/doc/parser.rb

Constant Summary collapse

REGEX_CODE =
/w:tbl.*?\((\d+)*\) > w:tr.*?(?:\((\d+)*\))? > w:tc(?::nth-of-type\((\d+)*\))?/

Class Method Summary collapse

Class Method Details

.cell_code(field) ⇒ Object



31
32
33
34
35
36
# File 'lib/sheet_filler/doc/parser.rb', line 31

def self.cell_code(field)
  css_path = field.css_path.match(REGEX_CODE)
  tr = css_path[2] || 0
  tc = css_path[3] || 0
  code = "field_#{css_path[1]}_#{tr}_#{tc}"
end

.fields(input_form) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/sheet_filler/doc/parser.rb', line 16

def self.fields(input_form)
  doc = to_xml(input_form)
  (doc/"//w:tr/w:tc").map do |element|
    text = element.text
    list = (element/".//w:result")
    unless list.empty?
      option = list.attribute("val").value.to_i
      text +=
        (element/".//w:listEntry")[option].attribute("val").value
    end
    text.delete!("") # word strange space char
    OpenStruct.new(name: cell_code(element), text: text)
  end
end

.to_xml(input_form) ⇒ Object



9
10
11
12
13
14
# File 'lib/sheet_filler/doc/parser.rb', line 9

def self.to_xml(input_form)
  zip = Zip::File.open(input_form)
  xml = zip.find_entry("word/document.xml")
  doc = Nokogiri::XML.parse(xml.get_input_stream)
  doc.root.search("//w:instrText").remove
end