htmltoword 1.0.0 → 1.1.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA256:
3
- metadata.gz: 3826f44c3db796f2386882c38ee4ff6f8f809fe1fc83e75dfa8b36ad6f9f0fa1
4
- data.tar.gz: 4f30dbf7f94ee5c29984c8915775d08f4e7338233242cb3425c6a51c171af24f
2
+ SHA1:
3
+ metadata.gz: c47d1b5bc4522ff27b68ab48736b845e1879856e
4
+ data.tar.gz: 126c012568a4dbb529194e24c1a84ccd11690807
5
5
  SHA512:
6
- metadata.gz: a2b1a75dc96a10170e6b355af6ee175812672386674cca559bdc65208b0def798c9664b6caca7a73e2932dafc80f4acf8b164aa6ad888ab3240ebf1da694cc07
7
- data.tar.gz: 1e53f03734ae25cd4c09b166e4034e451ea656100d57ed1f0576b6dbc7ad06ae6629691e492cb5cb0689d8571900ec83b1e308e6679e8f67e255ed2da88216cc
6
+ metadata.gz: 215fce4c96862fecf5be83838ae721e7f99a3f170b23a1f04bc4cd0440fba816cef205cfd5d137f884d7be8def32054684a311f035ced6b944750837a974da25
7
+ data.tar.gz: 3c802b88efcb2025195b3ce210e7d5addff8c1d63894e370f886b1f0d9bff27229f56c6654f3c2d635dc160d2fb85564a91718920c4eb2a0b71c946b00cb1b41
data/README.md CHANGED
@@ -148,6 +148,21 @@ To create page breaks simply add a div with class -page-break ie:
148
148
  <div class="-page-break"></div>
149
149
  ````
150
150
 
151
+ ### Images
152
+ Support for images is very basic and is only possible for external images(i.e accessed via URL). If the image doesn't
153
+ have correctly defined it's width and height it won't be included in the document
154
+
155
+ **Limitations:**
156
+ - Images are external i.e. pictures accessed via URL, not stored within document
157
+ - only sizing is customisable
158
+
159
+ Examples:
160
+ ```html
161
+ <img src="http://placehold.it/250x100.png" style="width: 250px; height: 100px">
162
+ <img src="http://placehold.it/250x100.png" data-width="250px" data-height="100px">
163
+ <img src="http://placehold.it/250x100.png" data-height="150px" style="width:250px; height:100px">
164
+ ```
165
+
151
166
  ## Contributing / Extending
152
167
 
153
168
  Word docx files are essentially just a zipped collection of xml files and resources.
@@ -91,7 +91,8 @@ module Htmltoword
91
91
 
92
92
  def replace_files(html, extras = false)
93
93
  html = '<body></body>' if html.nil? || html.empty?
94
- source = Nokogiri::HTML(html.gsub(/>\s+</, '><'))
94
+ original_source = Nokogiri::HTML(html.gsub(/>\s+</, '><'))
95
+ source = xslt(stylesheet_name: 'cleanup').transform(original_source)
95
96
  transform_and_replace(source, xslt_path('numbering'), Document.numbering_xml_file)
96
97
  transform_and_replace(source, xslt_path('relations'), Document.relations_xml_file)
97
98
  transform_doc_xml(source, extras)
@@ -1,3 +1,3 @@
1
1
  module Htmltoword
2
- VERSION = '1.0.0'
2
+ VERSION = '1.1.0'
3
3
  end
@@ -77,7 +77,7 @@
77
77
  </w:p>
78
78
  </xsl:template>
79
79
 
80
- <xsl:template match="div[not(ancestor::li) and not(ancestor::td) and not(ancestor::th) and not(ancestor::p) and not(descendant::div) and not(descendant::p) and not(descendant::h1) and not(descendant::h2) and not(descendant::h3) and not(descendant::h4) and not(descendant::h5) and not(descendant::h6) and not(descendant::table) and not(descendant::li) and not (descendant::pre)]">
80
+ <xsl:template match="div[not(ancestor::li) and not(ancestor::td) and not(ancestor::th) and not(ancestor::p) and not(ancestor::dl) and not(descendant::dl) and not(descendant::div) and not(descendant::p) and not(descendant::h1) and not(descendant::h2) and not(descendant::h3) and not(descendant::h4) and not(descendant::h5) and not(descendant::h6) and not(descendant::table) and not(descendant::li) and not (descendant::pre)]">
81
81
  <xsl:comment>Divs should create a p if nothing above them has and nothing below them will</xsl:comment>
82
82
  <w:p>
83
83
  <xsl:call-template name="text-alignment" />
@@ -178,6 +178,25 @@
178
178
  </xsl:for-each>
179
179
  </xsl:template>
180
180
 
181
+ <xsl:template match="dl">
182
+ <xsl:apply-templates/>
183
+ </xsl:template>
184
+
185
+ <xsl:template match="dt">
186
+ <w:p>
187
+ <xsl:apply-templates/>
188
+ </w:p>
189
+ </xsl:template>
190
+
191
+ <xsl:template match="dd">
192
+ <w:p>
193
+ <w:pPr>
194
+ <w:ind w:left="720"/>
195
+ </w:pPr>
196
+ <xsl:apply-templates/>
197
+ </w:p>
198
+ </xsl:template>
199
+
181
200
  <xsl:template match="span[not(ancestor::td) and not(ancestor::li) and (preceding-sibling::h1 or preceding-sibling::h2 or preceding-sibling::h3 or preceding-sibling::h4 or preceding-sibling::h5 or preceding-sibling::h6 or preceding-sibling::table or preceding-sibling::p or preceding-sibling::ol or preceding-sibling::ul or preceding-sibling::div or following-sibling::h1 or following-sibling::h2 or following-sibling::h3 or following-sibling::h4 or following-sibling::h5 or following-sibling::h6 or following-sibling::table or following-sibling::p or following-sibling::ol or following-sibling::ul or following-sibling::div)]
182
201
  |a[not(ancestor::td) and not(ancestor::li) and (preceding-sibling::h1 or preceding-sibling::h2 or preceding-sibling::h3 or preceding-sibling::h4 or preceding-sibling::h5 or preceding-sibling::h6 or preceding-sibling::table or preceding-sibling::p or preceding-sibling::ol or preceding-sibling::ul or preceding-sibling::div or following-sibling::h1 or following-sibling::h2 or following-sibling::h3 or following-sibling::h4 or following-sibling::h5 or following-sibling::h6 or following-sibling::table or following-sibling::p or following-sibling::ol or following-sibling::ul or following-sibling::div)]
183
202
  |small[not(ancestor::td) and not(ancestor::li) and (preceding-sibling::h1 or preceding-sibling::h2 or preceding-sibling::h3 or preceding-sibling::h4 or preceding-sibling::h5 or preceding-sibling::h6 or preceding-sibling::table or preceding-sibling::p or preceding-sibling::ol or preceding-sibling::ul or preceding-sibling::div or following-sibling::h1 or following-sibling::h2 or following-sibling::h3 or following-sibling::h4 or following-sibling::h5 or following-sibling::h6 or following-sibling::table or following-sibling::p or following-sibling::ol or following-sibling::ul or following-sibling::div)]
@@ -23,6 +23,7 @@
23
23
  <xsl:template match="command"/>
24
24
  <xsl:template match="font"/>
25
25
  <xsl:template match="iframe"/>
26
+ <xsl:template match="img[not(starts-with(@src, 'http://')) and not(starts-with(@src, 'https://'))]"/>
26
27
  <xsl:template match="isindex"/>
27
28
  <xsl:template match="map"/>
28
29
  <xsl:template match="noframes"/>
@@ -37,62 +37,69 @@
37
37
  <xsl:include href="./image_functions.xslt"/>
38
38
 
39
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">
61
- <xsl:with-param name="source" select="@src"/>
62
- <xsl:with-param name="data-filename" select="@data-filename"/>
63
- </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"/>
73
- </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>
40
+ <xsl:choose>
41
+ <xsl:when test="not(@data-width) and not(@data-height) and not(contains(@style, 'width')) and not(contains(@style, 'height'))">
42
+ <!-- Do not transfor images unless width and height are correctly specified -->
43
+ </xsl:when>
44
+ <xsl:otherwise>
45
+ <w:drawing>
46
+ <wp:inline distT="0" distB="0" distL="0" distR="0">
47
+ <wp:extent>
48
+ <xsl:call-template name="image-dimention-attributes"/>
49
+ </wp:extent>
50
+ <wp:effectExtent l="0" t="0" r="0" b="0"/>
51
+ <wp:docPr>
52
+ <xsl:attribute name="id"><xsl:value-of select="count(preceding::img)+1" /></xsl:attribute>
53
+ <xsl:attribute name="name">Picture <xsl:value-of select="count(preceding::img)+1" /></xsl:attribute>
54
+ </wp:docPr>
55
+ <wp:cNvGraphicFramePr>
56
+ <a:graphicFrameLocks noChangeAspect="1"/>
57
+ </wp:cNvGraphicFramePr>
58
+ <a:graphic>
59
+ <a:graphicData uri="http://schemas.openxmlformats.org/drawingml/2006/picture">
60
+ <pic:pic>
61
+ <pic:nvPicPr>
62
+ <pic:cNvPr>
63
+ <xsl:attribute name="id"><xsl:value-of select="count(preceding::img)+1" /></xsl:attribute>
64
+ <xsl:attribute name="title"><xsl:value-of select="@alt" /></xsl:attribute>
65
+ <xsl:attribute name="name"><xsl:call-template name="image-name">
66
+ <xsl:with-param name="source" select="@src"/>
67
+ <xsl:with-param name="data-filename" select="@data-filename"/>
68
+ </xsl:call-template></xsl:attribute>
69
+ </pic:cNvPr>
70
+ <pic:cNvPicPr/>
71
+ </pic:nvPicPr>
72
+ <pic:blipFill>
73
+ <a:blip>
74
+ <xsl:attribute name="r:embed"><xsl:call-template name="relationship-id"/></xsl:attribute>
75
+ <a:extLst>
76
+ <a:ext uri="{{28A0092B-C50C-407E-A947-70E740481C1C}}">
77
+ <a14:useLocalDpi val="0"/>
78
+ </a:ext>
79
+ </a:extLst>
80
+ </a:blip>
81
+ <a:stretch>
82
+ <a:fillRect/>
83
+ </a:stretch>
84
+ </pic:blipFill>
85
+ <pic:spPr>
86
+ <a:xfrm>
87
+ <a:off x="0" y="0"/>
88
+ <a:ext>
89
+ <xsl:call-template name="image-dimention-attributes"/>
90
+ </a:ext>
91
+ </a:xfrm>
92
+ <a:prstGeom prst="rect">
93
+ <a:avLst/>
94
+ </a:prstGeom>
95
+ </pic:spPr>
96
+ </pic:pic>
97
+ </a:graphicData>
98
+ </a:graphic>
99
+ </wp:inline>
100
+ </w:drawing>
101
+ </xsl:otherwise>
102
+ </xsl:choose>
96
103
  </xsl:template>
97
104
 
98
105
  <!--
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: htmltoword
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nicholas Frandsen, Cristina Matonte
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-05-15 00:00:00.000000000 Z
11
+ date: 2018-07-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: actionpack
@@ -175,7 +175,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
175
175
  version: '0'
176
176
  requirements: []
177
177
  rubyforge_project:
178
- rubygems_version: 2.7.3
178
+ rubygems_version: 2.6.13
179
179
  signing_key:
180
180
  specification_version: 4
181
181
  summary: This simple gem allows you to create MS Word docx documents from simple html