Skip to content

Commit

Permalink
resolves asciidoctor#1309 allow theme to specify background color and…
Browse files Browse the repository at this point in the history
… full border for quote and verse blocks
  • Loading branch information
mojavelinux committed Dec 31, 2019
1 parent 9719846 commit af28601
Show file tree
Hide file tree
Showing 10 changed files with 125 additions and 16 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ For a detailed view of what has changed, refer to the {uri-repo}/commits/master[
* allow theme to specify background image for running content (#356)
* allow theme to specify border for admonition block (#444)
* allow theme to specify background color for admonition block (#444)
* allow theme to specify background color and full border for quote and verse blocks (#1309)
* allow text alignment roles to be used to control alignment of discrete heading
* use font color from pygments style for unhighlighted text (#1441)
* render stem block as raw literal block
Expand Down
4 changes: 2 additions & 2 deletions data/themes/base-theme.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@ admonition_padding: [0, 12, 0, 12]
admonition_label_font_style: bold
admonition_label_text_transform: uppercase
blockquote_border_color: 'EEEEEE'
blockquote_border_width: 4
blockquote_border_left_width: 4
blockquote_padding: [6, 12, -6, 14]
verse_border_color: 'EEEEEE'
verse_border_width: 4
verse_border_left_width: 4
verse_padding: [6, 12, -6, 14]
code_font_family: Courier
code_font_size: 10.8
Expand Down
6 changes: 4 additions & 2 deletions data/themes/default-theme.yml
Original file line number Diff line number Diff line change
Expand Up @@ -173,15 +173,17 @@ admonition:
blockquote:
font_size: $base_font_size_large
border_color: $base_border_color
border_width: 5
border_width: 0
border_left_width: 5
# FIXME disable negative padding bottom once margin collapsing is implemented
padding: [0, $horizontal_rhythm, $block_margin_bottom * -0.75, $horizontal_rhythm + $blockquote_border_width / 2]
padding: [0, $horizontal_rhythm, $block_margin_bottom * -0.75, $horizontal_rhythm + $blockquote_border_left_width / 2]
cite_font_size: $base_font_size_small
cite_font_color: 999999
verse:
font_size: $blockquote_font_size
border_color: $blockquote_border_color
border_width: $blockquote_border_width
border_left_width: $blockquote_border_left_width
padding: $blockquote_padding
cite_font_size: $blockquote_cite_font_size
cite_font_color: $blockquote_cite_font_color
Expand Down
34 changes: 30 additions & 4 deletions docs/theming-guide.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -2621,11 +2621,23 @@ The keys in this category control the arrangement and style of quote blocks.

3+|[#key-prefix-blockquote]*Key Prefix:* <<key-prefix-blockquote,blockquote>>

|background-color
|<<colors,Color>> +
(default: _not set_)
|blockquote:
background-color: #dddddd

|border-width^[1]^
|<<values,Number>> +
(default: 0)
|blockquote:
border-width: 0.5

|border-left-width^[1]^
|<<values,Number>> +
(default: 4)
|blockquote:
border-width: 5
border-left-width: 5

|border-color^[1]^
|<<colors,Color>> +
Expand Down Expand Up @@ -2720,7 +2732,8 @@ The keys in this category control the arrangement and style of quote blocks.
text-transform: uppercase
|===

. Only applies to the left side.
. If border-left-width is non-zero, the border is only applies to the left side.
Otherwise, if border-width is non-zero, the border is drawn around the whole block.

[#keys-verse]
=== Verse
Expand All @@ -2733,11 +2746,23 @@ The keys in this category control the arrangement and style of verse blocks.

3+|[#key-prefix-verse]*Key Prefix:* <<key-prefix-verse,verse>>

|background-color
|<<colors,Color>> +
(default: _not set_)
|verse:
background-color: #dddddd

|border-width^[1]^
|<<values,Number>> +
(default: 0)
|verse:
border-width: 0.5

|border-left-width^[1]^
|<<values,Number>> +
(default: 4)
|verse:
border-width: 5
border-left-width: 5

|border-color^[1]^
|<<colors,Color>> +
Expand Down Expand Up @@ -2832,7 +2857,8 @@ The keys in this category control the arrangement and style of verse blocks.
text-transform: uppercase
|===

. Only applies to the left side.
. If border-left-width is non-zero, the border is only applies to the left side.
Otherwise, if border-width is non-zero, the border is drawn around the whole block.

[#keys-sidebar]
=== Sidebar
Expand Down
54 changes: 50 additions & 4 deletions lib/asciidoctor/pdf/converter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -983,10 +983,56 @@ def convert_quote_or_verse node
add_dest_for_block node if node.id
theme_margin :block, :top
category = node.context == :quote ? :blockquote : :verse
b_width = @theme[%(#{category}_border_width)] || 0
b_color = @theme[%(#{category}_border_color)]
b_width = (b_left_width = @theme[%(#{category}_border_left_width)] || 0) > 0 ? 0 : (@theme[%(#{category}_border_width)] || 0)
b_color = @theme[%(#{category}_border_color)] if b_width > 0 || b_left_width > 0
bg_color = @theme[%(#{category}_background_color)]
keep_together do |box_height = nil|
push_scratch node.document if scratch?
if box_height && (b_width > 0 || bg_color)
# FIXME: due to the calculation error logged in #789, we must advance page even when content is split across pages
advance_page if box_height > cursor && !at_page_top?
float do
# TODO: move the multi-page logic to theme_fill_and_stroke_bounds
if b_width
if b_color == @page_bg_color # let page background cut into sidebar background
b_gap_color, b_shift = @page_bg_color, b_width
elsif (b_gap_color = bg_color) && b_gap_color != b_color
b_shift = 0
else # let page background cut into border
b_gap_color, b_shift = @page_bg_color, 0
end
else # let page background cut into sidebar background
b_width = 0.5 if b_width == 0
b_shift, b_gap_color = b_width * 0.5, @page_bg_color
end
b_radius = @theme[%(#{category}_border_radius)] || 0
initial_page, remaining_height = true, box_height
while remaining_height > 0
advance_page unless initial_page
fragment_height = [(available_height = cursor), remaining_height].min
bounding_box [0, available_height], width: bounds.width, height: fragment_height do
theme_fill_and_stroke_bounds category
unless b_width == 0
indent b_radius, b_radius do
move_down b_shift
# dashed line to indicate continuation from previous page; swell line to cover background
stroke_horizontal_rule b_gap_color, line_width: b_width * 1.2, line_style: :dashed
move_up b_shift
end unless initial_page
if remaining_height > fragment_height
move_down fragment_height - b_shift
indent b_radius, b_radius do
# dashed line to indicate continuation to next page; swell line to cover background
stroke_horizontal_rule b_gap_color, line_width: b_width * 1.2, line_style: :dashed
end
end
end
end
remaining_height -= fragment_height
initial_page = false
end
end
end
start_page_number = page_number
start_cursor = cursor
caption_height = node.title? ? (layout_caption node, category: category) : 0
Expand All @@ -1007,7 +1053,7 @@ def convert_quote_or_verse node
end
# FIXME: we want to draw graphics before content, but box_height is not reliable when spanning pages
# FIXME: border extends to bottom of content area if block terminates at bottom of page
if box_height && b_width > 0
if box_height && b_left_width > 0
page_spread = page_number - start_page_number + 1
end_cursor = cursor
go_to_page start_page_number
Expand All @@ -1034,7 +1080,7 @@ def convert_quote_or_verse node
# NOTE: b_height is 0 when block terminates at bottom of page
next if b_height == 0
bounding_box [0, y_draw], width: bounds.width, height: b_height do
stroke_vertical_rule b_color, line_width: b_width, at: b_width / 2.0
stroke_vertical_rule b_color, line_width: b_left_width, at: b_left_width * 0.5
end
end
end
Expand Down
Binary file added spec/output/quote-border-and-background-color.pdf
Binary file not shown.
37 changes: 35 additions & 2 deletions spec/quote_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
(expect title_text[:x]).to eql 48.24
end

it 'should not draw left border if border_width is 0' do
pdf = to_pdf <<~'EOS', pdf_theme: { blockquote_border_width: 0 }, analyze: :line
it 'should not draw left border if border_left_width is 0' do
pdf = to_pdf <<~'EOS', pdf_theme: { blockquote_border_left_width: 0 }, analyze: :line
____
Let it be.
____
Expand Down Expand Up @@ -50,4 +50,37 @@
(expect quote_borders).to have_size 1
(expect quote_borders[0][:page_number]).to be 1
end

it 'should apply specified background color', visual: true do
pdf_theme = {
blockquote_background_color: 'dddddd',
blockquote_border_color: 'aa0000',
}
to_file = to_pdf_file <<~'EOS', 'quote-background-color.pdf', pdf_theme: pdf_theme
____
Let it be. +
Let it be.
____
EOS

(expect to_file).to visually_match 'quote-background-color.pdf'
end

it 'should apply specified border and background color', visual: true do
pdf_theme = build_pdf_theme \
blockquote_border_left_width: 0,
blockquote_border_width: 0.5,
blockquote_border_color: 'aa0000',
blockquote_background_color: 'dddddd'
pdf_theme.blockquote_padding = pdf_theme.sidebar_padding
to_file = to_pdf_file <<~'EOS', 'quote-border-and-background-color.pdf', pdf_theme: pdf_theme
[,Paul McCartney]
____
Let it be. +
Let it be.
____
EOS

(expect to_file).to visually_match 'quote-border-and-background-color.pdf'
end
end
Binary file added spec/reference/quote-background-color.pdf
Binary file not shown.
Binary file not shown.
5 changes: 3 additions & 2 deletions spec/verse_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@
(expect lines[2]).to eql %(\u00a0 go)
end

it 'should not draw left border if border_width is 0' do
pdf = to_pdf <<~'EOS', pdf_theme: { blockquote_border_width: 0 }, analyze: :line
it 'should not draw left border if border_left_width is 0' do
pdf = to_pdf <<~'EOS', pdf_theme: { verse_border_left_width: 0 }, analyze: :line
[verse]
____
here
we
Expand Down

0 comments on commit af28601

Please sign in to comment.