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.
@@ -6,9 +6,7 @@ module PrawnHtml
6
6
  ELEMENTS = [:b, :strong].freeze
7
7
 
8
8
  def tag_styles
9
- {
10
- 'font-weight' => 'bold'
11
- }
9
+ 'font-weight: bold'
12
10
  end
13
11
  end
14
12
  end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PrawnHtml
4
+ module Tags
5
+ class Blockquote < Tag
6
+ ELEMENTS = [:blockquote].freeze
7
+
8
+ MARGIN_BOTTOM = 10
9
+ MARGIN_LEFT = 25
10
+ MARGIN_TOP = 10
11
+
12
+ def block?
13
+ true
14
+ end
15
+
16
+ def tag_styles
17
+ <<~STYLES
18
+ margin-bottom: #{MARGIN_BOTTOM}px;
19
+ margin-left: #{MARGIN_LEFT}px;
20
+ margin-top: #{MARGIN_TOP}px;
21
+ STYLES
22
+ end
23
+ end
24
+ end
25
+ end
@@ -5,7 +5,7 @@ module PrawnHtml
5
5
  class Br < Tag
6
6
  ELEMENTS = [:br].freeze
7
7
 
8
- BR_SPACING = 12
8
+ BR_SPACING = Utils.convert_size('12')
9
9
 
10
10
  def block?
11
11
  true
@@ -14,8 +14,7 @@ module PrawnHtml
14
14
  def custom_render(pdf, context)
15
15
  return if context.last_text_node
16
16
 
17
- @spacing ||= Attributes.convert_size(BR_SPACING.to_s)
18
- pdf.move_down(@spacing)
17
+ pdf.advance_cursor(BR_SPACING)
19
18
  end
20
19
  end
21
20
  end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PrawnHtml
4
+ module Tags
5
+ class Code < Tag
6
+ ELEMENTS = [:code].freeze
7
+
8
+ def tag_styles
9
+ 'font-family: Courier'
10
+ end
11
+ end
12
+ end
13
+ end
@@ -6,9 +6,7 @@ module PrawnHtml
6
6
  ELEMENTS = [:del, :s].freeze
7
7
 
8
8
  def tag_styles
9
- {
10
- 'callback' => Callbacks::StrikeThrough
11
- }
9
+ 'callback: StrikeThrough'
12
10
  end
13
11
  end
14
12
  end
@@ -37,12 +37,12 @@ module PrawnHtml
37
37
  end
38
38
 
39
39
  def tag_styles
40
- @tag_styles ||= {
41
- 'font-size' => SIZES[tag].to_s,
42
- 'font-weight' => 'bold',
43
- 'margin-bottom' => MARGINS_BOTTOM[tag].to_s,
44
- 'margin-top' => MARGINS_TOP[tag].to_s
45
- }
40
+ <<~STYLES
41
+ font-size: #{SIZES[tag]}px;
42
+ font-weight: bold;
43
+ margin-bottom: #{MARGINS_BOTTOM[tag]}px;
44
+ margin-top: #{MARGINS_TOP[tag]}px;
45
+ STYLES
46
46
  end
47
47
  end
48
48
  end
@@ -13,17 +13,15 @@ module PrawnHtml
13
13
  end
14
14
 
15
15
  def custom_render(pdf, _context)
16
- dash = parse_dash_value(attrs.data['dash']) if attrs.data.include?('dash')
17
- pdf.dash(dash) if dash
18
- pdf.stroke_horizontal_rule
19
- pdf.undash if dash
16
+ dash = attrs.data.include?('dash') ? parse_dash_value(attrs.data['dash']) : nil
17
+ pdf.horizontal_rule(color: attrs.styles[:color], dash: dash)
20
18
  end
21
19
 
22
20
  def tag_styles
23
- @tag_styles ||= {
24
- 'margin-bottom' => MARGIN_BOTTOM.to_s,
25
- 'margin-top' => MARGIN_TOP.to_s,
26
- }
21
+ <<~STYLES
22
+ margin-bottom: #{MARGIN_BOTTOM}px;
23
+ margin-top: #{MARGIN_TOP}px;
24
+ STYLES
27
25
  end
28
26
 
29
27
  private
@@ -6,9 +6,7 @@ module PrawnHtml
6
6
  ELEMENTS = [:i, :em].freeze
7
7
 
8
8
  def tag_styles
9
- {
10
- 'font-style' => 'italic'
11
- }
9
+ 'font-style: italic'
12
10
  end
13
11
  end
14
12
  end
@@ -10,19 +10,19 @@ module PrawnHtml
10
10
  end
11
11
 
12
12
  def custom_render(pdf, context)
13
- styles = Attributes.parse_styles(attrs.style)
13
+ parsed_styles = Attributes.parse_styles(attrs.style)
14
14
  block_styles = context.block_styles
15
- evaluated_styles = evaluate_styles(pdf, block_styles.merge(styles))
16
- pdf.image(@attrs.src, evaluated_styles) if @attrs.src
15
+ evaluated_styles = evaluate_styles(pdf, block_styles.merge(parsed_styles))
16
+ pdf.image(@attrs.src, evaluated_styles)
17
17
  end
18
18
 
19
19
  private
20
20
 
21
- def evaluate_styles(pdf, styles)
21
+ def evaluate_styles(pdf, img_styles)
22
22
  {}.tap do |result|
23
- result[:width] = Attributes.convert_size(styles['width'], pdf.bounds.width) if styles.include?('width')
24
- result[:height] = Attributes.convert_size(styles['height'], pdf.bounds.height) if styles.include?('height')
25
- result[:position] = styles[:align] if %i[left center right].include?(styles[:align])
23
+ result[:width] = Utils.convert_size(img_styles['width'], pdf.bounds.width) if img_styles.include?('width')
24
+ result[:height] = Utils.convert_size(img_styles['height'], pdf.bounds.height) if img_styles.include?('height')
25
+ result[:position] = img_styles[:align] if %i[left center right].include?(img_styles[:align])
26
26
  end
27
27
  end
28
28
  end
@@ -9,10 +9,13 @@ module PrawnHtml
9
9
  true
10
10
  end
11
11
 
12
- def tag_styles
13
- {
14
- before_content: '&bullet; '
15
- }
12
+ def before_content
13
+ @counter ? "#{@counter}. " : "#{@symbol} "
14
+ end
15
+
16
+ def on_context_add(_context)
17
+ @counter = (parent.counter += 1) if parent.is_a? Ol
18
+ @symbol = parent.styles[:list_style_type] || '&bullet;' if parent.is_a? Ul
16
19
  end
17
20
  end
18
21
  end
@@ -6,9 +6,7 @@ module PrawnHtml
6
6
  ELEMENTS = [:mark].freeze
7
7
 
8
8
  def tag_styles
9
- {
10
- 'callback' => Callbacks::Highlight
11
- }
9
+ 'callback: Highlight'
12
10
  end
13
11
  end
14
12
  end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PrawnHtml
4
+ module Tags
5
+ class Ol < Tag
6
+ ELEMENTS = [:ol].freeze
7
+
8
+ MARGIN_LEFT = 25
9
+
10
+ attr_accessor :counter
11
+
12
+ def initialize(tag, attributes: {}, element_styles: '')
13
+ super
14
+ @counter = 0
15
+ end
16
+
17
+ def block?
18
+ true
19
+ end
20
+
21
+ def tag_styles
22
+ "margin-left: #{MARGIN_LEFT}px"
23
+ end
24
+ end
25
+ end
26
+ end
@@ -13,10 +13,10 @@ module PrawnHtml
13
13
  end
14
14
 
15
15
  def tag_styles
16
- @tag_styles ||= {
17
- 'margin-bottom' => MARGIN_BOTTOM.to_s,
18
- 'margin-top' => MARGIN_TOP.to_s
19
- }
16
+ <<~STYLES
17
+ margin-bottom: #{MARGIN_BOTTOM}px;
18
+ margin-top: #{MARGIN_TOP}px;
19
+ STYLES
20
20
  end
21
21
  end
22
22
  end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PrawnHtml
4
+ module Tags
5
+ class Pre < Tag
6
+ ELEMENTS = [:pre].freeze
7
+
8
+ MARGIN_BOTTOM = 14
9
+ MARGIN_TOP = 14
10
+
11
+ def block?
12
+ true
13
+ end
14
+
15
+ def tag_styles
16
+ <<~STYLES
17
+ font-family: Courier;
18
+ margin-bottom: #{MARGIN_BOTTOM}px;
19
+ margin-top: #{MARGIN_TOP}px;
20
+ white-space: pre;
21
+ STYLES
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PrawnHtml
4
+ module Tags
5
+ class Sub < Tag
6
+ ELEMENTS = [:sub].freeze
7
+
8
+ def tag_styles
9
+ 'vertical-align: sub'
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PrawnHtml
4
+ module Tags
5
+ class Sup < Tag
6
+ ELEMENTS = [:sup].freeze
7
+
8
+ def tag_styles
9
+ 'vertical-align: super'
10
+ end
11
+ end
12
+ end
13
+ end
@@ -6,9 +6,7 @@ module PrawnHtml
6
6
  ELEMENTS = [:ins, :u].freeze
7
7
 
8
8
  def tag_styles
9
- {
10
- 'text-decoration' => 'underline'
11
- }
9
+ 'text-decoration: underline'
12
10
  end
13
11
  end
14
12
  end
@@ -12,9 +12,7 @@ module PrawnHtml
12
12
  end
13
13
 
14
14
  def tag_styles
15
- @tag_styles ||= {
16
- 'margin-left' => MARGIN_LEFT.to_s,
17
- }
15
+ "margin-left: #{MARGIN_LEFT}px"
18
16
  end
19
17
  end
20
18
  end
@@ -0,0 +1,109 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PrawnHtml
4
+ module Utils
5
+ NORMALIZE_STYLES = {
6
+ 'bold' => :bold,
7
+ 'italic' => :italic,
8
+ 'sub' => :subscript,
9
+ 'super' => :superscript,
10
+ 'underline' => :underline
11
+ }.freeze
12
+
13
+ # Converts a color string
14
+ #
15
+ # Supported formats:
16
+ # - 3 hex digits, ex. `color: #FB1`;
17
+ # - 6 hex digits, ex. `color: #abcdef`;
18
+ # - RGB, ex. `color: RGB(64, 0, 128)`;
19
+ # - color name, ex. `color: red`.
20
+ #
21
+ # @param value [String] HTML string color
22
+ #
23
+ # @return [String] adjusted string color or nil if value is invalid
24
+ def convert_color(value)
25
+ val = value.to_s.strip.downcase
26
+ return Regexp.last_match[1] if val.match /\A#([a-f0-9]{6})\Z/ # rubocop:disable Performance/RedundantMatch
27
+
28
+ if val.match /\A#([a-f0-9]{3})\Z/ # rubocop:disable Performance/RedundantMatch
29
+ r, g, b = Regexp.last_match[1].chars
30
+ return r * 2 + g * 2 + b * 2
31
+ end
32
+ if val.match /\Argb\s*\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\)\Z/ # rubocop:disable Performance/RedundantMatch
33
+ r, g, b = Regexp.last_match[1..3].map { |v| v.to_i.to_s(16) }
34
+ return "#{r.rjust(2, '0')}#{g.rjust(2, '0')}#{b.rjust(2, '0')}"
35
+ end
36
+
37
+ COLORS[val]
38
+ end
39
+
40
+ # Converts a decimal number string
41
+ #
42
+ # @param value [String] string decimal
43
+ #
44
+ # @return [Float] converted and rounded float number
45
+ def convert_float(value)
46
+ val = value&.gsub(/[^0-9.]/, '') || ''
47
+ val.to_f.round(4)
48
+ end
49
+
50
+ # Converts a size string
51
+ #
52
+ # @param value [String] size string
53
+ # @param container_size [Numeric] container size
54
+ #
55
+ # @return [Float] converted and rounded size
56
+ def convert_size(value, container_size = nil)
57
+ val = value&.gsub(/[^0-9.]/, '') || ''
58
+ val =
59
+ if container_size && value.include?('%')
60
+ val.to_f * container_size * 0.01
61
+ else
62
+ val.to_f * PrawnHtml::PX
63
+ end
64
+ val.round(4)
65
+ end
66
+
67
+ # Converts a string to symbol
68
+ #
69
+ # @param value [String] string
70
+ #
71
+ # @return [Symbol] symbol
72
+ def convert_symbol(value)
73
+ value.to_sym if value && !value.match?(/\A\s*\Z/)
74
+ end
75
+
76
+ # Copy a value without conversion
77
+ #
78
+ # @param value
79
+ #
80
+ # @return value
81
+ def copy_value(value)
82
+ value
83
+ end
84
+
85
+ # Normalize a style value
86
+ #
87
+ # @param value [String] string value
88
+ #
89
+ # @return [Symbol] style value or nil
90
+ def normalize_style(value)
91
+ val = value&.strip&.downcase
92
+ NORMALIZE_STYLES[val]
93
+ end
94
+
95
+ # Unquotes a string
96
+ #
97
+ # @param value [String] string
98
+ #
99
+ # @return [String] string without quotes at the beginning/ending
100
+ def unquote(value)
101
+ (value&.strip || +'').tap do |val|
102
+ val.gsub!(/\A['"]|["']\Z/, '')
103
+ end
104
+ end
105
+
106
+ module_function :convert_color, :convert_float, :convert_size, :convert_symbol, :copy_value, :normalize_style,
107
+ :unquote
108
+ end
109
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module PrawnHtml # :nodoc:
4
- VERSION = '0.2.0'
4
+ VERSION = '0.4.2'
5
5
  end