Skip to content

Commit 1279dd4

Browse files
ro-guncodez
authored andcommitted
refactored based on review comments
1 parent bb052c8 commit 1279dd4

File tree

4 files changed

+38
-13
lines changed

4 files changed

+38
-13
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,8 @@ Beside these options handled by Prawn / prawn-table, the following values may be
110110
- `:checked`: The char to print for a checked radio. Default is '◉'.
111111
- `:unchecked`: The char to print for an unchecked radio. Default is '○'.
112112
- `:link`
113-
- `:color`: The link color, which can be specified in either RGB or CMYK format.
114-
- `:underline`: Specifies whether the link should be underlined. Default is false..
113+
- `:color`: The link color, which can be specified in either RGB hex format or 4-value CMYK.
114+
- `:underline`: Specifies whether the link should be underlined. Default is false.
115115

116116
## Development
117117

lib/prawn/markup/processor/text.rb

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ def self.prepended(base)
1111
end
1212

1313
def start_a
14-
start_u if link_options[:underline]
15-
start_color(link_options[:color]) if link_options[:color].any?
14+
start_u if link_options[:underline]
15+
append_color_tag(link_options[:color]) if link_options[:color]
1616
append_text("<link href=\"#{current_attrs['href']}\">")
1717
end
1818
alias start_link start_a
1919

2020
def end_a
2121
append_text('</link>')
22-
end_color if link_options[:color].any?
22+
end_color if link_options[:color]
2323
end_u if link_options[:underline]
2424
end
2525
alias end_link end_a
@@ -30,7 +30,7 @@ def link_options
3030

3131
def default_link_options
3232
{
33-
color: {},
33+
color: nil,
3434
underline: false
3535
}
3636
end
@@ -93,14 +93,13 @@ def end_sup
9393
append_text('</sup>')
9494
end
9595

96-
def start_color(options = {})
97-
options = current_attrs unless options.any?
98-
rgb, c, m, y, k = options.transform_keys(&:to_s).values_at('rgb', 'c', 'm', 'y', 'k')
96+
def start_color
97+
rgb, c, m, y, k = current_attrs.values_at('rgb', 'c', 'm', 'y', 'k')
9998

10099
if [c, m, y, k].all?
101-
append_text("<color c=\"#{c}\" m=\"#{m}\" y=\"#{y}\" k=\"#{k}\">")
100+
append_color_tag([c, m, y, k])
102101
else
103-
append_text("<color rgb=\"#{rgb}\">")
102+
append_color_tag(rgb)
104103
end
105104
end
106105

@@ -119,6 +118,15 @@ def start_font
119118
def end_font
120119
append_text('</font>')
121120
end
121+
122+
def append_color_tag(color)
123+
if color.is_a?(Array)
124+
c, m, y, k = color
125+
append_text("<color c=\"#{c}\" m=\"#{m}\" y=\"#{y}\" k=\"#{k}\">")
126+
else
127+
append_text("<color rgb=\"#{color}\">")
128+
end
129+
end
122130
end
123131
end
124132
end

spec/prawn/markup/processor/text_spec.rb

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
it 'handles prawn color tag for cmyk' do
2929
processor.parse('hello <color c="22" m="55" y="79" k="30">world</color>')
3030
expect(text.strings).to eq(['hello ', 'world'])
31-
end
31+
end
3232

3333
it 'handles prawn font name' do
3434
processor.parse('hello <font name="Courier">world</font>')
@@ -49,4 +49,21 @@
4949
processor.parse('hello <font name="Courier" size="20">world</font>')
5050
expect(text.strings).to eq(['hello ', 'world'])
5151
end
52+
53+
context 'with_options' do
54+
let(:options) do
55+
{
56+
link: {
57+
color: "AAAAAA",
58+
underline: true,
59+
}
60+
}
61+
end
62+
63+
it 'creates links with provided options' do
64+
processor.parse('hello <a href="http://example.com">world</a>')
65+
expect(text.strings).to eq(['hello ', 'world'])
66+
expect(top_positions).to eq([top, top].map(&:round))
67+
end
68+
end
5269
end

spec/prawn/markup/showcase_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
placeholder: ->(src) { "Embedded content: #{src}" }
2121
},
2222
input: { symbol_font: 'DejaVu', symbol_font_size: 16 },
23-
link: { color: { rgb: "0000FF" }, underline: true }
23+
link: { color: "AA0000", underline: true }
2424
}
2525
doc.markup(html)
2626
# lookatit

0 commit comments

Comments
 (0)