diff options
| author | Dan Allen <dan.j.allen@gmail.com> | 2019-07-28 23:53:19 -0600 |
|---|---|---|
| committer | Dan Allen <dan.j.allen@gmail.com> | 2019-07-29 00:57:33 -0600 |
| commit | c291fa5cae2fc42595f22a4cdb2cc4f875214d02 (patch) | |
| tree | f7ac6cab83b635052d168cf1e1df63f8b83774fd | |
| parent | d1ea2de5723daf7b62dab1dc78cf1192ca66ea76 (diff) | |
don't crash if no keys are defined by theme
| -rw-r--r-- | lib/asciidoctor-pdf/converter.rb | 44 | ||||
| -rw-r--r-- | lib/asciidoctor-pdf/theme_loader.rb | 2 | ||||
| -rw-r--r-- | spec/converter_spec.rb | 22 | ||||
| -rw-r--r-- | spec/theme_loader_spec.rb | 3 |
4 files changed, 46 insertions, 25 deletions
diff --git a/lib/asciidoctor-pdf/converter.rb b/lib/asciidoctor-pdf/converter.rb index 435b7a29..04ae361a 100644 --- a/lib/asciidoctor-pdf/converter.rb +++ b/lib/asciidoctor-pdf/converter.rb @@ -201,7 +201,7 @@ class Converter < ::Prawn::Document end # NOTE font must be set before toc dry run to ensure dry run size is accurate - font @theme.base_font_family, size: @theme.base_font_size, style: @theme.base_font_style.to_sym + font @theme.base_font_family, size: @theme.base_font_size, style: (@theme.base_font_style || :normal).to_sym num_toc_levels = (doc.attr 'toclevels', 2).to_i if (insert_toc = (doc.attr? 'toc') && doc.sections?) @@ -518,7 +518,7 @@ class Converter < ::Prawn::Document end else # FIXME smarter calculation here!! - start_new_page unless at_page_top? || cursor > (height_of title) + @theme.heading_margin_top + @theme.heading_margin_bottom + (@theme.base_line_height_length * 1.5) + start_new_page unless at_page_top? || cursor > (height_of title) + @theme.heading_margin_top + @theme.heading_margin_bottom + ((@theme.base_line_height_length || (font_size * @theme.base_line_height)) * 1.5) end # QUESTION should we store pdf-page-start, pdf-anchor & pdf-destination in internal map? sect.set_attr 'pdf-page-start', (start_pgnum = page_number) @@ -1037,26 +1037,25 @@ class Converter < ::Prawn::Document terms = [*terms] # NOTE don't orphan the terms, allow for at least one line of content # FIXME extract ensure_space (or similar) method - advance_page if cursor < @theme.base_line_height_length * (terms.size + 1) + advance_page if cursor < (@theme.base_line_height_length || (font_size * @theme.base_line_height)) * (terms.size + 1) terms.each do |term| # FIXME layout_prose should pass style downward when parsing formatted text #layout_prose term.text, style: @theme.description_list_term_font_style.to_sym, margin_top: 0, margin_bottom: @theme.description_list_term_spacing, align: :left - term_text = term.text - case @theme.description_list_term_font_style.to_sym - when :bold - term_text = %(<strong>#{term_text}</strong>) - when :italic - term_text = %(<em>#{term_text}</em>) - when :bold_italic - term_text = %(<strong><em>#{term_text}</em></strong>) + case @theme.description_list_term_font_style.to_s + when 'bold' + term_text = %(<strong>#{term.text}</strong>) + when 'italic' + term_text = %(<em>#{term.text}</em>) + when 'bold_italic' + term_text = %(<strong><em>#{term.text}</em></strong>) + else + term_text = term.text end layout_prose term_text, margin_top: 0, margin_bottom: @theme.description_list_term_spacing, align: :left end - if desc - indent @theme.description_list_description_indent do - convert_content_for_list_item desc, :dlist_desc - end - end + indent(@theme.description_list_description_indent || 0) do + convert_content_for_list_item desc, :dlist_desc + end if desc end end end @@ -1159,12 +1158,12 @@ class Converter < ::Prawn::Document if node.style == 'unstyled' # unstyled takes away all indentation list_indent = 0 - elsif (list_indent = @theme.outline_list_indent) > 0 + elsif (list_indent = @theme.outline_list_indent || 0) > 0 # no-bullet aligns text with left-hand side of bullet position (as though there's no bullet) list_indent = [list_indent - (rendered_width_of_string %(#{node.context == :ulist ? ?\u2022 : '1.'}x)), 0].max end else - list_indent = @theme.outline_list_indent + list_indent = @theme.outline_list_indent || 0 end indent list_indent do node.items.each do |item| @@ -1177,8 +1176,7 @@ class Converter < ::Prawn::Document # However, don't leave gap at the bottom if list is nested in an outline list unless complex || (node.nested? && node.parent.parent.outline?) # correct bottom margin of last item - list_margin_bottom = @theme.prose_margin_bottom - margin_bottom list_margin_bottom - @theme.outline_list_item_spacing + margin_bottom((@theme.prose_margin_bottom || 0) - (@theme.outline_list_item_spacing || 0)) end end @@ -1651,7 +1649,7 @@ class Converter < ::Prawn::Document end pad_box @theme.code_padding do - typeset_formatted_text source_chunks, (calc_line_metrics @theme.code_line_height), + typeset_formatted_text source_chunks, (calc_line_metrics @theme.code_line_height || @theme.base_line_height), # QUESTION should we require the code_font_color to be set? color: (@theme.code_font_color || @font_color), size: adjusted_font_size @@ -2589,7 +2587,7 @@ class Converter < ::Prawn::Document string = transform_text string, transform end margin_top top_margin - typeset_text string, calc_line_metrics((opts.delete :line_height) || @theme[%(heading_h#{opts[:level]}_line_height)] || @theme.heading_line_height), { + typeset_text string, calc_line_metrics((opts.delete :line_height) || @theme[%(heading_h#{opts[:level]}_line_height)] || @theme.heading_line_height || @theme.base_line_height), { color: @font_color, inline_format: true, align: @base_align.to_sym @@ -3290,7 +3288,7 @@ class Converter < ::Prawn::Document # Insert a margin space at the specified side unless cursor is at the top of the page. # Start a new page if n value is greater than remaining space on page. def margin n, side - unless n == 0 || at_page_top? + unless (n || 0) == 0 || at_page_top? # NOTE use low-level cursor calculation to workaround cursor bug in column_box context if y - reference_bounds.absolute_bottom > n move_down n diff --git a/lib/asciidoctor-pdf/theme_loader.rb b/lib/asciidoctor-pdf/theme_loader.rb index 0f149c3a..cc79f2b7 100644 --- a/lib/asciidoctor-pdf/theme_loader.rb +++ b/lib/asciidoctor-pdf/theme_loader.rb @@ -70,8 +70,8 @@ class ThemeLoader else theme_data = load_file theme_file, nil, theme_path unless (::File.dirname theme_file) == ThemesDir - # QUESTION should we enforce any other fallback values? theme_data.base_align ||= 'left' + theme_data.base_line_height ||= 1 theme_data.base_font_color ||= '000000' theme_data.code_font_family ||= (theme_data.literal_font_family || 'Courier') theme_data.conum_font_family ||= (theme_data.literal_font_family || 'Courier') diff --git a/spec/converter_spec.rb b/spec/converter_spec.rb index 3be40c91..41215178 100644 --- a/spec/converter_spec.rb +++ b/spec/converter_spec.rb @@ -120,6 +120,28 @@ describe Asciidoctor::PDF::Converter do }).to (raise_exception Errno::ENOENT) & (log_message severity: :ERROR, message: '~could not locate or load the built-in pdf theme `foo\'') end + it 'should not crash if theme does not specify any keys' do + pdf = to_pdf <<~'EOS', attribute_overrides: { 'pdf-theme' => (fixture_file 'extends-nil-empty-theme.yml') }, analyze: true + = Document Title + :doctype: book + + This is the stark theme. + + == Section Title + + .dlist + term:: desc + + .ulist + * one + * two + * three + EOS + + (expect pdf.pages).to have_size 2 + (expect pdf.find_text font_name: 'Helvetica', font_size: 12).to have_size pdf.text.size + end + it 'should convert background position to options' do converter = asciidoctor_2_or_better? ? (Asciidoctor::Converter.create 'pdf') : (Asciidoctor::Converter::Factory.create 'pdf') { diff --git a/spec/theme_loader_spec.rb b/spec/theme_loader_spec.rb index 28860c3e..be37b7a8 100644 --- a/spec/theme_loader_spec.rb +++ b/spec/theme_loader_spec.rb @@ -168,10 +168,11 @@ describe Asciidoctor::PDF::ThemeLoader do theme = subject.load_theme 'extends-nil-empty-theme.yml', fixtures_dir (expect theme.__dir__).to eql fixtures_dir (expect theme.base_align).to eql 'left' + (expect theme.base_line_height).to eql 1 (expect theme.base_font_color).to eql '000000' (expect theme.code_font_family).to eql 'Courier' (expect theme.conum_font_family).to eql 'Courier' - (expect theme.to_h.keys).to have_size 5 + (expect theme.to_h.keys).to have_size 6 end it 'should not overwrite required keys with default values if already set' do |
