diff --git a/lib/prawn_html/attributes.rb b/lib/prawn_html/attributes.rb
index 4baedc2..fc4a349 100644
--- a/lib/prawn_html/attributes.rb
+++ b/lib/prawn_html/attributes.rb
@@ -10,13 +10,12 @@ class Attributes < OpenStruct
block: %i[align bottom leading left margin_left padding_left position right top],
tag_close: %i[margin_bottom padding_bottom break_after],
tag_open: %i[margin_top padding_top break_before],
- text_node: %i[background callback character_spacing color font link list_style_type size styles white_space]
+ text_node: %i[callback character_spacing color font link list_style_type size styles white_space]
}.freeze
STYLES_LIST = {
# text node styles
- 'background' => { key: :background, set: :convert_color },
- 'callback' => { key: :callback, set: :copy_value },
+ 'background' => { key: :callback, set: :callback_background },
'color' => { key: :color, set: :convert_color },
'font-family' => { key: :font, set: :unquote },
'font-size' => { key: :size, set: :convert_size },
@@ -25,7 +24,7 @@ class Attributes < OpenStruct
'href' => { key: :link, set: :copy_value },
'letter-spacing' => { key: :character_spacing, set: :convert_float },
'list-style-type' => { key: :list_style_type, set: :unquote },
- 'text-decoration' => { key: :styles, set: :append_styles },
+ 'text-decoration' => { key: :styles, set: :append_text_decoration },
'vertical-align' => { key: :styles, set: :append_styles },
'white-space' => { key: :white_space, set: :convert_symbol },
# tag opening styles
@@ -103,6 +102,24 @@ def parse_styles(styles)
private
+ def process_styles(hash_styles, options:)
+ hash_styles.each do |key, value|
+ rule = evaluate_rule(key, value)
+ apply_rule!(merged_styles: @styles, rule: rule, value: value, options: options)
+ end
+ @styles
+ end
+
+ def evaluate_rule(rule_key, attr_value)
+ rule = STYLES_LIST[rule_key]
+ if rule && rule[:set] == :append_text_decoration
+ return { key: :callback, set: :callback_strike_through } if attr_value == 'line-through'
+
+ return { key: :styles, set: :append_styles }
+ end
+ rule
+ end
+
def apply_rule!(merged_styles:, rule:, value:, options:)
return unless rule
@@ -113,12 +130,5 @@ def apply_rule!(merged_styles:, rule:, value:, options:)
merged_styles[rule[:key]] = Utils.send(rule[:set], value, options: opts)
end
end
-
- def process_styles(hash_styles, options:)
- hash_styles.each do |key, value|
- apply_rule!(merged_styles: @styles, rule: STYLES_LIST[key], value: value, options: options)
- end
- @styles
- end
end
end
diff --git a/lib/prawn_html/callbacks/highlight.rb b/lib/prawn_html/callbacks/background.rb
similarity index 75%
rename from lib/prawn_html/callbacks/highlight.rb
rename to lib/prawn_html/callbacks/background.rb
index a4dce8f..4038b59 100644
--- a/lib/prawn_html/callbacks/highlight.rb
+++ b/lib/prawn_html/callbacks/background.rb
@@ -2,12 +2,12 @@
module PrawnHtml
module Callbacks
- class Highlight
+ class Background
DEF_HIGHLIGHT = 'ffff00'
- def initialize(pdf, item)
+ def initialize(pdf, color = nil)
@pdf = pdf
- @color = item.delete(:background) || DEF_HIGHLIGHT
+ @color = color || DEF_HIGHLIGHT
end
def render_behind(fragment)
diff --git a/lib/prawn_html/document_renderer.rb b/lib/prawn_html/document_renderer.rb
index 4b62181..e3fed22 100644
--- a/lib/prawn_html/document_renderer.rb
+++ b/lib/prawn_html/document_renderer.rb
@@ -120,8 +120,9 @@ def output_content(buffer, block_styles)
def apply_callbacks(buffer)
buffer.select { |item| item[:callback] }.each do |item|
- callback = Tag::CALLBACKS[item[:callback]]
- item[:callback] = callback.new(pdf, item)
+ callback, arg = item[:callback]
+ callback_class = Tag::CALLBACKS[callback]
+ item[:callback] = callback_class.new(pdf, arg)
end
end
diff --git a/lib/prawn_html/tag.rb b/lib/prawn_html/tag.rb
index c3ef522..d098700 100644
--- a/lib/prawn_html/tag.rb
+++ b/lib/prawn_html/tag.rb
@@ -3,7 +3,7 @@
module PrawnHtml
class Tag
CALLBACKS = {
- 'Highlight' => Callbacks::Highlight,
+ 'Background' => Callbacks::Background,
'StrikeThrough' => Callbacks::StrikeThrough
}.freeze
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
diff --git a/lib/prawn_html/tags/del.rb b/lib/prawn_html/tags/del.rb
index c10adac..33aacca 100644
--- a/lib/prawn_html/tags/del.rb
+++ b/lib/prawn_html/tags/del.rb
@@ -6,7 +6,7 @@ class Del < Tag
ELEMENTS = [:del, :s].freeze
def tag_styles
- 'callback: StrikeThrough'
+ 'text-decoration: line-through'
end
end
end
diff --git a/lib/prawn_html/tags/mark.rb b/lib/prawn_html/tags/mark.rb
index 659dc99..c80e884 100644
--- a/lib/prawn_html/tags/mark.rb
+++ b/lib/prawn_html/tags/mark.rb
@@ -6,7 +6,7 @@ class Mark < Tag
ELEMENTS = [:mark].freeze
def tag_styles
- 'callback: Highlight'
+ 'background: #ff0'
end
end
end
diff --git a/lib/prawn_html/utils.rb b/lib/prawn_html/utils.rb
index 602fa35..d4c49f5 100644
--- a/lib/prawn_html/utils.rb
+++ b/lib/prawn_html/utils.rb
@@ -10,6 +10,24 @@ module Utils
'underline' => :underline
}.freeze
+ # Setup a background callback
+ #
+ # @param value [String] HTML string color
+ #
+ # @return [Array] callback name and argument value
+ def callback_background(value, options: nil)
+ ['Background', convert_color(value, options: options)]
+ end
+
+ # Setup a strike through callback
+ #
+ # @param value [String] unused
+ #
+ # @return [Array] callback name and argument value
+ def callback_strike_through(value, options: nil)
+ ['StrikeThrough', nil]
+ end
+
# Converts a color string
#
# Supported formats:
@@ -103,7 +121,7 @@ def unquote(value, options: nil)
end
end
- module_function :convert_color, :convert_float, :convert_size, :convert_symbol, :copy_value, :normalize_style,
- :unquote
+ module_function :callback_background, :callback_strike_through, :convert_color, :convert_float, :convert_size,
+ :convert_symbol, :copy_value, :normalize_style, :unquote
end
end
diff --git a/spec/units/prawn_html/tags/del_spec.rb b/spec/units/prawn_html/tags/del_spec.rb
index 0d5cd93..134d2e7 100644
--- a/spec/units/prawn_html/tags/del_spec.rb
+++ b/spec/units/prawn_html/tags/del_spec.rb
@@ -13,7 +13,7 @@
end
it 'merges the callback property into styles' do
- expect(styles).to match(color: 'ffbb11', callback: 'StrikeThrough')
+ expect(styles).to match(color: 'ffbb11', callback: ['StrikeThrough', nil])
end
end
end
diff --git a/spec/units/prawn_html/tags/mark_spec.rb b/spec/units/prawn_html/tags/mark_spec.rb
index 0957719..078ab06 100644
--- a/spec/units/prawn_html/tags/mark_spec.rb
+++ b/spec/units/prawn_html/tags/mark_spec.rb
@@ -13,7 +13,7 @@
end
it 'merges the callback property into styles' do
- expect(styles).to match(color: 'ffbb11', callback: 'Highlight')
+ expect(styles).to match(color: 'ffbb11', callback: ['Background', 'ffff00'])
end
end
end
diff --git a/spec/units/prawn_html/utils_spec.rb b/spec/units/prawn_html/utils_spec.rb
index 49f07e5..c56f12a 100644
--- a/spec/units/prawn_html/utils_spec.rb
+++ b/spec/units/prawn_html/utils_spec.rb
@@ -1,6 +1,28 @@
# frozen_string_literal: true
RSpec.describe PrawnHtml::Utils do
+ describe '.callback_background' do
+ subject(:callback_background) { described_class.callback_background(value) }
+
+ context 'with a nil value' do
+ let(:value) { nil }
+
+ it { is_expected.to eq ['Background', nil] }
+ end
+
+ context 'with a color value' do
+ let(:value) { 'red' }
+
+ it { is_expected.to eq ['Background', 'ff0000'] }
+ end
+ end
+
+ describe '.callback_strike_through' do
+ subject(:callback_strike_through) { described_class.callback_strike_through(nil) }
+
+ it { is_expected.to eq ['StrikeThrough', nil] }
+ end
+
describe '.convert_color' do
subject(:convert_color) { described_class.convert_color(value) }