Class: Swordfish::DOCX::Document

Inherits:
Object
  • Object
show all
Includes:
Parser
Defined in:
lib/swordfish/formats/docx/document.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Parser

#_node_parse_list, #_node_parse_paragraph, #_node_parse_runs, #_node_parse_table, #_node_parse_table_cell, #_node_parse_table_row

Constructor Details

#initialize(archive, xml_docs) ⇒ Document

Returns a new instance of Document.



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/swordfish/formats/docx/document.rb', line 39

def initialize(archive, xml_docs)
  @docx_archive = archive
  @swordfish_doc = Swordfish::Document.new
  parse_styles xml_docs[:styles]
  parse_numbering(xml_docs[:numbering]) if xml_docs[:numbering]
  parse_relationships(xml_docs[:relationships]) if xml_docs[:relationships]
  parse_relationships(xml_docs[:footnote_rels], :footnotes) if xml_docs[:footnote_rels]
  parse_relationships(xml_docs[:endnote_rels], :endnotes) if xml_docs[:endnote_rels]
  parse_footnotes(xml_docs[:footnotes]) if xml_docs[:footnotes]
  parse_endnotes(xml_docs[:endnotes]) if xml_docs[:endnotes]
  parse xml_docs[:document]
end

Instance Attribute Details

#docx_archiveObject (readonly)

The source archive



15
16
17
# File 'lib/swordfish/formats/docx/document.rb', line 15

def docx_archive
  @docx_archive
end

#swordfish_docObject (readonly)

The Swordfish::Document corresponding to the parsed document



14
15
16
# File 'lib/swordfish/formats/docx/document.rb', line 14

def swordfish_doc
  @swordfish_doc
end

Class Method Details

.open(filepath) ⇒ Object

Parse a document and return a Swordfish::Document object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/swordfish/formats/docx/document.rb', line 18

def self.open(filepath)
  # .docx is a zipped file format consisting of several XML files.
  # Read in the content of each needed file.
  docx_archive = Zip::File.open(filepath)

  xml_docs = {
    :document      => docx_archive.read('word/document.xml'),
    :styles        => docx_archive.read('word/styles.xml'),
    :numbering     => (docx_archive.read('word/numbering.xml') rescue nil),
    :relationships => (docx_archive.read('word/_rels/document.xml.rels') rescue nil),
    :footnotes     => (docx_archive.read('word/footnotes.xml') rescue nil),
    :footnote_rels => (docx_archive.read('word/_rels/footnotes.xml.rels') rescue nil),
    :endnotes      => (docx_archive.read('word/endnotes.xml') rescue nil),
    :endnote_rels  => (docx_archive.read('word/_rels/endnotes.xml.rels') rescue nil)
  }

  # Parse the XML files and generate the Swordfish::Document
  swordfish_docx = new docx_archive, xml_docs
  swordfish_docx.swordfish_doc
end