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

resolves #121. Admonition icon customization by theme. #332

Merged
merged 4 commits into from
Oct 13, 2015
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
34 changes: 23 additions & 11 deletions lib/asciidoctor-pdf/converter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ class Converter < ::Prawn::Document

AsciidoctorVersion = ::Gem::Version.create ::Asciidoctor::VERSION
AdmonitionIcons = {
caution: { key: 'fa-fire', color: 'BF3400' },
important: { key: 'fa-exclamation-circle', color: 'BF0000' },
note: { key: 'fa-info-circle', color: '19407C' },
tip: { key: 'fa-lightbulb-o', color: '111111' },
warning: { key: 'fa-exclamation-triangle', color: 'BF6900' }
caution: { name: 'fa-fire', stroke_color: 'BF3400', size: 24 },
important: { name: 'fa-exclamation-circle', stroke_color: 'BF0000', size: 24 },
note: { name: 'fa-info-circle', stroke_color: '19407C', size: 24 },
tip: { name: 'fa-lightbulb-o', stroke_color: '111111', size: 24 },
warning: { name: 'fa-exclamation-triangle', stroke_color: 'BF6900', size: 24 }
}
Alignments = [:left, :center, :right]
AlignmentNames = ['left', 'center', 'right']
Expand Down Expand Up @@ -435,8 +435,13 @@ def convert_admonition node
# FIXME HACK make title in this location look right
label_margin_top = node.title? ? @theme.caption_margin_inside : 0
if icons
admon_icon_data = AdmonitionIcons[label]
icon admon_icon_data[:key], valign: :center, align: :center, color: admon_icon_data[:color], size: (admonition_icon_size node)
icon_data = admonition_icon_data label
icon icon_data[:name], {
valign: :center,
align: :center,
color: icon_data[:stroke_color],
size: (fit_icon_size node, icon_data[:size])
}
else
layout_prose label, valign: :center, style: :bold, line_height: 1, margin_top: label_margin_top, margin_bottom: 0
end
Expand Down Expand Up @@ -1089,7 +1094,7 @@ def conum_glyph number
end

# Adds guards to preserve indentation
def guard_indentation fragments
def guard_indentation fragments
start_of_line = true
fragments.each do |fragment|
next if (text = fragment[:text]).empty?
Expand Down Expand Up @@ -1770,9 +1775,16 @@ def layout_toc_level sections, num_levels, line_metrics, dot_width, num_front_ma

# Reduce icon size to fit inside bounds.height. Icons will not render
# properly if they are larger than the current bounds.height.
def admonition_icon_size node, max_size = 24
min_height = bounds.height.floor
min_height < max_size ? min_height : max_size
def fit_icon_size node, max_size = 24
(min_height = bounds.height.floor) < max_size ? min_height : max_size
end

def admonition_icon_data key
if (icon_data = @theme[%(admonition_icon_#{key})])
AdmonitionIcons[key].merge icon_data
else
AdmonitionIcons[key]
end
end

# TODO delegate to layout_page_header and layout_page_footer per page
Expand Down
11 changes: 8 additions & 3 deletions lib/asciidoctor-pdf/theme_loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,12 @@ def load hash, theme_data = nil
private

def process_entry key, val, data
if key != 'font_catalog' && ::Hash === val
if key.start_with? 'admonition_icon_'
data[key] = (val || {}).map do |(key2, val2)|
static_val2 = evaluate val2, data
[key2.to_sym, (key2.end_with? '_color') ? to_color(static_val2) : static_val2]
end.to_h
elsif key != 'font_catalog' && ::Hash === val
val.each do |key2, val2|
process_entry %(#{key}_#{key2.tr '-', '_'}), val2, data
end
Expand All @@ -106,7 +111,7 @@ def evaluate expr, vars
expr
end
end

# NOTE we assume expr is a String
def expand_vars expr, vars
if (idx = (expr.index '$'))
Expand All @@ -119,7 +124,7 @@ def expand_vars expr, vars
expr
end
end

def evaluate_math expr
return expr if !(::String === expr) || ColorValue === expr
original = expr
Expand Down