Skip to content

Commit

Permalink
refactored based on review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
rogancodes authored and codez committed Nov 1, 2024
1 parent bb052c8 commit 1279dd4
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 13 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
26 changes: 17 additions & 9 deletions lib/prawn/markup/processor/text.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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("<link href=\"#{current_attrs['href']}\">")
end
alias start_link start_a

def end_a
append_text('</link>')
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
Expand All @@ -30,7 +30,7 @@ def link_options

def default_link_options
{
color: {},
color: nil,
underline: false
}
end
Expand Down Expand Up @@ -93,14 +93,13 @@ def end_sup
append_text('</sup>')
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("<color c=\"#{c}\" m=\"#{m}\" y=\"#{y}\" k=\"#{k}\">")
append_color_tag([c, m, y, k])
else
append_text("<color rgb=\"#{rgb}\">")
append_color_tag(rgb)
end
end

Expand All @@ -119,6 +118,15 @@ def start_font
def end_font
append_text('</font>')
end

def append_color_tag(color)
if color.is_a?(Array)
c, m, y, k = color
append_text("<color c=\"#{c}\" m=\"#{m}\" y=\"#{y}\" k=\"#{k}\">")
else
append_text("<color rgb=\"#{color}\">")
end
end
end
end
end
19 changes: 18 additions & 1 deletion spec/prawn/markup/processor/text_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
it 'handles prawn color tag for cmyk' do
processor.parse('hello <color c="22" m="55" y="79" k="30">world</color>')
expect(text.strings).to eq(['hello ', 'world'])
end
end

it 'handles prawn font name' do
processor.parse('hello <font name="Courier">world</font>')
Expand All @@ -49,4 +49,21 @@
processor.parse('hello <font name="Courier" size="20">world</font>')
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 <a href="http://example.com">world</a>')
expect(text.strings).to eq(['hello ', 'world'])
expect(top_positions).to eq([top, top].map(&:round))
end
end
end
2 changes: 1 addition & 1 deletion spec/prawn/markup/showcase_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 1279dd4

Please sign in to comment.