avv2word 1.1.16 → 1.1.17

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 30b26cca5c3cc29c8d63096def055c7eb386bc49c57a8dfa1fca441c49007525
4
- data.tar.gz: 152b590592b4aa81f0ba6f3c4d3b35623843886025b144be234fa5c54675b4f2
3
+ metadata.gz: 3cbebc1afb001bc7dda9e38d4cd2f8baf1024eeaadcdc093d2ec98f4dc3512fd
4
+ data.tar.gz: 27607e2db08645c80f924c665adf051800204f5f81a1f35947c54ba42afc5a06
5
5
  SHA512:
6
- metadata.gz: 3fdb6953ee7de97c2335ac9db5f2a43a3ca7d83fddbd18e60f3cc256275607cc05744a911115c44b3caadccde777640b5027c52368a2d8c301e029868d397243
7
- data.tar.gz: c23b06f957c03497d6727c4390831850cabbd31c41908d25a10b3bf0f47378f2e1c71ad72d76dca62f75fcddbf19ab1b51b58ba941fffc03bea7787bb237c72b
6
+ metadata.gz: 965cee217f0a047a274baa21f166151e4019aba32b7f6bfb7c826fda9ee0a2303e0455f41e528b23b67e54d99a1b666d3af88e8809a5d8b17b5651ff526cabd5
7
+ data.tar.gz: b0ef73b72c1e6c83724cf1d563300cf94b59eac7e6494a82d15f0f47e810fb815c5d78cdea979a39689409a8d5f4dd0ef192dbdf32d0c65a65a0188957647299
@@ -4,7 +4,33 @@ module Avv2word
4
4
 
5
5
  class << self
6
6
  include TemplatesHelper
7
+
8
+ def extract_images_html(html)
9
+ require "base64"
10
+ require "fileutils"
11
+
12
+ # create working dir
13
+ FileUtils.mkdir_p("tmp_imgs")
14
+ resize = 1 # for testing purposes; half size: 0.5
15
+
16
+ doc = Nokogiri::HTML.fragment(html)
17
+ doc.css("img").each_with_index do |img_elm,i|
18
+ mime_type, img_data = img_elm.attributes["src"].value.split(",")
19
+ # ^--data:image/jpeg;base64
20
+ img_path = "tmp_imgs/image_#{i}.jpg"
21
+ File.open(img_path,"w"){|f| f.write Base64.decode64(img_data)}
22
+ img_elm.attributes["src"].value = img_path
23
+ match_dimensions = %x( file #{img_path} ).match(/, (?<x>\d+) ?x ?(?<y>\d+),/)
24
+ img_elm["style"] = "width:#{match_dimensions[:x].to_i*resize}px;height:#{match_dimensions[:y].to_i*resize}px"
25
+ # img_elm["style"] = "width:100.5px;height:100.5px"
26
+ # alt: style="width:350px;height:150px"
27
+ # alt: img_elm["data-width"] = "236px"
28
+ end
29
+ doc.to_html
30
+ end
31
+
7
32
  def create(content, template_name = nil, extras = false)
33
+ content = extract_images_html(content)
8
34
  template_name += extension if template_name && !template_name.end_with?(extension)
9
35
  document = new(template_file(template_name))
10
36
  document.replace_files(content, extras)
@@ -130,10 +156,27 @@ module Avv2word
130
156
 
131
157
  private
132
158
 
159
+ def inject_image_relations(relations, noko_html_source)
160
+ rel_doc = Nokogiri::XML(relations)
161
+ relations_root = rel_doc.css("Relationships").first
162
+
163
+ noko_html_source.css("img").each_with_index do |img,i|
164
+ new_relation = relations_root.css("Relationship").last.clone
165
+ new_relation["Id"] = "rId#{i+10}"
166
+ new_relation["Type"] = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image"
167
+ new_relation["Target"] = "media/image#{i+1}#{File.extname(img[:src])}"
168
+ relations_root.add_child new_relation
169
+ end
170
+ rel_doc.to_xml
171
+ end
172
+
133
173
  def transform_and_replace(source, stylesheet_path, file, remove_ns = false)
134
174
  stylesheet = xslt(stylesheet_path: stylesheet_path)
135
175
  content = stylesheet.apply_to(source)
136
176
  content.gsub!(/\s*xmlns:(\w+)="(.*?)\s*"/, '') if remove_ns
177
+ if ( file =~ /document\.xml\.rels/ ) && ( source.css("img").size > 0 )
178
+ content = inject_image_relations(content, source)
179
+ end
137
180
  @replaceable_files[file] = content
138
181
  end
139
182
 
@@ -1,3 +1,3 @@
1
1
  module Avv2word
2
- VERSION = '1.1.16'
2
+ VERSION = '1.1.17'
3
3
  end
@@ -33,5 +33,5 @@
33
33
  </func:function>
34
34
 
35
35
  <!-- template as function used to return the relationship id of the element (currently links or images) -->
36
- <xsl:template name="relationship-id">rId<xsl:value-of select="count(preceding::a[starts-with(@href, 'http://') or starts-with(@href, 'https://')])+count(preceding::img)+8"/></xsl:template>
36
+ <xsl:template name="relationship-id">rId<xsl:value-of select="count(preceding::a[starts-with(@href, 'http://') or starts-with(@href, 'https://')])+count(preceding::img)+10"/></xsl:template>
37
37
  </xsl:stylesheet>
@@ -36,64 +36,66 @@
36
36
 
37
37
  <xsl:include href="./image_functions.xslt"/>
38
38
 
39
- <xsl:template match="img|body/img" name="image">
40
- <w:drawing>
41
- <wp:inline distT="0" distB="0" distL="0" distR="0">
42
- <wp:extent>
43
- <xsl:call-template name="image-dimention-attributes"/>
44
- </wp:extent>
45
- <wp:effectExtent l="0" t="0" r="0" b="0"/>
46
- <wp:docPr>
47
- <xsl:attribute name="id"><xsl:value-of select="count(preceding::img)+1" /></xsl:attribute>
48
- <xsl:attribute name="name">Picture <xsl:value-of select="count(preceding::img)+1" /></xsl:attribute>
49
- </wp:docPr>
50
- <wp:cNvGraphicFramePr>
51
- <a:graphicFrameLocks noChangeAspect="1"/>
52
- </wp:cNvGraphicFramePr>
53
- <a:graphic>
54
- <a:graphicData uri="http://schemas.openxmlformats.org/drawingml/2006/picture">
55
- <pic:pic>
56
- <pic:nvPicPr>
57
- <pic:cNvPr>
58
- <xsl:attribute name="id"><xsl:value-of select="count(preceding::img)+1" /></xsl:attribute>
59
- <xsl:attribute name="title"><xsl:value-of select="@alt" /></xsl:attribute>
60
- <xsl:attribute name="name"><xsl:call-template name="image-name">
39
+ <xsl:template match="img|body/img" name="image">
40
+ <w:r>
41
+ <w:drawing>
42
+ <wp:inline distT="0" distB="0" distL="0" distR="0">
43
+ <wp:extent>
44
+ <xsl:call-template name="image-dimention-attributes"/>
45
+ </wp:extent>
46
+ <wp:effectExtent l="0" t="0" r="0" b="0"/>
47
+ <wp:docPr>
48
+ <xsl:attribute name="id"><xsl:value-of select="count(preceding::img)+1" /></xsl:attribute>
49
+ <xsl:attribute name="name">Picture <xsl:value-of select="count(preceding::img)+1" /></xsl:attribute>
50
+ </wp:docPr>
51
+ <wp:cNvGraphicFramePr>
52
+ <a:graphicFrameLocks noChangeAspect="1"/>
53
+ </wp:cNvGraphicFramePr>
54
+ <a:graphic>
55
+ <a:graphicData uri="http://schemas.openxmlformats.org/drawingml/2006/picture">
56
+ <pic:pic>
57
+ <pic:nvPicPr>
58
+ <pic:cNvPr>
59
+ <xsl:attribute name="id"><xsl:value-of select="count(preceding::img)+1" /></xsl:attribute>
60
+ <xsl:attribute name="title"><xsl:value-of select="@alt" /></xsl:attribute>
61
+ <xsl:attribute name="name"><xsl:call-template name="image-name">
61
62
  <xsl:with-param name="source" select="@src"/>
62
63
  <xsl:with-param name="data-filename" select="@data-filename"/>
63
64
  </xsl:call-template></xsl:attribute>
64
- </pic:cNvPr>
65
- <pic:cNvPicPr/>
66
- </pic:nvPicPr>
67
- <pic:blipFill>
68
- <a:blip>
69
- <xsl:attribute name="r:embed"><xsl:call-template name="relationship-id"/></xsl:attribute>
70
- <a:extLst>
71
- <a:ext uri="{{28A0092B-C50C-407E-A947-70E740481C1C}}">
72
- <a14:useLocalDpi val="0"/>
65
+ </pic:cNvPr>
66
+ <pic:cNvPicPr/>
67
+ </pic:nvPicPr>
68
+ <pic:blipFill>
69
+ <a:blip>
70
+ <xsl:attribute name="r:embed"><xsl:call-template name="relationship-id"/></xsl:attribute>
71
+ <a:extLst>
72
+ <a:ext uri="{{28A0092B-C50C-407E-A947-70E740481C1C}}">
73
+ <a14:useLocalDpi val="0"/>
74
+ </a:ext>
75
+ </a:extLst>
76
+ </a:blip>
77
+ <a:stretch>
78
+ <a:fillRect/>
79
+ </a:stretch>
80
+ </pic:blipFill>
81
+ <pic:spPr>
82
+ <a:xfrm>
83
+ <a:off x="0" y="0"/>
84
+ <a:ext>
85
+ <xsl:call-template name="image-dimention-attributes"/>
73
86
  </a:ext>
74
- </a:extLst>
75
- </a:blip>
76
- <a:stretch>
77
- <a:fillRect/>
78
- </a:stretch>
79
- </pic:blipFill>
80
- <pic:spPr>
81
- <a:xfrm>
82
- <a:off x="0" y="0"/>
83
- <a:ext>
84
- <xsl:call-template name="image-dimention-attributes"/>
85
- </a:ext>
86
- </a:xfrm>
87
- <a:prstGeom prst="rect">
88
- <a:avLst/>
89
- </a:prstGeom>
90
- </pic:spPr>
91
- </pic:pic>
92
- </a:graphicData>
93
- </a:graphic>
94
- </wp:inline>
95
- </w:drawing>
96
- </xsl:template>
87
+ </a:xfrm>
88
+ <a:prstGeom prst="rect">
89
+ <a:avLst/>
90
+ </a:prstGeom>
91
+ </pic:spPr>
92
+ </pic:pic>
93
+ </a:graphicData>
94
+ </a:graphic>
95
+ </wp:inline>
96
+ </w:drawing>
97
+ </w:r>
98
+ </xsl:template>
97
99
 
98
100
  <!--
99
101
  A style specifiying the width and height is required to render an image correctly in word.
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: avv2word
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.16
4
+ version: 1.1.17
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marián Bilas
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-06-05 00:00:00.000000000 Z
11
+ date: 2019-06-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: actionpack