@@ -16,14 +16,16 @@ module Docx
1616 # puts d.text
1717 # end
1818 class Document
19- delegate :paragraphs , :bookmarks , :to => :@parser
20- delegate :doc , :xml , :zip , :to => :@parser
19+ attr_reader :xml , :doc , :zip
20+
2121 def initialize ( path , &block )
2222 @replace = { }
23+ @zip = Zip ::ZipFile . open ( path )
24+ @xml = @zip . read ( 'word/document.xml' )
25+ @doc = Nokogiri ::XML ( @xml )
2326 if block_given?
24- @parser = Parser . new ( File . expand_path ( path ) , &block )
25- else
26- @parser = Parser . new ( File . expand_path ( path ) )
27+ yield self
28+ @zip . close
2729 end
2830 end
2931
@@ -35,6 +37,18 @@ def self.open(path, &block)
3537 self . new ( path , &block )
3638 end
3739
40+ def paragraphs
41+ @doc . xpath ( '//w:document//w:body//w:p' ) . map { |p_node | parse_paragraph_from p_node }
42+ end
43+
44+ def bookmarks
45+ bkmrks_hsh = Hash . new
46+ bkmrks_ary = @doc . xpath ( '//w:bookmarkStart' ) . map { |b_node | parse_bookmark_from b_node }
47+ # auto-generated by office 2010
48+ bkmrks_ary . reject! { |b | b . name == "_GoBack" }
49+ bkmrks_ary . each { |b | bkmrks_hsh [ b . name ] = b }
50+ bkmrks_hsh
51+ end
3852 ##
3953 # *Deprecated*
4054 #
@@ -83,5 +97,14 @@ def update
8397 @replace [ "word/document.xml" ] = doc . serialize :save_with => 0
8498 end
8599
100+ # generate Elements::Containers::Paragraph from paragraph XML node
101+ def parse_paragraph_from ( p_node )
102+ Elements ::Containers ::Paragraph . new ( p_node )
103+ end
104+
105+ # generate Elements::Bookmark from bookmark XML node
106+ def parse_bookmark_from ( b_node )
107+ Elements ::Bookmark . new ( b_node )
108+ end
86109 end
87110end
0 commit comments