Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added headers to document #22

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
.bundle
Gemfile.lock
pkg/*
doc/
doc/
vendor/ruby
6 changes: 5 additions & 1 deletion lib/docx/document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ module Docx
# puts d.text
# end
class Document
attr_reader :xml, :doc, :zip, :styles
attr_reader :xml, :doc, :zip, :styles, :headers

def initialize(path, &block)
@replace = {}
Expand All @@ -27,6 +27,8 @@ def initialize(path, &block)
@doc = Nokogiri::XML(@document_xml)
@styles_xml = @zip.read('word/styles.xml')
@styles = Nokogiri::XML(@styles_xml)
@header_paths = @zip.glob('word/header*.xml').map { |h| h.name }
@headers = Hash[@header_paths.map { |h| [h, ::Nokogiri::XML(@zip.read(h))] }]
if block_given?
yield self
@zip.close
Expand Down Expand Up @@ -57,6 +59,7 @@ def paragraphs
def bookmarks
bkmrks_hsh = Hash.new
bkmrks_ary = @doc.xpath('//w:bookmarkStart').map { |b_node| parse_bookmark_from b_node }
bkmrks_ary += @headers.values.map { |h| h.xpath('//w:bookmarkStart').map { |b_node| parse_bookmark_from b_node } }.flatten
# auto-generated by office 2010
bkmrks_ary.reject! {|b| b.name == "_GoBack" }
bkmrks_ary.each {|b| bkmrks_hsh[b.name] = b }
Expand Down Expand Up @@ -129,6 +132,7 @@ def replace_entry(entry_path, file_contents)
#++
def update
replace_entry "word/document.xml", doc.serialize(:save_with => 0)
@headers.each { |path, h| replace_entry(path, h.serialize(:save_with => 0)) }
end

# generate Elements::Containers::Paragraph from paragraph XML node
Expand Down
31 changes: 31 additions & 0 deletions spec/docx/document_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -422,5 +422,36 @@
end
end
end

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

it 'should read the document' do
expect(@doc.headers.size).to eq(3)
end

it 'should read bookmarks' do
expect(@doc.bookmarks.size).to eq(2)
expect(@doc.bookmarks['header_bookmark']).to_not eq(nil)
end

it 'should be able to make changes in a header bookmark' do
@doc.bookmarks['header_bookmark'].insert_text_after('testing')
expect(@doc.headers['word/header2.xml'].to_html).to include('testing')
end

it 'should save changes in a header bookmark' do
@doc.bookmarks['header_bookmark'].insert_text_after('testing')
expect(@doc.headers['word/header2.xml'].to_html).to include('testing')

@new_doc_path = @fixtures_path + '/new_save.docx'
@doc.save(@new_doc_path)
@new_doc = Docx::Document.open(@new_doc_path)
expect(@doc.headers['word/header2.xml'].to_html).to include('testing')
File.exists?(@new_doc_path) if File.delete(@new_doc_path)
end
end
end

Binary file added spec/fixtures/headers.docx
Binary file not shown.