prawn-markup 0.3.4 → 0.3.7

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a5217761bc64b6cf5ae6697d326934a9b87d3e54a3a65428f9c7025d3a03f043
4
- data.tar.gz: c3274d3dfd6ab699f4d9fe17db8148305c9b4c4c7da8cd3b2a8b834735380583
3
+ metadata.gz: 3224b605102e828f529ef5acdd6ba99910237a58e7a609a631c670b1bb025559
4
+ data.tar.gz: fc939e927be4748051a2ab4d9f0b8c4155aacd1234c3c7dc41eb1e9861d5533c
5
5
  SHA512:
6
- metadata.gz: 728da10a3ce7d80aca95a3b9d54d9122c9c791567ab150188c6f459bb0de15863524322d091ed01900e844661aa0e5337542fd191f03eb97d080e27b905cc394
7
- data.tar.gz: c701c14bcc7fd0baa8cb17179f5096caef7a8f8ce829f8d548d9667d5ce78f2ec2248a1848eca32e01cb0e52e94befb0a845a9d6da0f14c851fb195420e9c90f
6
+ metadata.gz: 5099a48640adb9c06a706cd63bf70767365f74361e1c80dc0501fda5c0e23999d6d7ff1264edfe8aaf45aa05af06dfb285d8d9324f8e6a750b9817184c676dcc
7
+ data.tar.gz: b6725ba940801a114a41d3645605406f947c4f6dcb0e35bc7901ee12cc10241591aee889bfae956ab11f26e7a835e45aa5a18395f712748b4cd0d08e14b7c838
data/README.md CHANGED
@@ -37,14 +37,14 @@ doc.markup('<p>Hello World</p><hr/><p>KTHXBYE</p>')
37
37
 
38
38
  This gem parses the given HTML and layouts the following elements in a vertical order:
39
39
 
40
- * Text blocks: `p`, `div`, `ol`, `ul`, `li`, `hr`, `br`
41
- * Text semantics: `a`, `b`, `strong`, `i`, `em`, `u`, `s`, `del`, `sub`, `sup`
42
- * Headings: `h1`, `h2`, `h3`, `h4`, `h5`, `h6`
43
- * Tables: `table`, `tr`, `td`, `th`
44
- * Media: `img`, `iframe`
45
- * Inputs: `type=checkbox`, `type=radio`
40
+ - Text blocks: `p`, `div`, `ol`, `ul`, `li`, `hr`, `br`
41
+ - Text semantics: `a`, `b`, `strong`, `i`, `em`, `u`, `s`, `del`, `sub`, `sup`, `color`
42
+ - Headings: `h1`, `h2`, `h3`, `h4`, `h5`, `h6`
43
+ - Tables: `table`, `tr`, `td`, `th`
44
+ - Media: `img`, `iframe`
45
+ - Inputs: `type=checkbox`, `type=radio`
46
46
 
47
- All other elements are ignored, their content is added to the parent element. With a few exceptions, no CSS is processed. One exception is the `width` property of `img`, `td` and `th`, which may contain values in `cm`, `mm`, `px`, `pt`, `%` or `auto`.
47
+ All other elements are ignored, their content is added to the parent element. With a few exceptions, no CSS is processed. One exception is the `width` property of `img`, `td` and `th`, which may contain values in `cm`, `mm`, `px`, `pt`, `%` or `auto`. Another exception is the `rgb` or `cmyk` properties of the Prawn-specific `color` tag.
48
48
 
49
49
  If no explicit loader is given (see above), images are loaded from `http(s)` addresses or may be contained in the `src` attribute as base64 encoded data URIs. Prawn only supports `PNG` and `JPG`.
50
50
 
@@ -75,40 +75,40 @@ Tables and lists are rendered with [prawn-table](https://github.com/prawnpdf/pra
75
75
 
76
76
  Beside these options handled by Prawn / prawn-table, the following values may be customized:
77
77
 
78
- * `:text`
79
- * `:preprocessor`: A proc/callable that is called each time before a chunk of text is rendered.
80
- * `:margin_bottom`: Margin after each `<p>`, `<ol>`, `<ul>` or `<table>`. Defaults to about half a line.
81
- * `:heading1-6`
82
- * `:margin_top`: Margin before a heading. Default is 0.
83
- * `:margin_bottom`: Margin after a heading. Default is 0.
84
- * `:table`
85
- * `:placeholder`
86
- * `:too_large`: If the table content does not fit into the current bounding box, this text/callable is rendered instead. Defaults to '[table content too large]'.
87
- * `:subtable_too_large`: If the content of a subtable cannot be fitted into the table, this text is rendered instead. Defaults to '[nested tables with automatic width are not supported]'.
88
- * `:list`
89
- * `:vertical_margin`: Margin at the top and the bottom of a list. Default is 5.
90
- * `:bullet`
91
- * `:char`: The text used as bullet in unordered lists. Default is '•'.
92
- * `:margin`: Margin before the bullet. Default is 10.
93
- * `:content`
94
- * `:margin`: Margin between the bullet and the content. Default is 10.
95
- * `:placeholder`
96
- * `:too_large`: If the list content does not fit into the current bounding box, this text/callable is rendered instead. Defaults to '[list content too large]'.
97
- * `:image`
98
- * `:loader`: A callable that accepts the `src` attribute as an argument an returns a value understood by Prawn's `image` method. Loads `http(s)` URLs and base64 encoded data URIs by default.
99
- * `:placeholder`: If an image is not supported, this text/callable is rendered instead. Defaults to '[unsupported image]'.
100
- * `:iframe`
101
- * `:placeholder`: If the HTML contains IFrames, this text/callable is rendered instead.
102
- A callable gets the URL of the IFrame as an argument. Defaults to ignore iframes.
103
- * `:input`
104
- * `:symbol_font`: A special font to print checkboxes and radios. Prawn's standard fonts do not support special unicode characters. Do not forget to update the document's `font_families`.
105
- * `:symbol_font_size`: The size of the special font to print checkboxes and radios.
106
- * `:checkbox`
107
- * `:checked`: The char to print for a checked checkbox. Default is '☑'.
108
- * `:unchecked`: The char to print for an unchecked checkbox. Default is '☐'.
109
- * `:radio`
110
- * `:checked`: The char to print for a checked radio. Default is '◉'.
111
- * `:unchecked`: The char to print for an unchecked radio. Default is '○'.
78
+ - `:text`
79
+ - `:preprocessor`: A proc/callable that is called each time before a chunk of text is rendered.
80
+ - `:margin_bottom`: Margin after each `<p>`, `<ol>`, `<ul>` or `<table>`. Defaults to about half a line.
81
+ - `:heading1-6`
82
+ - `:margin_top`: Margin before a heading. Default is 0.
83
+ - `:margin_bottom`: Margin after a heading. Default is 0.
84
+ - `:table`
85
+ - `:placeholder`
86
+ - `:too_large`: If the table content does not fit into the current bounding box, this text/callable is rendered instead. Defaults to '[table content too large]'.
87
+ - `:subtable_too_large`: If the content of a subtable cannot be fitted into the table, this text is rendered instead. Defaults to '[nested tables with automatic width are not supported]'.
88
+ - `:list`
89
+ - `:vertical_margin`: Margin at the top and the bottom of a list. Default is 5.
90
+ - `:bullet`
91
+ - `:char`: The text used as bullet in unordered lists. Default is '•'.
92
+ - `:margin`: Margin before the bullet. Default is 10.
93
+ - `:content`
94
+ - `:margin`: Margin between the bullet and the content. Default is 10.
95
+ - `:placeholder`
96
+ - `:too_large`: If the list content does not fit into the current bounding box, this text/callable is rendered instead. Defaults to '[list content too large]'.
97
+ - `:image`
98
+ - `:loader`: A callable that accepts the `src` attribute as an argument an returns a value understood by Prawn's `image` method. Loads `http(s)` URLs and base64 encoded data URIs by default.
99
+ - `:placeholder`: If an image is not supported, this text/callable is rendered instead. Defaults to '[unsupported image]'.
100
+ - `:iframe`
101
+ - `:placeholder`: If the HTML contains IFrames, this text/callable is rendered instead.
102
+ A callable gets the URL of the IFrame as an argument. Defaults to ignore iframes.
103
+ - `:input`
104
+ - `:symbol_font`: A special font to print checkboxes and radios. Prawn's standard fonts do not support special unicode characters. Do not forget to update the document's `font_families`.
105
+ - `:symbol_font_size`: The size of the special font to print checkboxes and radios.
106
+ - `:checkbox`
107
+ - `:checked`: The char to print for a checked checkbox. Default is '☑'.
108
+ - `:unchecked`: The char to print for an unchecked checkbox. Default is '☐'.
109
+ - `:radio`
110
+ - `:checked`: The char to print for a checked radio. Default is '◉'.
111
+ - `:unchecked`: The char to print for an unchecked radio. Default is '○'.
112
112
 
113
113
  ## Development
114
114
 
@@ -128,4 +128,4 @@ The gem is available as open source under the terms of the [MIT License](https:/
128
128
 
129
129
  ## Code of Conduct
130
130
 
131
- Everyone interacting in the Prawn::Markup project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/puzzle/prawn-markup/blob/master/CODE_OF_CONDUCT.md).
131
+ Everyone interacting in the Prawn::Markup project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/puzzle/prawn-markup/blob/main/CODE_OF_CONDUCT.md).
@@ -98,7 +98,7 @@ module Prawn
98
98
  end
99
99
 
100
100
  def content_width
101
- column_widths.last && column_widths.last - content_margin
101
+ column_widths.last && (column_widths.last - content_margin)
102
102
  end
103
103
 
104
104
  def compute_column_widths
@@ -110,9 +110,10 @@ module Prawn
110
110
  end
111
111
 
112
112
  def bullet_text_width
113
+ largest_string = bullet('0' * list.items.size.digits.size)
113
114
  font = bullet_font
115
+ encoded = font.normalize_encoding(largest_string)
114
116
  font_size = column_cell_style(:bullet)[:size] || pdf.font_size
115
- encoded = font.normalize_encoding(bullet(list.items.size))
116
117
  font.compute_width_of(encoded, size: font_size)
117
118
  end
118
119
 
@@ -34,10 +34,12 @@ module Prawn
34
34
  hash.dup.tap do |image_hash|
35
35
  image_hash.delete(:width)
36
36
  image_hash[:image_width] = SizeConverter.new(max_width).parse(hash[:width])
37
- if max_width
38
- natural_width, _height = natural_image_dimensions(image_hash)
39
- image_hash[:fit] = [max_width, 999_999] if max_width < natural_width
37
+ natural_width, _height = natural_image_dimensions(image_hash)
38
+ if max_width && (max_width < natural_width)
39
+ image_hash[:fit] = [max_width, 999_999]
40
40
  end
41
+ rescue Prawn::Errors::UnsupportedImageType
42
+ image_hash.clear
41
43
  end
42
44
  end
43
45
 
@@ -131,7 +131,7 @@ module Prawn
131
131
 
132
132
  def normalize_cell_hash(node, width, style_options)
133
133
  if width.nil? && total_width
134
- width = total_width - column_width_sum - (columns_without_width - 1) * MIN_COL_WIDTH
134
+ width = total_width - column_width_sum - ((columns_without_width - 1) * MIN_COL_WIDTH)
135
135
  end
136
136
  super(node, width, style_options)
137
137
  end
@@ -190,13 +190,13 @@ module Prawn
190
190
 
191
191
  def increase_widths(sum)
192
192
  diff = total_width - sum
193
- column_widths.map! { |w| w + w / sum * diff }
193
+ column_widths.map! { |w| w + (w / sum * diff) }
194
194
  end
195
195
 
196
196
  def decrease_widths(sum)
197
197
  sum += columns_without_width * MIN_COL_WIDTH
198
198
  diff = sum - total_width
199
- column_widths.map! { |w| w ? [w - w / sum * diff, 0].max : nil }
199
+ column_widths.map! { |w| w ? [w - (w / sum * diff), 0].max : nil }
200
200
  end
201
201
 
202
202
  def failover_on_error
@@ -210,7 +210,7 @@ module Prawn
210
210
 
211
211
  def horizontal_padding
212
212
  @horizontal_padding ||= begin
213
- padding = table_options[:cell][:padding] || [DEFAULT_CELL_PADDING] * 4
213
+ padding = table_options[:cell][:padding] || ([DEFAULT_CELL_PADDING] * 4)
214
214
  padding.is_a?(Array) ? padding[1] + padding[3] : padding
215
215
  end
216
216
  end
@@ -57,7 +57,7 @@ module Prawn
57
57
  def default_options_with_size(level)
58
58
  default = text_options.dup
59
59
  default[:size] ||= pdf.font_size
60
- default[:size] *= 2.5 - level * 0.25
60
+ default[:size] *= 2.5 - (level * 0.25)
61
61
  HashMerger.deep(default, options[:"heading#{level}"] || {})
62
62
  end
63
63
 
@@ -75,7 +75,7 @@ module Prawn
75
75
  if src =~ %r{^https?:/}
76
76
  begin
77
77
  URI.parse(src).open
78
- rescue OpenURI::HTTPError, SocketError
78
+ rescue StandardError # OpenURI::HTTPError, SocketError or anything else
79
79
  nil
80
80
  end
81
81
  end
@@ -90,9 +90,12 @@ module Prawn
90
90
  end
91
91
 
92
92
  def add_cell_image(cell)
93
+ src = current_attrs['src']
94
+ return if src.to_s.strip.empty?
95
+
93
96
  add_cell_text_node(cell)
94
- img = image_properties(current_attrs['src'])
95
- cell.nodes << img || invalid_image_placeholder
97
+ img = image_properties(src)
98
+ (cell.nodes << img) || invalid_image_placeholder
96
99
  end
97
100
 
98
101
  def add_table(cells)
@@ -6,7 +6,7 @@ module Prawn
6
6
  def self.prepended(base)
7
7
  base.known_elements.push(
8
8
  'a', 'b', 'strong', 'i', 'em', 'u', 'strikethrough', 'strike', 's', 'del',
9
- 'sub', 'sup'
9
+ 'sub', 'sup', 'color', 'font'
10
10
  )
11
11
  end
12
12
 
@@ -78,6 +78,31 @@ module Prawn
78
78
  append_text('</sup>')
79
79
  end
80
80
 
81
+ def start_color
82
+ rgb, c, m, y, k = current_attrs.values_at('rgb', 'c', 'm', 'y', 'k')
83
+
84
+ if [c, m, y, k].all?
85
+ append_text("<color c=\"#{c}\" m=\"#{m}\" y=\"#{y}\" k=\"#{k}\">")
86
+ else
87
+ append_text("<color rgb=\"#{rgb}\">")
88
+ end
89
+ end
90
+
91
+ def end_color
92
+ append_text('</color>')
93
+ end
94
+
95
+ def start_font
96
+ font_attrs = current_attrs
97
+ .slice('size', 'name', 'character_spacing')
98
+ .reduce('') { |acc, (key, val)| "#{acc} #{key}=\"#{val}\"" }
99
+
100
+ append_text("<font #{font_attrs}>")
101
+ end
102
+
103
+ def end_font
104
+ append_text('</font>')
105
+ end
81
106
  end
82
107
  end
83
108
  end
@@ -125,7 +125,11 @@ module Prawn
125
125
  def style_properties
126
126
  style = current_attrs['style']
127
127
  if style
128
- style.split(';').map { |p| p.split(':', 2).map(&:strip) }.to_h
128
+ style
129
+ .split(';')
130
+ .map { |p| p.split(':', 2).map(&:strip) }
131
+ .select { |tuple| tuple.size == 2 }
132
+ .to_h
129
133
  else
130
134
  {}
131
135
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Prawn
4
4
  module Markup
5
- VERSION = '0.3.4'
5
+ VERSION = '0.3.7'
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: prawn-markup
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.4
4
+ version: 0.3.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pascal Zumkehr
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-01-10 00:00:00.000000000 Z
11
+ date: 2022-08-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nokogiri
@@ -202,7 +202,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
202
202
  - !ruby/object:Gem::Version
203
203
  version: '0'
204
204
  requirements: []
205
- rubygems_version: 3.1.4
205
+ rubygems_version: 3.3.19
206
206
  signing_key:
207
207
  specification_version: 4
208
208
  summary: Parse simple HTML markup to include in Prawn PDFs