Class: Docx::Document

Inherits:
Object
  • Object
show all
Defined in:
lib/docx/document.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, &block) ⇒ Document

Returns a new instance of Document.



8
9
10
11
12
13
14
15
# File 'lib/docx/document.rb', line 8

def initialize(path, &block)
  @replace = {}
  if block_given?
    @parser = Parser.new(File.expand_path(path), &block)
  else
    @parser = Parser.new(File.expand_path(path))
  end
end

Class Method Details

.open(path, &block) ⇒ Object



17
18
19
# File 'lib/docx/document.rb', line 17

def self.open(path, &block)
  self.new(path, &block)
end

Instance Method Details

#each_paragraphObject



21
22
23
# File 'lib/docx/document.rb', line 21

def each_paragraph
  paragraphs.each { |p| yield(p) }
end

#save(path) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/docx/document.rb', line 36

def save(path)
  update
  Zip::ZipOutputStream.open(path) do |out|
    zip.each do |entry|
      out.put_next_entry(entry.name)

      if @replace[entry.name]
        out.write(@replace[entry.name])
      else
        out.write(zip.read(entry.name))
      end
    end
  end
  zip.close
end

#to_sObject Also known as: text



25
26
27
# File 'lib/docx/document.rb', line 25

def to_s
  paragraphs.map(&:to_s).join("\n")
end

#updateObject

TODO: Flesh this out to be compatible with other files TODO: Method to set flag on files that have been edited, probably by inserting something at the end of methods that make edits?



32
33
34
# File 'lib/docx/document.rb', line 32

def update
  @replace["word/document.xml"] = doc.serialize :save_with => 0
end