sablon 0.3.0 → 0.3.1
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.
- checksums.yaml +4 -4
- data/Gemfile.lock +3 -3
- data/lib/sablon/content.rb +30 -1
- data/lib/sablon/html/ast.rb +1 -1
- data/lib/sablon/processor/document/blocks.rb +11 -0
- data/lib/sablon/version.rb +1 -1
- data/test/content_test.rb +19 -0
- data/test/fixtures/images_sample.docx +0 -0
- data/test/sablon_test.rb +2 -2
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 47a5aeb4ef473eea715114b4a32f0e2a5ab63711
|
4
|
+
data.tar.gz: 2335675beefc2ff842609cf7be53c2146c6bb4cb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a66e1e936397fbc66858216b2259c4665a52d32c754c416ad71705c36d2bf2e6deff152421ee67eebce4d643ee8ecf3d543746f00a921b0533ce44dcfe5bd5ab
|
7
|
+
data.tar.gz: '0053920d8f9de09ab35885f68c79cc63f46f72b19874c2970c4e3b0655915c210af7f0c0836d8447c8a556955d937b9535a1abe426de4ed46444e3c3ba75a35b'
|
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
sablon (0.3.
|
4
|
+
sablon (0.3.1)
|
5
5
|
nokogiri (>= 1.6.0)
|
6
6
|
rubyzip (>= 1.1.1)
|
7
7
|
|
@@ -10,10 +10,10 @@ GEM
|
|
10
10
|
specs:
|
11
11
|
mini_portile2 (2.3.0)
|
12
12
|
minitest (5.10.3)
|
13
|
-
nokogiri (1.8.
|
13
|
+
nokogiri (1.8.5)
|
14
14
|
mini_portile2 (~> 2.3.0)
|
15
15
|
rake (12.3.0)
|
16
|
-
rubyzip (1.2.
|
16
|
+
rubyzip (1.2.2)
|
17
17
|
xml-simple (1.1.5)
|
18
18
|
|
19
19
|
PLATFORMS
|
data/lib/sablon/content.rb
CHANGED
@@ -173,8 +173,9 @@ module Sablon
|
|
173
173
|
end
|
174
174
|
|
175
175
|
# Handles reading image data and inserting it into the document
|
176
|
-
class Image < Struct.new(:name, :data, :
|
176
|
+
class Image < Struct.new(:name, :data, :properties)
|
177
177
|
attr_reader :rid_by_file
|
178
|
+
attr_accessor :local_rid
|
178
179
|
|
179
180
|
def self.id; :image end
|
180
181
|
def self.wraps?(value) false end
|
@@ -196,11 +197,23 @@ module Sablon
|
|
196
197
|
#
|
197
198
|
super name, img_data
|
198
199
|
@attributes = attributes
|
200
|
+
@properties = @attributes.fetch("properties", {})
|
201
|
+
|
199
202
|
# rId's are separate for each XML file but I want to be able
|
200
203
|
# to reuse the actual image file itself.
|
201
204
|
@rid_by_file = {}
|
202
205
|
end
|
203
206
|
|
207
|
+
def width
|
208
|
+
return unless (width_str = @properties[:width])
|
209
|
+
convert_to_emu(width_str)
|
210
|
+
end
|
211
|
+
|
212
|
+
def height
|
213
|
+
return unless (height_str = @properties[:height])
|
214
|
+
convert_to_emu(height_str)
|
215
|
+
end
|
216
|
+
|
204
217
|
def append_to(paragraph, display_node, env) end
|
205
218
|
|
206
219
|
private
|
@@ -223,6 +236,22 @@ module Sablon
|
|
223
236
|
#
|
224
237
|
[File.basename(name), source.read]
|
225
238
|
end
|
239
|
+
|
240
|
+
# Convert centimeters or inches to Word specific emu format
|
241
|
+
def convert_to_emu(dim_str)
|
242
|
+
value, unit = dim_str.match(/(^\.?\d+\.?\d*)(\w+)/).to_a[1..-1]
|
243
|
+
value = value.to_f
|
244
|
+
|
245
|
+
if unit == "cm"
|
246
|
+
value = value * 360000
|
247
|
+
elsif unit == "in"
|
248
|
+
value = value * 914400
|
249
|
+
else
|
250
|
+
throw ArgumentError, "Unsupported unit '#{unit}', only 'cm' and 'in' are permitted."
|
251
|
+
end
|
252
|
+
|
253
|
+
value.round()
|
254
|
+
end
|
226
255
|
end
|
227
256
|
|
228
257
|
register Sablon::Content::String
|
data/lib/sablon/html/ast.rb
CHANGED
@@ -480,7 +480,7 @@ module Sablon
|
|
480
480
|
class Run < Node
|
481
481
|
PROPERTIES = %w[b i caps color dstrike emboss imprint highlight outline
|
482
482
|
rStyle shadow shd smallCaps strike sz u vanish
|
483
|
-
vertAlign].freeze
|
483
|
+
vertAlign rFonts].freeze
|
484
484
|
|
485
485
|
def initialize(_env, node, properties)
|
486
486
|
super
|
@@ -102,6 +102,17 @@ module Sablon
|
|
102
102
|
pic_prop.attributes['name'].value = image.name if pic_prop
|
103
103
|
blip = node.at_xpath('.//a:blip', a: 'http://schemas.openxmlformats.org/drawingml/2006/main')
|
104
104
|
blip.attributes['embed'].value = image.local_rid if blip
|
105
|
+
drawing_size = node.at_xpath('.//wp:extent')
|
106
|
+
|
107
|
+
# if image properties are defined, the size of the placeholder image
|
108
|
+
# should be replaced with the actual values
|
109
|
+
if image.width && image.height
|
110
|
+
drawing_size.attributes['cx'].value = image.width.to_s if drawing_size
|
111
|
+
drawing_size.attributes['cy'].value = image.height.to_s if drawing_size
|
112
|
+
pic_size = node.at_xpath('.//a:xfrm//a:ext', a: 'http://schemas.openxmlformats.org/drawingml/2006/main')
|
113
|
+
pic_size.attributes['cx'].value = image.width.to_s if pic_size
|
114
|
+
pic_size.attributes['cy'].value = image.height.to_s if pic_size
|
115
|
+
end
|
105
116
|
end
|
106
117
|
end
|
107
118
|
#
|
data/lib/sablon/version.rb
CHANGED
data/test/content_test.rb
CHANGED
@@ -287,4 +287,23 @@ class ContentImageTest < Sablon::TestCase
|
|
287
287
|
Sablon.content(:image, data)
|
288
288
|
end
|
289
289
|
end
|
290
|
+
|
291
|
+
def test_width_conversion
|
292
|
+
img = Sablon.content(:image, @image_path.to_s, properties: {width: '1.0cm'})
|
293
|
+
assert_equal 360000, img.width
|
294
|
+
assert_nil img.height
|
295
|
+
end
|
296
|
+
|
297
|
+
def test_height_conversion
|
298
|
+
img = Sablon.content(:image, @image_path.to_s, properties: {height: '1.0in'})
|
299
|
+
assert_nil img.width
|
300
|
+
assert_equal 914400, img.height
|
301
|
+
end
|
302
|
+
|
303
|
+
def test_invalid_unit_conversion
|
304
|
+
img = Sablon.content(:image, @image_path.to_s, properties: {width: '100px'})
|
305
|
+
assert_raises ArgumentError do
|
306
|
+
img.width
|
307
|
+
end
|
308
|
+
end
|
290
309
|
end
|
Binary file
|
data/test/sablon_test.rb
CHANGED
@@ -150,12 +150,12 @@ class SablonImagesTest < Sablon::TestCase
|
|
150
150
|
template = Sablon.template @template_path
|
151
151
|
#
|
152
152
|
# setup two image contents to allow quick reuse
|
153
|
-
r2d2 = Sablon.content(:image, @image_fixtures.join('r2d2.jpg').to_s)
|
153
|
+
r2d2 = Sablon.content(:image, @image_fixtures.join('r2d2.jpg').to_s, properties: {height: '1cm', width: '1cm'})
|
154
154
|
c3po = Sablon.content(:image, @image_fixtures.join('c3po.jpg'))
|
155
155
|
darth = Sablon.content(:image, @image_fixtures.join('darth_vader.jpg'))
|
156
156
|
#
|
157
157
|
im_data = StringIO.new(IO.binread(@image_fixtures.join('clone.jpg')))
|
158
|
-
trooper = Sablon.content(:image, im_data, filename: 'clone.jpg')
|
158
|
+
trooper = Sablon.content(:image, im_data, filename: 'clone.jpg', properties: {height: '8cm', width: '4cm'})
|
159
159
|
#
|
160
160
|
# with the following context setup all trooper should be reused and
|
161
161
|
# only a single file added to media. R2D2 should get duplicated in the
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sablon
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Yves Senn
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-10-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: nokogiri
|