prawn-html 0.6.4 → 0.7.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c773502449714d4f0a6f2401721ba67a349731ef9f0c525216864b355fbe76c8
4
- data.tar.gz: 8857a7761f343367a4c6496757c767b6b62b46d0d840df96a7d545c3693c744f
3
+ metadata.gz: 661d2dbfaa7c3d2186ba6e643f552e46031d27571ca0381607202eca3e4cb0ea
4
+ data.tar.gz: 0ae68c59f5c2efb89f6ae561a50b460d81f50e1b96ae958e2a3602049cd5aed5
5
5
  SHA512:
6
- metadata.gz: 51be5bfbbf4427f6124a4a00cfb399017d6b806d8d3d2bb228fc81b7547a3cbaffe535767c5270a2666bed0a9abd89099d156dd79b1ea638d39101e528207be0
7
- data.tar.gz: 520221561fc067919e098349fe6fab940290f6de419b37722679eacbb959055700620539c067d6a57786e7d4396bd0a4659e0f95428b09a96c3a93c55e174bda
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
- pdf_wrapper = PdfWrapper.new(pdf)
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: :unquote },
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] },
@@ -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
- before_content = context.before_content
110
- text = before_content ? ::Oga::HTML::Entities.decode(before_content) : ''
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
- @styles = {}
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
- hash_styles = text_styles.scan(REGEXP_STYLES).to_h
64
- hash_styles.each do |selector, rule|
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
@@ -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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module PrawnHtml # :nodoc:
4
- VERSION = '0.6.4'
4
+ VERSION = '0.7.1'
5
5
  end
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.6.4
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-03 00:00:00.000000000 Z
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