prawn-html 0.6.4 → 0.7.1
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 +17 -0
- data/lib/prawn-html.rb +2 -4
- data/lib/prawn_html/attributes.rb +1 -1
- data/lib/prawn_html/context.rb +6 -0
- data/lib/prawn_html/document_renderer.rb +5 -2
- data/lib/prawn_html/html_parser.rb +13 -4
- data/lib/prawn_html/instance.rb +18 -0
- data/lib/prawn_html/utils.rb +11 -1
- data/lib/prawn_html/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 661d2dbfaa7c3d2186ba6e643f552e46031d27571ca0381607202eca3e4cb0ea
|
4
|
+
data.tar.gz: 0ae68c59f5c2efb89f6ae561a50b460d81f50e1b96ae958e2a3602049cd5aed5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7ec9c6055dbb336fe9b845ecd274bc6524a0bcaa5a5e0cf2911f01506c4c8d6a6c5c50ab70140f6c8bac134764a79ee46dfa7c80ccadb043d48f656c1f4f3af1
|
7
|
+
data.tar.gz: 4c48e9cca75c7ec86d28c0c9304c1ff92b98f14389007da6e55189092c5e8503d77bd2d5cc379e8f0aa333850d2e6cc79ce19a2a4921778c2289afb1f186cac1
|
data/README.md
CHANGED
@@ -35,6 +35,23 @@ pdf.render_file('test.pdf')
|
|
35
35
|
|
36
36
|
To check some examples with the PDF output see [examples](examples/) folder.
|
37
37
|
|
38
|
+
Alternative form using _PrawnHtml::Instance_ to preserve the context:
|
39
|
+
|
40
|
+
```rb
|
41
|
+
require 'prawn-html'
|
42
|
+
pdf = Prawn::Document.new(page_size: 'A4')
|
43
|
+
phtml = PrawnHtml::Instance.new(pdf)
|
44
|
+
css = <<~CSS
|
45
|
+
h1 { color: green }
|
46
|
+
i { color: red }
|
47
|
+
CSS
|
48
|
+
phtml.append(css: css)
|
49
|
+
phtml.append(html: '<h1>Some <i>HTML</i> before</h1>')
|
50
|
+
pdf.text 'Some Prawn text'
|
51
|
+
phtml.append(html: '<h1>Some <i>HTML</i> after</h1>')
|
52
|
+
pdf.render_file('test.pdf')
|
53
|
+
```
|
54
|
+
|
38
55
|
## Supported tags & attributes
|
39
56
|
|
40
57
|
HTML tags (using MDN definitions):
|
data/lib/prawn-html.rb
CHANGED
@@ -156,10 +156,7 @@ module PrawnHtml
|
|
156
156
|
}.freeze
|
157
157
|
|
158
158
|
def append_html(pdf, html)
|
159
|
-
|
160
|
-
renderer = DocumentRenderer.new(pdf_wrapper)
|
161
|
-
html_parser = PrawnHtml::HtmlParser.new(renderer)
|
162
|
-
html_parser.process(html)
|
159
|
+
PrawnHtml::Instance.new(pdf).append(html: html)
|
163
160
|
end
|
164
161
|
|
165
162
|
module_function :append_html
|
@@ -179,3 +176,4 @@ require 'prawn_html/context'
|
|
179
176
|
require 'prawn_html/pdf_wrapper'
|
180
177
|
require 'prawn_html/document_renderer'
|
181
178
|
require 'prawn_html/html_parser'
|
179
|
+
require 'prawn_html/instance'
|
@@ -18,7 +18,7 @@ module PrawnHtml
|
|
18
18
|
# text node styles
|
19
19
|
'background' => { key: :callback, set: :callback_background },
|
20
20
|
'color' => { key: :color, set: :convert_color },
|
21
|
-
'font-family' => { key: :font, set: :
|
21
|
+
'font-family' => { key: :font, set: :filter_font_family },
|
22
22
|
'font-size' => { key: :size, set: :convert_size },
|
23
23
|
'font-style' => { key: :styles, set: :append_styles, values: %i[italic] },
|
24
24
|
'font-weight' => { key: :styles, set: :append_styles, values: %i[bold] },
|
data/lib/prawn_html/context.rb
CHANGED
@@ -62,6 +62,12 @@ module PrawnHtml
|
|
62
62
|
end
|
63
63
|
end
|
64
64
|
|
65
|
+
# :nocov:
|
66
|
+
def inspect
|
67
|
+
map(&:class).map(&:to_s).join(', ')
|
68
|
+
end
|
69
|
+
# :nocov:
|
70
|
+
|
65
71
|
# Remove the last element from the context
|
66
72
|
def remove_last
|
67
73
|
last.on_context_remove(self) if last.respond_to?(:on_context_remove)
|
@@ -9,6 +9,7 @@ module PrawnHtml
|
|
9
9
|
#
|
10
10
|
# @param pdf [PdfWrapper] target PDF wrapper
|
11
11
|
def initialize(pdf)
|
12
|
+
@before_content = []
|
12
13
|
@buffer = []
|
13
14
|
@context = Context.new
|
14
15
|
@last_margin = 0
|
@@ -42,6 +43,7 @@ module PrawnHtml
|
|
42
43
|
options = { width: pdf.page_width, height: pdf.page_height }
|
43
44
|
tag_class.new(tag_name, attributes: attributes, options: options).tap do |element|
|
44
45
|
setup_element(element, element_styles: element_styles)
|
46
|
+
@before_content.push(element.before_content) if element.respond_to?(:before_content)
|
45
47
|
@last_tag_open = true
|
46
48
|
end
|
47
49
|
end
|
@@ -106,8 +108,9 @@ module PrawnHtml
|
|
106
108
|
end
|
107
109
|
|
108
110
|
def prepare_text(content)
|
109
|
-
|
110
|
-
|
111
|
+
text = @before_content.any? ? ::Oga::HTML::Entities.decode(@before_content.join) : ''
|
112
|
+
@before_content.clear
|
113
|
+
|
111
114
|
return (@last_text = text + content) if context.white_space_pre?
|
112
115
|
|
113
116
|
content = content.lstrip if @last_text[-1] == ' ' || @last_tag_open
|
@@ -15,19 +15,28 @@ module PrawnHtml
|
|
15
15
|
@ignore = false
|
16
16
|
@ignore_content_tags = ignore_content_tags
|
17
17
|
@renderer = renderer
|
18
|
-
@
|
18
|
+
@raw_styles = {}
|
19
19
|
end
|
20
20
|
|
21
21
|
# Processes HTML and renders it
|
22
22
|
#
|
23
23
|
# @param html [String] The HTML content to process
|
24
24
|
def process(html)
|
25
|
+
@styles = {}
|
25
26
|
@processing = !html.include?('<body')
|
26
27
|
@document = Oga.parse_html(html)
|
28
|
+
process_styles # apply previously loaded styles
|
27
29
|
traverse_nodes(document.children)
|
28
30
|
renderer.flush
|
29
31
|
end
|
30
32
|
|
33
|
+
# Parses CSS styles
|
34
|
+
#
|
35
|
+
# @param text_styles [String] The CSS styles to evaluate
|
36
|
+
def parse_styles(text_styles)
|
37
|
+
@raw_styles = text_styles.scan(REGEXP_STYLES).to_h
|
38
|
+
end
|
39
|
+
|
31
40
|
private
|
32
41
|
|
33
42
|
attr_reader :document, :ignore, :processing, :renderer, :styles
|
@@ -59,9 +68,9 @@ module PrawnHtml
|
|
59
68
|
end
|
60
69
|
end
|
61
70
|
|
62
|
-
def process_styles(text_styles)
|
63
|
-
|
64
|
-
|
71
|
+
def process_styles(text_styles = nil)
|
72
|
+
parse_styles(text_styles) if text_styles
|
73
|
+
@raw_styles.each do |selector, rule|
|
65
74
|
document.css(selector).each do |node|
|
66
75
|
styles[node] = rule
|
67
76
|
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module PrawnHtml
|
4
|
+
class Instance
|
5
|
+
attr_reader :html_parser, :pdf_wrapper, :renderer
|
6
|
+
|
7
|
+
def initialize(pdf)
|
8
|
+
@pdf_wrapper = PrawnHtml::PdfWrapper.new(pdf)
|
9
|
+
@renderer = PrawnHtml::DocumentRenderer.new(@pdf_wrapper)
|
10
|
+
@html_parser = PrawnHtml::HtmlParser.new(@renderer)
|
11
|
+
end
|
12
|
+
|
13
|
+
def append(css: nil, html: nil)
|
14
|
+
html_parser.parse_styles(css) if css
|
15
|
+
html_parser.process(html) if html
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
data/lib/prawn_html/utils.rb
CHANGED
@@ -100,6 +100,16 @@ module PrawnHtml
|
|
100
100
|
value
|
101
101
|
end
|
102
102
|
|
103
|
+
# Filter font family
|
104
|
+
#
|
105
|
+
# @param value [String] string value
|
106
|
+
#
|
107
|
+
# @return [Symbol] unquoted font family or nil if the input value is 'inherit'
|
108
|
+
def filter_font_family(value, options: nil)
|
109
|
+
result = unquote(value, options: options)
|
110
|
+
result == 'inherit' ? nil : result
|
111
|
+
end
|
112
|
+
|
103
113
|
# Normalize a style value
|
104
114
|
#
|
105
115
|
# @param value [String] string value
|
@@ -124,6 +134,6 @@ module PrawnHtml
|
|
124
134
|
end
|
125
135
|
|
126
136
|
module_function :callback_background, :callback_strike_through, :convert_color, :convert_float, :convert_size,
|
127
|
-
:convert_symbol, :copy_value, :normalize_style, :unquote
|
137
|
+
:convert_symbol, :copy_value, :filter_font_family, :normalize_style, :unquote
|
128
138
|
end
|
129
139
|
end
|
data/lib/prawn_html/version.rb
CHANGED
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.
|
4
|
+
version: 0.7.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mattia Roccoberton
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-05
|
11
|
+
date: 2022-09-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: oga
|
@@ -53,6 +53,7 @@ files:
|
|
53
53
|
- lib/prawn_html/context.rb
|
54
54
|
- lib/prawn_html/document_renderer.rb
|
55
55
|
- lib/prawn_html/html_parser.rb
|
56
|
+
- lib/prawn_html/instance.rb
|
56
57
|
- lib/prawn_html/pdf_wrapper.rb
|
57
58
|
- lib/prawn_html/tag.rb
|
58
59
|
- lib/prawn_html/tags/a.rb
|