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 1 commit
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 or CMYK format.
codez marked this conversation as resolved.
Show resolved Hide resolved
- `:underline`: Specifies whether the link should be underlined. Default is false..
codez marked this conversation as resolved.
Show resolved Hide resolved

## Development

Expand Down
20 changes: 18 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]
start_color(link_options[:color]) if link_options[:color].any?
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_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: {},
codez marked this conversation as resolved.
Show resolved Hide resolved
underline: false
}
end

def start_b
append_text('<b>')
end
Expand Down Expand Up @@ -78,8 +93,9 @@ def end_sup
append_text('</sup>')
end

def start_color
rgb, c, m, y, k = current_attrs.values_at('rgb', 'c', 'm', 'y', 'k')
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')
codez marked this conversation as resolved.
Show resolved Hide resolved

if [c, m, y, k].all?
append_text("<color c=\"#{c}\" m=\"#{m}\" y=\"#{y}\" k=\"#{k}\">")
Expand Down
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: { rgb: "0000FF" }, underline: true }
codez marked this conversation as resolved.
Show resolved Hide resolved
}
doc.markup(html)
# lookatit
Expand Down