Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 21 additions & 11 deletions lib/prawn_html/attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand All @@ -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
Expand Down Expand Up @@ -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

Expand All @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions lib/prawn_html/document_renderer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion lib/prawn_html/tag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/prawn_html/tags/del.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Del < Tag
ELEMENTS = [:del, :s].freeze

def tag_styles
'callback: StrikeThrough'
'text-decoration: line-through'
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/prawn_html/tags/mark.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Mark < Tag
ELEMENTS = [:mark].freeze

def tag_styles
'callback: Highlight'
'background: #ff0'
end
end
end
Expand Down
22 changes: 20 additions & 2 deletions lib/prawn_html/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
2 changes: 1 addition & 1 deletion spec/units/prawn_html/tags/del_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion spec/units/prawn_html/tags/mark_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
22 changes: 22 additions & 0 deletions spec/units/prawn_html/utils_spec.rb
Original file line number Diff line number Diff line change
@@ -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) }

Expand Down