From 0be19a8460662bec12dc28e9132b43e38aaf8b1f Mon Sep 17 00:00:00 2001 From: Dan Allen Date: Mon, 2 Sep 2019 03:02:41 -0600 Subject: resolves #1246 don't crash when adding indentation guards to source highlighted with Pygments (PR #1248) --- CHANGELOG.adoc | 1 + lib/asciidoctor-pdf/converter.rb | 16 +++++++++------- spec/source_spec.rb | 23 +++++++++++++++++++++-- 3 files changed, 31 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.adoc b/CHANGELOG.adoc index 14a8acea..2a4e0bde 100644 --- a/CHANGELOG.adoc +++ b/CHANGELOG.adoc @@ -8,6 +8,7 @@ For a detailed view of what has changed, refer to the {uri-repo}/commits/master[ == Unreleased * allow font catalog and font fallbacks to be defined as flat keys in the theme file (#1243) +* don't crash when adding indentation guards to source highlighted with Pygments (#1246) == 1.5.0.beta.3 (2019-08-30) - @mojavelinux diff --git a/lib/asciidoctor-pdf/converter.rb b/lib/asciidoctor-pdf/converter.rb index c848a157..9a36a096 100644 --- a/lib/asciidoctor-pdf/converter.rb +++ b/lib/asciidoctor-pdf/converter.rb @@ -1808,8 +1808,7 @@ class Converter < ::Prawn::Document # append conums to appropriate lines, then flatten to an array of fragments lines.flat_map.with_index do |line, cur_line_num| last_line = cur_line_num == last_line_num - # NOTE use ::String.new to ensure string is not frozen - line.unshift text: (::String.new %(#{(cur_line_num + linenums).to_s.rjust pad_size} )), color: linenum_color if linenums + line.unshift text: %(#{(cur_line_num + linenums).to_s.rjust pad_size} ), color: linenum_color if linenums if (conums = conum_mapping.delete cur_line_num) line << { text: ' ' * num_trailing_spaces } if last_line && num_trailing_spaces > 0 conum_text = conums.map {|num| conum_glyph num }.join ' ' @@ -3480,10 +3479,10 @@ class Converter < ::Prawn::Document arranger = ::Prawn::Text::Formatted::Arranger.new self by_line = arranger.consumed = [] fragments.each do |fragment| - if (txt = fragment[:text]) == LF + if (text = fragment[:text]) == LF by_line << fragment - elsif txt.include? LF - txt.scan(LineScanRx) do |line| + elsif text.include? LF + text.scan(LineScanRx) do |line| by_line << (line == LF ? { text: LF } : (fragment.merge text: line)) end else @@ -3576,8 +3575,11 @@ class Converter < ::Prawn::Document start_of_line = true fragments.each do |fragment| next if (text = fragment[:text]).empty? - text[0] = GuardedIndent if start_of_line && (text.start_with? ' ') - text.gsub! InnerIndent, GuardedInnerIndent if text.include? InnerIndent + if start_of_line && (text.start_with? ' ') + fragment[:text] = GuardedIndent + (((text = text.slice 1, text.length).include? InnerIndent) ? (text.gsub InnerIndent, GuardedInnerIndent) : text) + elsif text.include? InnerIndent + fragment[:text] = text.gsub InnerIndent, GuardedInnerIndent + end start_of_line = text.end_with? LF end fragments diff --git a/spec/source_spec.rb b/spec/source_spec.rb index 38c7e49e..16fd5e76 100644 --- a/spec/source_spec.rb +++ b/spec/source_spec.rb @@ -142,7 +142,7 @@ describe 'Asciidoctor::PDF::Converter - Source' do end context 'Pygments' do - it 'should highlight source using CodeRay if source-highlighter is coderay' do + it 'should highlight source using Pygments if source-highlighter is pygments' do pdf = to_pdf <<~'EOS', analyze: true :source-highlighter: pygments @@ -158,9 +158,28 @@ describe 'Asciidoctor::PDF::Converter - Source' do (expect hello_text[:font_name]).to eql 'mplus1mn-regular' end + it 'should not crash when adding indentation guards' do + (expect { + pdf = to_pdf <<~EOS, analyze: true + :source-highlighter: pygments + + [source,yaml] + --- + category: + hash: + key: "value" + --- + EOS + (expect pdf.find_text 'category:').to have_size 1 + (expect pdf.find_text %(\u00a0 hash:)).to have_size 1 + (expect pdf.find_text %(\u00a0 key: )).to have_size 1 + (expect pdf.find_text '"value"').to have_size 1 + }).not_to raise_exception + end + it 'should not crash when aligning line numbers' do (expect { - to_pdf <<~'EOS', analyze: true + to_pdf <<~'EOS' :source-highlighter: pygments [source,xml,linenums] -- cgit v1.2.3