Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion lib/docx/document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ module Docx
class Document
include Docx::SimpleInspect

attr_reader :xml, :doc, :zip, :styles, :headers
attr_reader :xml, :doc, :zip, :styles, :headers, :footers

def initialize(path_or_io, options = {})
@replace = {}
Expand All @@ -41,6 +41,7 @@ def initialize(path_or_io, options = {})
@doc = Nokogiri::XML(@document_xml)
load_styles
load_headers
load_footers
yield(self) if block_given?
ensure
@zip.close unless @zip.nil?
Expand Down Expand Up @@ -210,6 +211,15 @@ def load_headers
@headers = Hash[filename_and_contents_pairs]
end

def load_footers
footer_files = @zip.glob("word/footer*.xml").map{|h| h.name}
filename_and_contents_pairs = footer_files.map do |file|
simple_file_name = file.sub(/^word\//, "").sub(/\.xml$/, "")
[simple_file_name, Nokogiri::XML(@zip.read(file))]
end
@footers = Hash[filename_and_contents_pairs]
end

def load_styles
@styles_xml = @zip.read('word/styles.xml')
@styles = Nokogiri::XML(@styles_xml)
Expand Down
12 changes: 12 additions & 0 deletions spec/docx/document_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,18 @@
end
end

describe 'read footers' do
before do
@doc = Docx::Document.open(@fixtures_path + '/multi_doc.docx')
end

it 'can extract footers' do
expect(@doc.footers).to_not be_nil
expect(@doc.footers.keys).to eq ["footer1"]
expect(@doc.footers["footer1"].text).to eq "Hello from the footer."
end
end

describe 'read tables' do
before do
@doc = Docx::Document.open(@fixtures_path + '/tables.docx')
Expand Down
Loading