From 1279dd4a636e534a1752ca0b60de89b1c6f31ae9 Mon Sep 17 00:00:00 2001 From: the-code-bot Date: Fri, 1 Nov 2024 08:45:31 +0530 Subject: [PATCH] refactored based on review comments --- README.md | 4 ++-- lib/prawn/markup/processor/text.rb | 26 ++++++++++++++++-------- spec/prawn/markup/processor/text_spec.rb | 19 ++++++++++++++++- spec/prawn/markup/showcase_spec.rb | 2 +- 4 files changed, 38 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 6626b21..2289e4e 100644 --- a/README.md +++ b/README.md @@ -110,8 +110,8 @@ Beside these options handled by Prawn / prawn-table, the following values may be - `:checked`: The char to print for a checked radio. Default is '◉'. - `:unchecked`: The char to print for an unchecked radio. Default is '○'. - `:link` - - `:color`: The link color, which can be specified in either RGB or CMYK format. - - `:underline`: Specifies whether the link should be underlined. Default is false.. + - `:color`: The link color, which can be specified in either RGB hex format or 4-value CMYK. + - `:underline`: Specifies whether the link should be underlined. Default is false. ## Development diff --git a/lib/prawn/markup/processor/text.rb b/lib/prawn/markup/processor/text.rb index 7494cf7..894ba3c 100644 --- a/lib/prawn/markup/processor/text.rb +++ b/lib/prawn/markup/processor/text.rb @@ -11,15 +11,15 @@ def self.prepended(base) end def start_a - start_u if link_options[:underline] - start_color(link_options[:color]) if link_options[:color].any? + start_u if link_options[:underline] + append_color_tag(link_options[:color]) if link_options[:color] append_text("") end alias start_link start_a def end_a append_text('') - end_color if link_options[:color].any? + end_color if link_options[:color] end_u if link_options[:underline] end alias end_link end_a @@ -30,7 +30,7 @@ def link_options def default_link_options { - color: {}, + color: nil, underline: false } end @@ -93,14 +93,13 @@ def end_sup append_text('') end - def start_color(options = {}) - options = current_attrs unless options.any? - rgb, c, m, y, k = options.transform_keys(&:to_s).values_at('rgb', 'c', 'm', 'y', 'k') + def start_color + rgb, c, m, y, k = current_attrs.values_at('rgb', 'c', 'm', 'y', 'k') if [c, m, y, k].all? - append_text("") + append_color_tag([c, m, y, k]) else - append_text("") + append_color_tag(rgb) end end @@ -119,6 +118,15 @@ def start_font def end_font append_text('') end + + def append_color_tag(color) + if color.is_a?(Array) + c, m, y, k = color + append_text("") + else + append_text("") + end + end end end end diff --git a/spec/prawn/markup/processor/text_spec.rb b/spec/prawn/markup/processor/text_spec.rb index 7b702b2..06b7b79 100644 --- a/spec/prawn/markup/processor/text_spec.rb +++ b/spec/prawn/markup/processor/text_spec.rb @@ -28,7 +28,7 @@ it 'handles prawn color tag for cmyk' do processor.parse('hello world') expect(text.strings).to eq(['hello ', 'world']) - end + end it 'handles prawn font name' do processor.parse('hello world') @@ -49,4 +49,21 @@ processor.parse('hello world') expect(text.strings).to eq(['hello ', 'world']) end + + context 'with_options' do + let(:options) do + { + link: { + color: "AAAAAA", + underline: true, + } + } + end + + it 'creates links with provided options' do + processor.parse('hello world') + expect(text.strings).to eq(['hello ', 'world']) + expect(top_positions).to eq([top, top].map(&:round)) + end + end end diff --git a/spec/prawn/markup/showcase_spec.rb b/spec/prawn/markup/showcase_spec.rb index 00e4bda..eb1d76c 100644 --- a/spec/prawn/markup/showcase_spec.rb +++ b/spec/prawn/markup/showcase_spec.rb @@ -20,7 +20,7 @@ placeholder: ->(src) { "Embedded content: #{src}" } }, input: { symbol_font: 'DejaVu', symbol_font_size: 16 }, - link: { color: { rgb: "0000FF" }, underline: true } + link: { color: "AA0000", underline: true } } doc.markup(html) # lookatit