Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Styling options for links #53

Merged
merged 2 commits into from
Nov 1, 2024
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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ Beside these options handled by Prawn / prawn-table, the following values may be
- `:radio`
- `: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 hex format or 4-value CMYK.
- `:underline`: Specifies whether the link should be underlined. Default is false.

## Development

Expand Down
28 changes: 26 additions & 2 deletions lib/prawn/markup/processor/text.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,30 @@ def self.prepended(base)
end

def start_a
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]
end_u if link_options[:underline]
end
alias end_link end_a

def link_options
@link_options ||= HashMerger.deep(default_link_options, options[:link] || {})
end

def default_link_options
{
color: nil,
underline: false
}
end

def start_b
append_text('<b>')
end
Expand Down Expand Up @@ -82,9 +97,9 @@ 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 @@ -103,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
3 changes: 2 additions & 1 deletion spec/prawn/markup/showcase_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
iframe: {
placeholder: ->(src) { "Embedded content: #{src}" }
},
input: { symbol_font: 'DejaVu', symbol_font_size: 16 }
input: { symbol_font: 'DejaVu', symbol_font_size: 16 },
link: { color: "AA0000", underline: true }
}
doc.markup(html)
# lookatit
Expand Down