|
1 | | -require 'docx/parser' |
2 | | -require 'zip/zip' |
3 | | - |
4 | | -module Docx |
5 | | - class Document |
6 | | - delegate :paragraphs, :bookmarks, :to => :@parser |
7 | | - delegate :doc, :xml, :zip, :to => :@parser |
8 | | - def initialize(path, &block) |
9 | | - @replace = {} |
10 | | - if block_given? |
11 | | - @parser = Parser.new(File.expand_path(path), &block) |
12 | | - else |
13 | | - @parser = Parser.new(File.expand_path(path)) |
14 | | - end |
15 | | - end |
16 | | - |
17 | | - def self.open(path, &block) |
18 | | - self.new(path, &block) |
19 | | - end |
20 | | - |
21 | | - def each_paragraph |
22 | | - paragraphs.each { |p| yield(p) } |
23 | | - end |
24 | | - |
25 | | - def to_s |
26 | | - paragraphs.map(&:to_s).join("\n") |
27 | | - end |
28 | | - |
29 | | - # TODO: Flesh this out to be compatible with other files |
30 | | - # TODO: Method to set flag on files that have been edited, probably by inserting something at the |
31 | | - # end of methods that make edits? |
32 | | - def update |
33 | | - @replace["word/document.xml"] = doc.serialize :save_with => 0 |
34 | | - end |
35 | | - |
36 | | - def save(path) |
37 | | - update |
38 | | - Zip::ZipFile.open(path, Zip::ZipFile::CREATE) do |out| |
39 | | - zip.each do |entry| |
40 | | - out.get_output_stream(entry.name) do |o| |
41 | | - if @replace[entry.name] |
42 | | - o.write(@replace[entry.name]) |
43 | | - else |
44 | | - o.write(zip.read(entry.name)) |
45 | | - end |
46 | | - end |
47 | | - end |
48 | | - end |
49 | | - zip.close |
50 | | - end |
51 | | - |
52 | | - alias_method :text, :to_s |
53 | | - end |
54 | | -end |
| 1 | +require 'docx/parser' |
| 2 | +require 'zip/zip' |
| 3 | + |
| 4 | +module Docx |
| 5 | + class Document |
| 6 | + delegate :paragraphs, :bookmarks, :to => :@parser |
| 7 | + delegate :doc, :xml, :zip, :to => :@parser |
| 8 | + def initialize(path, &block) |
| 9 | + @replace = {} |
| 10 | + if block_given? |
| 11 | + @parser = Parser.new(File.expand_path(path), &block) |
| 12 | + else |
| 13 | + @parser = Parser.new(File.expand_path(path)) |
| 14 | + end |
| 15 | + end |
| 16 | + |
| 17 | + def self.open(path, &block) |
| 18 | + self.new(path, &block) |
| 19 | + end |
| 20 | + |
| 21 | + def each_paragraph |
| 22 | + paragraphs.each { |p| yield(p) } |
| 23 | + end |
| 24 | + |
| 25 | + def to_s |
| 26 | + paragraphs.map(&:to_s).join("\n") |
| 27 | + end |
| 28 | + |
| 29 | + # TODO: Flesh this out to be compatible with other files |
| 30 | + # TODO: Method to set flag on files that have been edited, probably by inserting something at the |
| 31 | + # end of methods that make edits? |
| 32 | + def update |
| 33 | + @replace["word/document.xml"] = doc.serialize :save_with => 0 |
| 34 | + end |
| 35 | + |
| 36 | + def save(path) |
| 37 | + update |
| 38 | + Zip::ZipOutputStream.open(path) do |out| |
| 39 | + zip.each do |entry| |
| 40 | + out.put_next_entry(entry.name) |
| 41 | + |
| 42 | + if @replace[entry.name] |
| 43 | + out.write(@replace[entry.name]) |
| 44 | + else |
| 45 | + out.write(zip.read(entry.name)) |
| 46 | + end |
| 47 | + end |
| 48 | + end |
| 49 | + zip.close |
| 50 | + end |
| 51 | + |
| 52 | + alias_method :text, :to_s |
| 53 | + end |
| 54 | +end |
0 commit comments