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.
- checksums.yaml +4 -4
- data/README.md +45 -6
- data/lib/prawn-html.rb +170 -15
- data/lib/prawn_html/attributes.rb +33 -101
- data/lib/prawn_html/callbacks/highlight.rb +2 -4
- data/lib/prawn_html/callbacks/strike_through.rb +4 -4
- data/lib/prawn_html/context.rb +16 -3
- data/lib/prawn_html/document_renderer.rb +39 -27
- data/lib/prawn_html/html_parser.rb +95 -0
- data/lib/prawn_html/pdf_wrapper.rb +94 -0
- data/lib/prawn_html/tag.rb +13 -22
- data/lib/prawn_html/tags/a.rb +1 -1
- data/lib/prawn_html/tags/b.rb +1 -3
- data/lib/prawn_html/tags/blockquote.rb +25 -0
- data/lib/prawn_html/tags/br.rb +2 -3
- data/lib/prawn_html/tags/code.rb +13 -0
- data/lib/prawn_html/tags/del.rb +1 -3
- data/lib/prawn_html/tags/h.rb +6 -6
- data/lib/prawn_html/tags/hr.rb +6 -8
- data/lib/prawn_html/tags/i.rb +1 -3
- data/lib/prawn_html/tags/img.rb +7 -7
- data/lib/prawn_html/tags/li.rb +7 -4
- data/lib/prawn_html/tags/mark.rb +1 -3
- data/lib/prawn_html/tags/ol.rb +26 -0
- data/lib/prawn_html/tags/p.rb +4 -4
- data/lib/prawn_html/tags/pre.rb +25 -0
- data/lib/prawn_html/tags/sub.rb +13 -0
- data/lib/prawn_html/tags/sup.rb +13 -0
- data/lib/prawn_html/tags/u.rb +1 -3
- data/lib/prawn_html/tags/ul.rb +1 -3
- data/lib/prawn_html/utils.rb +109 -0
- data/lib/prawn_html/version.rb +1 -1
- metadata +11 -3
- data/lib/prawn_html/html_handler.rb +0 -66
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
|
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-
|
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/
|
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
|