Skip to content

Commit

Permalink
Merge pull request #1 from mojavelinux/pr/332-review
Browse files Browse the repository at this point in the history
optimize custom admonition implementation. #332
  • Loading branch information
jessedoyle committed Oct 13, 2015
2 parents 3829962 + 21e47e8 commit 575f433
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 33 deletions.
32 changes: 13 additions & 19 deletions lib/asciidoctor-pdf/converter.rb
Expand Up @@ -33,11 +33,11 @@ class Converter < ::Prawn::Document

AsciidoctorVersion = ::Gem::Version.create ::Asciidoctor::VERSION
AdmonitionIcons = {
caution: { key: 'fa-fire', color: 'BF3400', size: 24 },
important: { key: 'fa-exclamation-circle', color: 'BF0000', size: 24 },
note: { key: 'fa-info-circle', color: '19407C', size: 24 },
tip: { key: 'fa-lightbulb-o', color: '111111', size: 24 },
warning: { key: 'fa-exclamation-triangle', color: 'BF6900', size: 24 }
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,14 +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 = admonition_icon_data label
opts = {
icon_data = admonition_icon_data label
icon icon_data[:name], {
valign: :center,
align: :center,
color: admon_icon_data[:color],
size: (admonition_icon_size node, admon_icon_data[:size])
color: icon_data[:stroke_color],
size: (fit_icon_size node, icon_data[:size])
}
icon admon_icon_data[:key], opts
else
layout_prose label, valign: :center, style: :bold, line_height: 1, margin_top: label_margin_top, margin_bottom: 0
end
Expand Down Expand Up @@ -1776,18 +1775,13 @@ 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 @theme.admonition_icons && @theme.admonition_icons[key]
@theme.admonition_icons[key].tap do |data|
AdmonitionIcons[key].each do |k, v|
data[k] ||= v
end
end
if (icon_data = @theme[%(admonition_icon_#{key})])
AdmonitionIcons[key].merge icon_data
else
AdmonitionIcons[key]
end
Expand Down
19 changes: 5 additions & 14 deletions lib/asciidoctor-pdf/theme_loader.rb
Expand Up @@ -86,8 +86,11 @@ def load hash, theme_data = nil
private

def process_entry key, val, data
if key == "admonition_icons"
data[key] = symbolize_keys 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
Expand Down Expand Up @@ -225,18 +228,6 @@ def to_color value
end
HexColorValue.new value.upcase
end

def symbolize_keys hash
hash.inject({}) do |data, (k, v)|
data.tap do |i|
if v.is_a? Hash
i[k.to_sym] = symbolize_keys v
else
i[k.to_sym] = v
end
end
end
end
end
end
end

0 comments on commit 575f433

Please sign in to comment.