prawn-html 0.6.2 → 0.7.0

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: 105884df1bf42d6aa44ff0a102a025960740c0d5a771c27e75949113cd7b9b70
4
- data.tar.gz: abe3998f3f65cd2b421280d99151f18e321ac650dd8abed26260062e6f8e455a
3
+ metadata.gz: 6d7b30ab7d50313d9b5f16fd37bba84207725c8368ba3370a2ef037bdf871f27
4
+ data.tar.gz: 730c56224e0961d707db073b550f605e4dd4f39d6a902cbb2af5e9f4533e4785
5
5
  SHA512:
6
- metadata.gz: 0dc20b1ee2c6bd1321b4359e31e217cea923c68f5f3af242b097d56d9492f198e1df6af263cadcf75978c83700a90cad3cfbae08d25e9b0ebb39ddea6ad2c059
7
- data.tar.gz: 88180bff0e1aac88d566911fafac9c6b6ae799eee6c1f77948f0bc811825f03c2e7ca2c8b1bf8962dc60e27ba355904d77289e3870e1c3faecd8c362e6536237
6
+ metadata.gz: 790833d6338e59019751025c38a1b37ffa44ff583dd3f23771bc1015754cc1f047a6da9aa2d28c975191de8451ecc164d7dda412c2df15ca779c27b5aa1bc27a
7
+ data.tar.gz: 3ad2faf9178136a477fa4c0952410593219f85e6cacfbc88d1d504fbbe70388184ee62d3a4e84844eb0a8f225ba04845214d41beff68aa665bed12b03f563547
data/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # Prawn HTML
2
2
  [![gem version](https://badge.fury.io/rb/prawn-html.svg)](https://rubygems.org/gems/prawn-html)
3
+ [![gem downloads](https://badgen.net/rubygems/dt/prawn-html)](https://rubygems.org/gems/prawn-html)
4
+ [![maintainability](https://api.codeclimate.com/v1/badges/db674db00817d56ca1e9/maintainability)](https://codeclimate.com/github/blocknotes/prawn-html/maintainability)
3
5
  [![linters](https://github.com/blocknotes/prawn-html/actions/workflows/linters.yml/badge.svg)](https://github.com/blocknotes/prawn-html/actions/workflows/linters.yml)
4
6
  [![specs](https://github.com/blocknotes/prawn-html/actions/workflows/specs.yml/badge.svg)](https://github.com/blocknotes/prawn-html/actions/workflows/specs.yml)
5
7
 
@@ -33,6 +35,23 @@ pdf.render_file('test.pdf')
33
35
 
34
36
  To check some examples with the PDF output see [examples](examples/) folder.
35
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
+
36
55
  ## Supported tags & attributes
37
56
 
38
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'
@@ -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,7 +108,9 @@ module PrawnHtml
106
108
  end
107
109
 
108
110
  def prepare_text(content)
109
- text = context.before_content ? ::Oga::HTML::Entities.decode(context.before_content) : ''
111
+ text = @before_content.any? ? ::Oga::HTML::Entities.decode(@before_content.join) : ''
112
+ @before_content.clear
113
+
110
114
  return (@last_text = text + content) if context.white_space_pre?
111
115
 
112
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
@@ -13,7 +13,9 @@ module PrawnHtml
13
13
  end
14
14
 
15
15
  def before_content
16
- @counter ? "#{@counter}. " : "#{@symbol} "
16
+ return if @before_content_once
17
+
18
+ @before_content_once = @counter ? "#{@counter}. " : "#{@symbol} "
17
19
  end
18
20
 
19
21
  def block_styles
@@ -45,7 +45,7 @@ module PrawnHtml
45
45
 
46
46
  if val.match /\A#([a-f0-9]{3})\Z/ # rubocop:disable Performance/RedundantMatch
47
47
  r, g, b = Regexp.last_match[1].chars
48
- return r * 2 + g * 2 + b * 2
48
+ return (r * 2) + (g * 2) + (b * 2)
49
49
  end
50
50
  if val.match /\Argb\s*\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\)\Z/ # rubocop:disable Performance/RedundantMatch
51
51
  r, g, b = Regexp.last_match[1..3].map { |v| v.to_i.to_s(16) }
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module PrawnHtml # :nodoc:
4
- VERSION = '0.6.2'
4
+ VERSION = '0.7.0'
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.2
4
+ version: 0.7.0
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-09-20 00:00:00.000000000 Z
11
+ date: 2022-06-10 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
@@ -83,7 +84,10 @@ files:
83
84
  homepage: https://github.com/blocknotes/prawn-html
84
85
  licenses:
85
86
  - MIT
86
- metadata: {}
87
+ metadata:
88
+ homepage_uri: https://github.com/blocknotes/prawn-html
89
+ source_code_uri: https://github.com/blocknotes/prawn-html
90
+ rubygems_mfa_required: 'true'
87
91
  post_install_message:
88
92
  rdoc_options: []
89
93
  require_paths:
@@ -99,7 +103,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
99
103
  - !ruby/object:Gem::Version
100
104
  version: '0'
101
105
  requirements: []
102
- rubygems_version: 3.1.4
106
+ rubygems_version: 3.1.6
103
107
  signing_key:
104
108
  specification_version: 4
105
109
  summary: Prawn PDF - HTML renderer