Skip to content

Commit

Permalink
resolves asciidoctor#451 allow button to be styled by theme
Browse files Browse the repository at this point in the history
  • Loading branch information
mojavelinux committed Jul 8, 2019
1 parent 9e6b3d7 commit ef54f48
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 39 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ For a detailed view of what has changed, refer to the {uri-repo}/commits/master[
* replace hyphens with underscores in top-level theme keys
* allow hyphens to be used in variable references in theme (#1122)
* allow theme to control background and border of inline code (literal) (#306)
* allow theme to control background and border of inline button (#451)
* resolve null color value in theme to nil (aka not set)
* add support for cgi-style options on source language when syntax highlighting with Rouge (#1102)
* apply custom theme to chronicles example to customize running content and demonstrate how to extend default theme
Expand Down
3 changes: 3 additions & 0 deletions data/themes/base-theme.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ base_line_height: 1.15
base_line_height_length: 13.8
base_border_color: 'EEEEEE'
base_border_width: 0.5
button_content: '[%s]'
button_font_family: Courier
button_font_style: bold
link_font_color: '0000EE'
literal_font_family: Courier
heading_font_style: bold
Expand Down
4 changes: 4 additions & 0 deletions data/themes/default-theme.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ vertical_rhythm: $base_line_height_length
horizontal_rhythm: $base_line_height_length
# QUESTION should vertical_spacing be block_spacing instead?
vertical_spacing: $vertical_rhythm
button:
content: "[\u2009%s\u2009]"
font_style: bold
font_color: b12146
link:
font_color: 428bca
# literal is currently used for inline monospaced in prose and table cells
Expand Down
3 changes: 1 addition & 2 deletions lib/asciidoctor-pdf/converter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2202,8 +2202,7 @@ def convert_inline_break node
end

def convert_inline_button node
%(<strong>[#{NarrowNoBreakSpace}#{node.text}#{NarrowNoBreakSpace}]</strong>)
#%(<strong>[#{NoBreakSpace}#{node.text}#{NoBreakSpace}]</strong>)
%(<button>#{(@theme.button_content || '%s').sub '%s', node.text}</button>)
end

def convert_inline_callout node
Expand Down
36 changes: 24 additions & 12 deletions lib/asciidoctor-pdf/formatted_text/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -356,44 +356,44 @@ def _nt_tag_name
r1 = SyntaxNode.new(input, (index-1)...index) if r1 == true
r0 = r1
else
if (match_len = has_terminal?('code', false, index))
if (match_len = has_terminal?('strong', false, index))
r2 = instantiate_node(SyntaxNode,input, index...(index + match_len))
@index += match_len
else
terminal_parse_failure('code')
terminal_parse_failure('strong')
r2 = nil
end
if r2
r2 = SyntaxNode.new(input, (index-1)...index) if r2 == true
r0 = r2
else
if (match_len = has_terminal?('color', false, index))
if (match_len = has_terminal?('em', false, index))
r3 = instantiate_node(SyntaxNode,input, index...(index + match_len))
@index += match_len
else
terminal_parse_failure('color')
terminal_parse_failure('em')
r3 = nil
end
if r3
r3 = SyntaxNode.new(input, (index-1)...index) if r3 == true
r0 = r3
else
if (match_len = has_terminal?('del', false, index))
if (match_len = has_terminal?('code', false, index))
r4 = instantiate_node(SyntaxNode,input, index...(index + match_len))
@index += match_len
else
terminal_parse_failure('del')
terminal_parse_failure('code')
r4 = nil
end
if r4
r4 = SyntaxNode.new(input, (index-1)...index) if r4 == true
r0 = r4
else
if (match_len = has_terminal?('em', false, index))
if (match_len = has_terminal?('color', false, index))
r5 = instantiate_node(SyntaxNode,input, index...(index + match_len))
@index += match_len
else
terminal_parse_failure('em')
terminal_parse_failure('color')
r5 = nil
end
if r5
Expand Down Expand Up @@ -422,11 +422,11 @@ def _nt_tag_name
r7 = SyntaxNode.new(input, (index-1)...index) if r7 == true
r0 = r7
else
if (match_len = has_terminal?('strong', false, index))
if (match_len = has_terminal?('button', false, index))
r8 = instantiate_node(SyntaxNode,input, index...(index + match_len))
@index += match_len
else
terminal_parse_failure('strong')
terminal_parse_failure('button')
r8 = nil
end
if r8
Expand Down Expand Up @@ -455,8 +455,20 @@ def _nt_tag_name
r10 = SyntaxNode.new(input, (index-1)...index) if r10 == true
r0 = r10
else
@index = i0
r0 = nil
if (match_len = has_terminal?('del', false, index))
r11 = instantiate_node(SyntaxNode,input, index...(index + match_len))
@index += match_len
else
terminal_parse_failure('del')
r11 = nil
end
if r11
r11 = SyntaxNode.new(input, (index-1)...index) if r11 == true
r0 = r11
else
@index = i0
r0 = nil
end
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/asciidoctor-pdf/formatted_text/parser.treetop
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ grammar Markup
# QUESTION faster to do regex?
# QUESTION can we cut stuff we aren't using? what about supporting hr?
#'a' / 'b' / 'code' / 'color' / 'del' / 'em' / 'font' / 'i' / 'img' / 'link' / 'span' / 'strikethrough' / 'strong' / 'sub' / 'sup' / 'u'
'a' / 'code' / 'color' / 'del' / 'em' / 'font' / 'span' / 'strong' / 'sub' / 'sup'
'a' / 'strong' / 'em' / 'code' / 'color' / 'font' / 'span' / 'button' / 'sub' / 'sup' / 'del'
end

rule void_tag_name
Expand Down
63 changes: 40 additions & 23 deletions lib/asciidoctor-pdf/formatted_text/transform.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,44 @@ def initialize(options = {})
@merge_adjacent_text_nodes = options[:merge_adjacent_text_nodes]
# TODO add support for character spacing
if (theme = options[:theme])
@link_font_settings = {
color: theme.link_font_color,
font: theme.link_font_family,
size: theme.link_font_size,
styles: to_styles(theme.link_font_style, theme.link_text_decoration)
}.compact
@monospaced_font_settings = {
color: theme.literal_font_color,
font: theme.literal_font_family,
size: theme.literal_font_size,
styles: to_styles(theme.literal_font_style),
background_color: (monospaced_bg_color = theme.literal_background_color),
border_width: (monospaced_border_width = theme.literal_border_width),
border_color: monospaced_border_width && (theme.literal_border_color || theme.base_border_color),
border_offset: (monospaced_bg_or_border = monospaced_bg_color || monospaced_border_width) && theme.literal_border_offset,
border_radius: monospaced_bg_or_border && theme.literal_border_radius,
callback: monospaced_bg_or_border && [TextBackgroundAndBorderRenderer],
}.compact
@theme_settings = {
button: {
color: theme.button_font_color,
font: theme.button_font_family,
size: theme.button_font_size,
styles: to_styles(theme.button_font_style),
background_color: (button_bg_color = theme.button_background_color),
border_width: (button_border_width = theme.button_border_width),
border_color: button_border_width && (theme.button_border_color || theme.base_border_color),
border_offset: (button_bg_or_border = button_bg_color || button_border_width) && theme.button_border_offset,
border_radius: button_bg_or_border && theme.button_border_radius,
callback: button_bg_or_border && [TextBackgroundAndBorderRenderer],
}.compact,
code: {
color: theme.literal_font_color,
font: theme.literal_font_family,
size: theme.literal_font_size,
styles: to_styles(theme.literal_font_style),
background_color: (monospaced_bg_color = theme.literal_background_color),
border_width: (monospaced_border_width = theme.literal_border_width),
border_color: monospaced_border_width && (theme.literal_border_color || theme.base_border_color),
border_offset: (monospaced_bg_or_border = monospaced_bg_color || monospaced_border_width) && theme.literal_border_offset,
border_radius: monospaced_bg_or_border && theme.literal_border_radius,
callback: monospaced_bg_or_border && [TextBackgroundAndBorderRenderer],
}.compact,
link: {
color: theme.link_font_color,
font: theme.link_font_family,
size: theme.link_font_size,
styles: to_styles(theme.link_font_style, theme.link_text_decoration)
}.compact,
}
else
@link_font_settings = { color: '0000FF' }
@monospaced_font_settings = { font: 'Courier', size: 0.9 }
@theme_settings = {
button: { font: 'Courier', styles: [:bold].to_set },
code: { font: 'Courier', size: 0.9 },
link: { color: '0000FF' },
}
end
end

Expand Down Expand Up @@ -129,9 +146,9 @@ def build_fragment(fragment, tag_name, attrs = {})
styles << :bold
when :em
styles << :italic
when :code
when :code, :button
# NOTE prefer old value, except for styles and callback, which should be combined
fragment.update(@monospaced_font_settings) {|k, oval, nval| k == :styles ? oval.merge(nval) : (k == :callback ? oval.union(nval) : oval) }
fragment.update(@theme_settings[tag_name]) {|k, oval, nval| k == :styles ? oval.merge(nval) : (k == :callback ? oval.union(nval) : oval) }
when :color
if !fragment[:color]
if (rgb = attrs[:rgb])
Expand Down Expand Up @@ -202,7 +219,7 @@ def build_fragment(fragment, tag_name, attrs = {})
end
end
# NOTE prefer old value, except for styles, which should be combined
fragment.update(@link_font_settings) {|k, old_v, new_v| k == :styles ? old_v.merge(new_v) : old_v } if visible
fragment.update(@theme_settings[:link]) {|k, oval, nval| k == :styles ? oval.merge(nval) : oval } if visible
when :sub
styles << :subscript
when :sup
Expand Down
Binary file added spec/reference/text-formatter-button.pdf
Binary file not shown.
14 changes: 13 additions & 1 deletion spec/text_formatter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@
(expect text[0][:y]).to be > text[1][:y]
end

it 'should add background and border to monospaced text defined in theme', integration: true do
it 'should add background and border to code as defined in theme', integration: true do
theme_overrides = {
literal_background_color: 'f5f5f5',
literal_border_color: 'dddddd',
Expand All @@ -112,5 +112,17 @@
to_file = to_pdf_file 'All your `code` belongs to us.', 'text-formatter-code.pdf', pdf_theme: theme_overrides, analyze: true
(expect to_file).to visually_match 'text-formatter-code.pdf'
end

it 'should add background and border to button as defined in theme', integration: true do
theme_overrides = {
button_content: '%s',
button_background_color: '007BFF',
button_border_offset: 1.5,
button_border_radius: 2.5,
button_font_color: 'ffffff',
}
to_file = to_pdf_file 'Click btn:[Save] to save your work.', 'text-formatter-button.pdf', pdf_theme: theme_overrides, attribute_overrides: { 'experimental' => '' }, analyze: true
(expect to_file).to visually_match 'text-formatter-button.pdf'
end
end
end

0 comments on commit ef54f48

Please sign in to comment.