Skip to content

Commit d04ef23

Browse files
authored
Merge pull request #26 from blocknotes/improve-prawn-callbacks
Improve Prawn callbacks
2 parents 6146b5f + f7242db commit d04ef23

10 files changed

Lines changed: 74 additions & 23 deletions

File tree

lib/prawn_html/attributes.rb

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,12 @@ class Attributes < OpenStruct
1010
block: %i[align bottom leading left margin_left padding_left position right top],
1111
tag_close: %i[margin_bottom padding_bottom break_after],
1212
tag_open: %i[margin_top padding_top break_before],
13-
text_node: %i[background callback character_spacing color font link list_style_type size styles white_space]
13+
text_node: %i[callback character_spacing color font link list_style_type size styles white_space]
1414
}.freeze
1515

1616
STYLES_LIST = {
1717
# text node styles
18-
'background' => { key: :background, set: :convert_color },
19-
'callback' => { key: :callback, set: :copy_value },
18+
'background' => { key: :callback, set: :callback_background },
2019
'color' => { key: :color, set: :convert_color },
2120
'font-family' => { key: :font, set: :unquote },
2221
'font-size' => { key: :size, set: :convert_size },
@@ -25,7 +24,7 @@ class Attributes < OpenStruct
2524
'href' => { key: :link, set: :copy_value },
2625
'letter-spacing' => { key: :character_spacing, set: :convert_float },
2726
'list-style-type' => { key: :list_style_type, set: :unquote },
28-
'text-decoration' => { key: :styles, set: :append_styles },
27+
'text-decoration' => { key: :styles, set: :append_text_decoration },
2928
'vertical-align' => { key: :styles, set: :append_styles },
3029
'white-space' => { key: :white_space, set: :convert_symbol },
3130
# tag opening styles
@@ -103,6 +102,24 @@ def parse_styles(styles)
103102

104103
private
105104

105+
def process_styles(hash_styles, options:)
106+
hash_styles.each do |key, value|
107+
rule = evaluate_rule(key, value)
108+
apply_rule!(merged_styles: @styles, rule: rule, value: value, options: options)
109+
end
110+
@styles
111+
end
112+
113+
def evaluate_rule(rule_key, attr_value)
114+
rule = STYLES_LIST[rule_key]
115+
if rule && rule[:set] == :append_text_decoration
116+
return { key: :callback, set: :callback_strike_through } if attr_value == 'line-through'
117+
118+
return { key: :styles, set: :append_styles }
119+
end
120+
rule
121+
end
122+
106123
def apply_rule!(merged_styles:, rule:, value:, options:)
107124
return unless rule
108125

@@ -113,12 +130,5 @@ def apply_rule!(merged_styles:, rule:, value:, options:)
113130
merged_styles[rule[:key]] = Utils.send(rule[:set], value, options: opts)
114131
end
115132
end
116-
117-
def process_styles(hash_styles, options:)
118-
hash_styles.each do |key, value|
119-
apply_rule!(merged_styles: @styles, rule: STYLES_LIST[key], value: value, options: options)
120-
end
121-
@styles
122-
end
123133
end
124134
end
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
module PrawnHtml
44
module Callbacks
5-
class Highlight
5+
class Background
66
DEF_HIGHLIGHT = 'ffff00'
77

8-
def initialize(pdf, item)
8+
def initialize(pdf, color = nil)
99
@pdf = pdf
10-
@color = item.delete(:background) || DEF_HIGHLIGHT
10+
@color = color || DEF_HIGHLIGHT
1111
end
1212

1313
def render_behind(fragment)

lib/prawn_html/document_renderer.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,9 @@ def output_content(buffer, block_styles)
120120

121121
def apply_callbacks(buffer)
122122
buffer.select { |item| item[:callback] }.each do |item|
123-
callback = Tag::CALLBACKS[item[:callback]]
124-
item[:callback] = callback.new(pdf, item)
123+
callback, arg = item[:callback]
124+
callback_class = Tag::CALLBACKS[callback]
125+
item[:callback] = callback_class.new(pdf, arg)
125126
end
126127
end
127128

lib/prawn_html/tag.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
module PrawnHtml
44
class Tag
55
CALLBACKS = {
6-
'Highlight' => Callbacks::Highlight,
6+
'Background' => Callbacks::Background,
77
'StrikeThrough' => Callbacks::StrikeThrough
88
}.freeze
99
TAG_CLASSES = %w[A B Blockquote Body Br Code Del Div H Hr I Img Li Mark Ol P Pre Small Span Sub Sup U Ul].freeze

lib/prawn_html/tags/del.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class Del < Tag
66
ELEMENTS = [:del, :s].freeze
77

88
def tag_styles
9-
'callback: StrikeThrough'
9+
'text-decoration: line-through'
1010
end
1111
end
1212
end

lib/prawn_html/tags/mark.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class Mark < Tag
66
ELEMENTS = [:mark].freeze
77

88
def tag_styles
9-
'callback: Highlight'
9+
'background: #ff0'
1010
end
1111
end
1212
end

lib/prawn_html/utils.rb

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,24 @@ module Utils
1010
'underline' => :underline
1111
}.freeze
1212

13+
# Setup a background callback
14+
#
15+
# @param value [String] HTML string color
16+
#
17+
# @return [Array] callback name and argument value
18+
def callback_background(value, options: nil)
19+
['Background', convert_color(value, options: options)]
20+
end
21+
22+
# Setup a strike through callback
23+
#
24+
# @param value [String] unused
25+
#
26+
# @return [Array] callback name and argument value
27+
def callback_strike_through(value, options: nil)
28+
['StrikeThrough', nil]
29+
end
30+
1331
# Converts a color string
1432
#
1533
# Supported formats:
@@ -103,7 +121,7 @@ def unquote(value, options: nil)
103121
end
104122
end
105123

106-
module_function :convert_color, :convert_float, :convert_size, :convert_symbol, :copy_value, :normalize_style,
107-
:unquote
124+
module_function :callback_background, :callback_strike_through, :convert_color, :convert_float, :convert_size,
125+
:convert_symbol, :copy_value, :normalize_style, :unquote
108126
end
109127
end

spec/units/prawn_html/tags/del_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
end
1414

1515
it 'merges the callback property into styles' do
16-
expect(styles).to match(color: 'ffbb11', callback: 'StrikeThrough')
16+
expect(styles).to match(color: 'ffbb11', callback: ['StrikeThrough', nil])
1717
end
1818
end
1919
end

spec/units/prawn_html/tags/mark_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
end
1414

1515
it 'merges the callback property into styles' do
16-
expect(styles).to match(color: 'ffbb11', callback: 'Highlight')
16+
expect(styles).to match(color: 'ffbb11', callback: ['Background', 'ffff00'])
1717
end
1818
end
1919
end

spec/units/prawn_html/utils_spec.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,28 @@
11
# frozen_string_literal: true
22

33
RSpec.describe PrawnHtml::Utils do
4+
describe '.callback_background' do
5+
subject(:callback_background) { described_class.callback_background(value) }
6+
7+
context 'with a nil value' do
8+
let(:value) { nil }
9+
10+
it { is_expected.to eq ['Background', nil] }
11+
end
12+
13+
context 'with a color value' do
14+
let(:value) { 'red' }
15+
16+
it { is_expected.to eq ['Background', 'ff0000'] }
17+
end
18+
end
19+
20+
describe '.callback_strike_through' do
21+
subject(:callback_strike_through) { described_class.callback_strike_through(nil) }
22+
23+
it { is_expected.to eq ['StrikeThrough', nil] }
24+
end
25+
426
describe '.convert_color' do
527
subject(:convert_color) { described_class.convert_color(value) }
628

0 commit comments

Comments
 (0)