diff --git a/lib/docx/document.rb b/lib/docx/document.rb index 3102da4..440875b 100755 --- a/lib/docx/document.rb +++ b/lib/docx/document.rb @@ -18,15 +18,22 @@ module Docx # puts d.text # end class Document - attr_reader :xml, :doc, :zip, :styles + + DOCUMENT_PATHS = { + doc: "word/document.xml", + styles: "word/styles.xml", + header: "word/header1.xml", + footer: "word/footer1.xml", + numbering: "word/numbering.xml" + } + + attr_reader :xml, :doc, :zip, :styles, :header, :footer, :numbering def initialize(path, &block) @replace = {} @zip = Zip::File.open(path) - @document_xml = @zip.read('word/document.xml') - @doc = Nokogiri::XML(@document_xml) - @styles_xml = @zip.read('word/styles.xml') - @styles = Nokogiri::XML(@styles_xml) + extract_documents + if block_given? yield self @zip.close @@ -123,6 +130,15 @@ def replace_entry(entry_path, file_contents) private + def extract_documents + DOCUMENT_PATHS.each do |attr_name, path| + if @zip.find_entry(path) + xml_doc = @zip.read(path) + self.instance_variable_set(:"@#{attr_name}", Nokogiri::XML(xml_doc)) + end + end + end + #-- # TODO: Flesh this out to be compatible with other files # TODO: Method to set flag on files that have been edited, probably by inserting something at the diff --git a/spec/docx/document_spec.rb b/spec/docx/document_spec.rb index a1f76dc..335e525 100755 --- a/spec/docx/document_spec.rb +++ b/spec/docx/document_spec.rb @@ -279,6 +279,22 @@ end end + describe 'multiple documents' do + before do + @doc = Docx::Document.open(@fixtures_path + '/multi_doc.docx') + end + + it 'should extract all inner documents' do + expect(@doc.doc).to_not be_nil + expect(@doc.styles).to_not be_nil + expect(@doc.header).to_not be_nil + expect(@doc.header.text).to eq "Hello from the header." + expect(@doc.footer).to_not be_nil + expect(@doc.footer.text).to eq "Hello from the footer." + expect(@doc.numbering).to_not be_nil + end + end + describe 'saving' do before do @doc = Docx::Document.open(@fixtures_path + '/saving.docx') diff --git a/spec/fixtures/multi_doc.docx b/spec/fixtures/multi_doc.docx new file mode 100644 index 0000000..008d06e Binary files /dev/null and b/spec/fixtures/multi_doc.docx differ