Skip to content

Commit 3c91a6b

Browse files
committed
Merge branch 'dev'
Conflicts: lib/docx/document.rb lib/docx/parser.rb
2 parents 122220d + 5a6a3c7 commit 3c91a6b

2 files changed

Lines changed: 40 additions & 60 deletions

File tree

lib/docx/document.rb

100644100755
Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
require 'docx/parser'
1+
require 'docx/containers'
2+
require 'docx/elements'
3+
require 'nokogiri'
24
require 'zip/zip'
35

46
module Docx
@@ -16,14 +18,16 @@ module Docx
1618
# puts d.text
1719
# end
1820
class Document
19-
delegate :paragraphs, :bookmarks, :tables, :to => :@parser
20-
delegate :doc, :xml, :zip, :to => :@parser
21+
attr_reader :xml, :doc, :zip
22+
2123
def initialize(path, &block)
2224
@replace = {}
25+
@zip = Zip::ZipFile.open(path)
26+
@xml = @zip.read('word/document.xml')
27+
@doc = Nokogiri::XML(@xml)
2328
if block_given?
24-
@parser = Parser.new(File.expand_path(path), &block)
25-
else
26-
@parser = Parser.new(File.expand_path(path))
29+
yield self
30+
@zip.close
2731
end
2832
end
2933

@@ -35,6 +39,23 @@ def self.open(path, &block)
3539
self.new(path, &block)
3640
end
3741

42+
def paragraphs
43+
@doc.xpath('//w:document//w:body//w:p').map { |p_node| parse_paragraph_from p_node }
44+
end
45+
46+
def bookmarks
47+
bkmrks_hsh = Hash.new
48+
bkmrks_ary = @doc.xpath('//w:bookmarkStart').map { |b_node| parse_bookmark_from b_node }
49+
# auto-generated by office 2010
50+
bkmrks_ary.reject! {|b| b.name == "_GoBack" }
51+
bkmrks_ary.each {|b| bkmrks_hsh[b.name] = b }
52+
bkmrks_hsh
53+
end
54+
55+
def tables
56+
@doc.xpath('//w:document//w:body//w:tbl').map { |t_node| parse_table_from t_node }
57+
end
58+
3859
##
3960
# *Deprecated*
4061
#
@@ -83,5 +104,18 @@ def update
83104
@replace["word/document.xml"] = doc.serialize :save_with => 0
84105
end
85106

107+
# generate Elements::Containers::Paragraph from paragraph XML node
108+
def parse_paragraph_from(p_node)
109+
Elements::Containers::Paragraph.new(p_node)
110+
end
111+
112+
# generate Elements::Bookmark from bookmark XML node
113+
def parse_bookmark_from(b_node)
114+
Elements::Bookmark.new(b_node)
115+
end
116+
117+
def parse_table_from(t_node)
118+
Elements::Containers::Table.new(t_node)
119+
end
86120
end
87121
end

lib/docx/parser.rb

Lines changed: 0 additions & 54 deletions
This file was deleted.

0 commit comments

Comments
 (0)