docx_manipulator 0.0.3 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,3 +1,3 @@
1
1
  class DocxManipulator
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -11,6 +11,7 @@ class DocxManipulator
11
11
  @target = target
12
12
  @new_relationships = source_relationships
13
13
  @images = {}
14
+ @binary_images = {}
14
15
  end
15
16
 
16
17
  def source_content
@@ -52,6 +53,15 @@ class DocxManipulator
52
53
  new_relationships.root << image_node
53
54
  end
54
55
 
56
+ def add_binary_image(id, name, data)
57
+ @binary_images[name] = data
58
+ image_node = Nokogiri::XML::Node.new('Relationship', new_relationships)
59
+ image_node['Id'] = id
60
+ image_node['Type'] = 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/image'
61
+ image_node['Target'] = "media/#{name}"
62
+ new_relationships.root << image_node
63
+ end
64
+
55
65
  def process
56
66
  Zip::ZipOutputStream.open(target) do |os|
57
67
  Zip::ZipFile.foreach(source) do |entry|
@@ -71,6 +81,11 @@ class DocxManipulator
71
81
  IO.copy_stream file, os
72
82
  end
73
83
  end
84
+
85
+ @binary_images.each do |name, data|
86
+ os.put_next_entry "word/media/#{name}"
87
+ os.write data
88
+ end
74
89
  end
75
90
  end
76
91
 
@@ -59,6 +59,20 @@ EOF
59
59
  end
60
60
  end
61
61
 
62
+ describe '#add_binary_image' do
63
+ let(:image) { File.new(File.join(File.dirname(__FILE__), 'files', 'duck.jpeg'), 'rb') }
64
+ it 'adds an image in binary format to the docx' do
65
+ data = image.read
66
+ subject.add_binary_image 'rId18', 'duck.jpeg', data
67
+ subject.new_relationships.to_s.should =~ /<Relationship Id="rId18" Type="http:\/\/schemas.openxmlformats.org\/officeDocument\/2006\/relationships\/image" Target="media\/duck.jpeg"\/>/
68
+ subject.process
69
+
70
+ Zip::ZipFile.open('spec/files/result.docx') do |file|
71
+ file.read('word/media/duck.jpeg').should == data
72
+ end
73
+ end
74
+ end
75
+
62
76
  describe "#process" do
63
77
  after :each do
64
78
  File.delete 'spec/files/result.docx'
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: docx_manipulator
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.3
5
+ version: 0.0.4
6
6
  platform: ruby
7
7
  authors:
8
8
  - "Michael St\xC3\xA4mpfli"
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2012-04-02 00:00:00 Z
13
+ date: 2012-04-03 00:00:00 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rspec