prawn-html 0.2.0 → 0.4.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.
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: prawn-html
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.4.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mattia Roccoberton
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-08-20 00:00:00.000000000 Z
11
+ date: 2021-09-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: oga
@@ -52,12 +52,15 @@ files:
52
52
  - lib/prawn_html/callbacks/strike_through.rb
53
53
  - lib/prawn_html/context.rb
54
54
  - lib/prawn_html/document_renderer.rb
55
- - lib/prawn_html/html_handler.rb
55
+ - lib/prawn_html/html_parser.rb
56
+ - lib/prawn_html/pdf_wrapper.rb
56
57
  - lib/prawn_html/tag.rb
57
58
  - lib/prawn_html/tags/a.rb
58
59
  - lib/prawn_html/tags/b.rb
60
+ - lib/prawn_html/tags/blockquote.rb
59
61
  - lib/prawn_html/tags/body.rb
60
62
  - lib/prawn_html/tags/br.rb
63
+ - lib/prawn_html/tags/code.rb
61
64
  - lib/prawn_html/tags/del.rb
62
65
  - lib/prawn_html/tags/div.rb
63
66
  - lib/prawn_html/tags/h.rb
@@ -66,11 +69,16 @@ files:
66
69
  - lib/prawn_html/tags/img.rb
67
70
  - lib/prawn_html/tags/li.rb
68
71
  - lib/prawn_html/tags/mark.rb
72
+ - lib/prawn_html/tags/ol.rb
69
73
  - lib/prawn_html/tags/p.rb
74
+ - lib/prawn_html/tags/pre.rb
70
75
  - lib/prawn_html/tags/small.rb
71
76
  - lib/prawn_html/tags/span.rb
77
+ - lib/prawn_html/tags/sub.rb
78
+ - lib/prawn_html/tags/sup.rb
72
79
  - lib/prawn_html/tags/u.rb
73
80
  - lib/prawn_html/tags/ul.rb
81
+ - lib/prawn_html/utils.rb
74
82
  - lib/prawn_html/version.rb
75
83
  homepage: https://github.com/blocknotes/prawn-html
76
84
  licenses:
@@ -1,66 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module PrawnHtml
4
- class HtmlHandler
5
- # Init the HtmlHandler
6
- #
7
- # @param pdf [Prawn::Document] Target Prawn PDF document
8
- def initialize(pdf)
9
- @processing = false
10
- @renderer = DocumentRenderer.new(pdf)
11
- end
12
-
13
- # Processes HTML and renders it on the PDF document
14
- #
15
- # @param html [String] The HTML content to process
16
- def process(html)
17
- @processing = !html.include?('<body')
18
- doc = Oga.parse_html(html)
19
- traverse_nodes(doc.children)
20
- renderer.flush
21
- end
22
-
23
- private
24
-
25
- attr_reader :processing, :renderer
26
-
27
- def traverse_nodes(nodes)
28
- nodes.each do |node|
29
- element = node_open(node)
30
- traverse_nodes(node.children) if node.children.any?
31
- node_close(element) if element
32
- end
33
- end
34
-
35
- def node_open(node)
36
- tag = node.is_a?(Oga::XML::Element) && init_element(node)
37
- return unless processing
38
- return renderer.on_text_node(node.text) unless tag
39
-
40
- attributes = prepare_attributes(node)
41
- renderer.on_tag_open(tag, attributes)
42
- end
43
-
44
- def init_element(node)
45
- node.name.downcase.to_sym.tap do |tag_name|
46
- @processing = true if tag_name == :body
47
- renderer.assign_document_styles(extract_styles(node.text)) if tag_name == :style && !@processing
48
- end
49
- end
50
-
51
- def extract_styles(text)
52
- text.scan(/\s*([^{\s]+)\s*{\s*([^}]*?)\s*}/m).to_h
53
- end
54
-
55
- def prepare_attributes(node)
56
- node.attributes.each_with_object({}) do |attr, res|
57
- res[attr.name] = attr.value
58
- end
59
- end
60
-
61
- def node_close(element)
62
- renderer.on_tag_close(element) if @processing
63
- @processing = false if element.tag == :body
64
- end
65
- end
66
- end