html2odt 0.3.2 → 0.3.3

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
2
  SHA1:
3
- metadata.gz: 4faafbc073e768bc0f1752ffe28e9227bb2a0ff7
4
- data.tar.gz: fba8f90511c1205ec52d7cd466f3573ab2627f9e
3
+ metadata.gz: d6e392a2bb293ff1f5ddade2b2584369040903a6
4
+ data.tar.gz: a95aa2ca695d94b779ea9357cdc9a282871e1a52
5
5
  SHA512:
6
- metadata.gz: 70d0313da825e9fc5e426716d570891f675e1c3ab772149d64c8668011eefd60de1915e2e736f2650726567f11c8eebbe97b48c6e698bfd314df6076611b166a
7
- data.tar.gz: d2830075e1fe36f1be2e78056d3f582396c77b40e7282c503e7cfd899e8571967fef6f75fededef12f6744200ae050a8277e9a7d794c3c6be2c6f0ddbf4d8bd5
6
+ metadata.gz: 6bd769ca10d9cb05d365da4fc37dced0136b5509e94ff21e527ae50f920cb3c470cc4031e4b534856ff5d86ab96f402f6abe88f9df77da12ae07052c989f6ee0
7
+ data.tar.gz: 201dc464672f62141c80486f8a6984e89f56d793829936eedfe23c97b5e6eb86187bf56d2febbf0273c1eda938414a92b90c64ec59c7594e77709ed5e8634f46
data/CHANGELOG.md CHANGED
@@ -1,3 +1,8 @@
1
+ # v0.3.3 - 2016-06-07
2
+
3
+ Properly handle HTTP errors on remote image handling. Improved handling of top
4
+ level inline elements.
5
+
1
6
  # v0.3.2 - 2016-06-07
2
7
 
3
8
  Properly handle errors on remote image handling, bump nokogiri dependency to
data/Rakefile CHANGED
@@ -5,6 +5,7 @@ Rake::TestTask.new(:test) do |t|
5
5
  t.libs << "test"
6
6
  t.libs << "lib"
7
7
  t.test_files = FileList['test/**/*_test.rb']
8
+ t.warning = true
8
9
  end
9
10
 
10
11
  task :default => :test
data/html2odt.gemspec CHANGED
@@ -26,5 +26,5 @@ Gem::Specification.new do |spec|
26
26
  spec.add_development_dependency "bundler", "~> 1.12"
27
27
  spec.add_development_dependency "rake", "~> 10.0"
28
28
  spec.add_development_dependency "minitest", "~> 5.0"
29
- spec.add_development_dependency "byebug"
29
+ spec.add_development_dependency "fakeweb", "~> 1.3"
30
30
  end
@@ -17,6 +17,7 @@ class Html2Odt::Document
17
17
  def initialize(template: Html2Odt::ODT_TEMPLATE, html: nil)
18
18
  @html = html
19
19
  @template = template
20
+ @base_uri = nil
20
21
 
21
22
  read_xmls
22
23
  end
@@ -246,13 +247,8 @@ class Html2Odt::Document
246
247
  def fix_document_structure(html)
247
248
  doc = Nokogiri::HTML::DocumentFragment.parse(html)
248
249
 
249
- # XHTML2ODT cannot handle <code> elements without parent block elements,
250
- # i.e. a containing <pre> or <p>. Adding a <p> in that case
251
- doc.css("code").select { |code| code.ancestors("p, pre").empty? }.each do |code|
252
- p = create_node(doc, "p")
253
- code.add_next_sibling(p)
254
- p.add_child(code)
255
- end
250
+ # Removing undesired elements
251
+ doc.css("script, object, embed, iframe, style, link, map, area").remove
256
252
 
257
253
  # XHTML2ODT cannot handle <br> within <pre> tags properly, replacing them
258
254
  # with new lines should have the same side effects.
@@ -260,9 +256,46 @@ class Html2Odt::Document
260
256
  br.replace("\n")
261
257
  end
262
258
 
259
+ # XHTML2ODT cannot handle inline nodes without containing block elements, so
260
+ # we're wrapping anything, that's a top-level inline element or text node
261
+ # into a newly created p tag, trying to join all sibling inline elements
262
+ # into a single paragraph.
263
+ children = doc.children.to_a
264
+
265
+ previous = nil
266
+ while !children.empty?
267
+ child = children.shift
268
+ if inline_node?(child)
269
+ if previous
270
+ previous.add_child(child)
271
+ elsif child.element? or child.text !~ /\A\s*\z/
272
+ p = create_node(doc, "p")
273
+ child.add_next_sibling(p)
274
+ p.add_child(child)
275
+
276
+ previous = p
277
+ end
278
+ else
279
+ previous = nil
280
+ end
281
+ end
282
+
283
+
263
284
  doc.to_xml(:save_with => Nokogiri::XML::Node::SaveOptions::AS_XML)
264
285
  end
265
286
 
287
+ def inline_node? node
288
+ return true if node.text?
289
+
290
+ # https://developer.mozilla.org/en-US/docs/Web/HTML/Inline_elements
291
+ [
292
+ "b", "big", "i", "small", "tt", "abbr", "acronym", "cite", "code", "dfn",
293
+ "em", "kbd", "strong", "samp", "time", "var", "a", "bdo", "br", "img",
294
+ "map", "object", "q", "script", "span", "sub", "sup", "button", "input",
295
+ "label", "select", "textarea"
296
+ ].include?(node.name)
297
+ end
298
+
266
299
  def fix_links(html)
267
300
  doc = Nokogiri::HTML::DocumentFragment.parse(html)
268
301
 
@@ -364,18 +397,19 @@ class Html2Odt::Document
364
397
  end
365
398
 
366
399
  def uri_to_file(uri)
367
- file = Tempfile.new("html2odt")
368
- file.binmode
369
-
370
400
  Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == "https") do |http|
371
401
  resp = http.get(uri.path)
372
402
 
403
+ return nil unless resp.is_a?(Net::HTTPSuccess)
404
+
405
+ file = Tempfile.new("html2odt")
406
+ file.binmode
407
+
373
408
  file.write(resp.body)
374
409
  file.flush
410
+
375
411
  file
376
412
  end
377
-
378
- file
379
413
  rescue
380
414
  # Could not fetch remote image
381
415
  #
@@ -1,8 +1,9 @@
1
1
  class Html2Odt::Image
2
- attr_accessor :source
2
+ attr_reader :source
3
3
 
4
4
  def initialize(target_base)
5
5
  @target_base = target_base
6
+ @valid = nil
6
7
  end
7
8
 
8
9
  # Assign file instead of source, if you were creating tempfiles and need them
@@ -1,3 +1,3 @@
1
1
  module Html2Odt
2
- VERSION = "0.3.2"
2
+ VERSION = "0.3.3"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: html2odt
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.2
4
+ version: 0.3.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gregor Schmidt (Planio)
@@ -95,19 +95,19 @@ dependencies:
95
95
  - !ruby/object:Gem::Version
96
96
  version: '5.0'
97
97
  - !ruby/object:Gem::Dependency
98
- name: byebug
98
+ name: fakeweb
99
99
  requirement: !ruby/object:Gem::Requirement
100
100
  requirements:
101
- - - '>='
101
+ - - ~>
102
102
  - !ruby/object:Gem::Version
103
- version: '0'
103
+ version: '1.3'
104
104
  type: :development
105
105
  prerelease: false
106
106
  version_requirements: !ruby/object:Gem::Requirement
107
107
  requirements:
108
- - - '>='
108
+ - - ~>
109
109
  - !ruby/object:Gem::Version
110
- version: '0'
110
+ version: '1.3'
111
111
  description: html2odt generates ODT documents based on HTML fragments using xhtml2odt
112
112
  email:
113
113
  - gregor@plan.io