qnd_html2page 0.2.1 → 0.2.2

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
  SHA256:
3
- metadata.gz: a72421b4710f629052f764efd5f2613cb0eadba4f5dc910914794d5fc3b93e8e
4
- data.tar.gz: 8ed7eb5ee811967f7ab35954ccac3e50dae8f7e3f8a9853d032087d55b65227d
3
+ metadata.gz: 51029b1be2ac9cb9e95d6567f99dc1c6fdda70c3a8d5904372f0ee07dcd2d78f
4
+ data.tar.gz: d9ff95df9754003699ce0a0bea93ebf308c7ff808bd5a8a6e450f016aba45c70
5
5
  SHA512:
6
- metadata.gz: cca59f8cbe1e762ccf55d8329990cecffd5bdff19e7d221d713c7b1d3feaf57bf9a6b4404b0bcb9ecf571d26746ef1efc27797b56e4a5f4d2f1262c5cf3029c5
7
- data.tar.gz: d11b6f38420d004b25c1ead22a59ec2c79ff5af2659cc98c7fda8517f8d4a0011fa087373eb61a401cc239b38e3f79dadd6a86af62dadc398a744d3bce8c18b4
6
+ metadata.gz: 5496cc9c5f98b5d4dd0a68686983a42cac7f97c451b56acca42520dfc77aa0a5e5721851a9d4d0c970ad6c078f7e714ab14fa91f7e414e74dfb518bac8e6b559
7
+ data.tar.gz: 8330416d589c832e06df5147eb648617c92cd75809905803a10fffa5da1b4aca5ae81372c7c1cd7dae21d5b3d0bbd0a02f35312dbf1d6e614f2cf0051d09fec9
Binary file
data.tar.gz.sig CHANGED
Binary file
@@ -39,9 +39,16 @@ class QndHtml2Page
39
39
 
40
40
  private
41
41
 
42
- def scan(html)
43
-
44
- # add the span tag after almost every element in the body
42
+ def scan(obj)
43
+
44
+ raw_html = obj.is_a?(Rexle) ? obj.xml : obj
45
+
46
+ # <br/> acts as a hard page break
47
+ html = raw_html.gsub(/<br\s*\/>/) do |x|
48
+ '<span class="qndhtml2pg">pagebreak' + x.object_id.to_s + '</span>'
49
+ end
50
+
51
+ # add the span tag after almost every element in the body
45
52
 
46
53
  doc = Rexle.new(html)
47
54
  body = doc.root.element('body')
@@ -68,11 +75,16 @@ class QndHtml2Page
68
75
 
69
76
  browser = Ferrum::Browser.new
70
77
  browser.goto('file://' + tmpfile.path + '.html')
71
- span_list = browser.xpath('//span')
72
- a = span_list.map {|x| [x.text, x.find_position.last] }
78
+ span_list = browser.xpath('//span[@class="qndhtml2pg"]')
79
+
80
+ maxheight = span_list.last.find_position.last
73
81
 
82
+ a = span_list.map do |x|
83
+ ypos = x.text[/^pagebreak/] ? maxheight : x.find_position.last
84
+ [x.text, ypos]
85
+ end
74
86
 
75
- heights = ((a.last.last) / @height).round.to_i.times\
87
+ heights = ((maxheight) / @height).round.to_i.times\
76
88
  .inject([@height]) {|r, x| r << (r.last + @height) }
77
89
 
78
90
  puts ('heights: ' + heights.inspect).debug if @debug
@@ -84,7 +96,12 @@ class QndHtml2Page
84
96
  puts ('x: ' + x.inspect).debug if @debug
85
97
  puts ('height: ' + height.inspect).debug if @debug
86
98
 
87
- x.last < height ? (r.last << x) : (height = heights.shift; r << [x])
99
+ if x.first[/^pagebreak/] then
100
+ r << [x]
101
+ else
102
+ x.last < height ? (r.last << x) : (height = heights.shift; r << [x])
103
+ end
104
+
88
105
  r
89
106
 
90
107
  end
@@ -106,9 +123,12 @@ class QndHtml2Page
106
123
  pages = elements.slice_at(*stops).map do |e_list|
107
124
 
108
125
  div = Rexle::Element.new 'div'
109
- e_list.reject! {|e| e.name == 'span' and
110
- e.attributes[:class] == 'qndhtml2pg' }
126
+ e_list.reject! do |e|
127
+ r = e.name == 'span' and e.attributes[:class] == 'qndhtml2pg'
128
+ r
129
+ end
111
130
  next if e_list.empty?
131
+
112
132
  e_list.each {|e| div.add e}
113
133
 
114
134
  div
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: qnd_html2page
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Robertson
metadata.gz.sig CHANGED
Binary file