asciidoctor / asciidoctor-mathematical

An extension for Asciidoctor that converts the content of STEM blocks and inline macros using Mathematical.
MIT License
49 stars 44 forks source link

asciidoctor-pdf fails for missing 'a' attribute in cells with stem blocks #73

Closed rillbert closed 3 years ago

rillbert commented 4 years ago

Hello there,

I discovered that if I use inline stem blocks in table cells without specifying the 'a' attribute at the beginning of the cell, asciidoctor-pdf will fail with an error message that is quite cryptic and makes it difficult to track what is wrong with the source.

This can be reproduced by eg removing the leading 'a' character in line 82 in the attached source file. pdf_stem_bug.txt

(this was discovered at the same time as asciidoctor/asciidoctor-pdf#1516 but I thought it best to separate these two issues).

Running with the --trace option, the following stack trace is generated:

$ asciidoctor-pdf --trace -a stem=latexmath -r asciidoctor-mathematical -a mathematical-format=svg ./pdf_stem_bug.adoc
Traceback (most recent call last):
    18: from /home/anders/.rbenv/versions/2.5.3/bin/asciidoctor-pdf:23:in `<main>'
    17: from /home/anders/.rbenv/versions/2.5.3/bin/asciidoctor-pdf:23:in `load'
    16: from /home/anders/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/asciidoctor-pdf-1.5.0.beta.6/bin/asciidoctor-pdf:32:in `<top (required)>'
    15: from /home/anders/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/asciidoctor-2.0.10/lib/asciidoctor/cli/invoker.rb:111:in `invoke!'
    14: from /home/anders/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/asciidoctor-2.0.10/lib/asciidoctor/cli/invoker.rb:111:in `each'
    13: from /home/anders/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/asciidoctor-2.0.10/lib/asciidoctor/cli/invoker.rb:128:in `block in invoke!'
    12: from /home/anders/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/asciidoctor-2.0.10/lib/asciidoctor/convert.rb:183:in `convert_file'
    11: from /home/anders/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/asciidoctor-2.0.10/lib/asciidoctor/convert.rb:183:in `open'
    10: from /home/anders/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/asciidoctor-2.0.10/lib/asciidoctor/convert.rb:183:in `block in convert_file'
     9: from /home/anders/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/asciidoctor-2.0.10/lib/asciidoctor/convert.rb:78:in `convert'
     8: from /home/anders/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/asciidoctor-2.0.10/lib/asciidoctor/load.rb:83:in `load'
     7: from /home/anders/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/asciidoctor-2.0.10/lib/asciidoctor/document.rb:555:in `parse'
     6: from /home/anders/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/asciidoctor-2.0.10/lib/asciidoctor/document.rb:555:in `each'
     5: from /home/anders/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/asciidoctor-2.0.10/lib/asciidoctor/document.rb:556:in `block in parse'
     4: from /home/anders/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/asciidoctor-2.0.10/lib/asciidoctor/document.rb:556:in `[]'
     3: from /home/anders/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/asciidoctor-mathematical-0.3.1/lib/asciidoctor-mathematical/extension.rb:40:in `process'
     2: from /home/anders/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/asciidoctor-mathematical-0.3.1/lib/asciidoctor-mathematical/extension.rb:40:in `each'
     1: from /home/anders/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/asciidoctor-mathematical-0.3.1/lib/asciidoctor-mathematical/extension.rb:41:in `block in process'
/home/anders/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/asciidoctor-mathematical-0.3.1/lib/asciidoctor-mathematical/extension.rb:105:in `handle_prose_block': asciidoctor: FAILED: /home/anders/vironova/tmp/pdf_stem_bug.adoc: Failed to load AsciiDoc document - undefined method `lines=' for #<Asciidoctor::Table::Cell:0x000055f873a917c8> (NoMethodError)
Did you mean?  lines
               lineno
mojavelinux commented 4 years ago

will fail with an error message that is quite cryptic and makes it difficult to track what is wrong with the source.

It's doing exactly what it should, which is to provide enough information to the developer to fix the bug. Crashes aren't supposed to be pretty because under normal circumstances, they aren't suppose to happen.

mojavelinux commented 4 years ago

This is a bug in Asciidoctor Mathematical. It is matching normal table cells, but processing them as paragraphs.

mojavelinux commented 4 years ago

@ProgramFan Since version 2.0.0, Asciidoctor matches normal table cells when you use find_by. A quick fix for this is to exclude the :table_cell context from the initial call to find_by to retrieve prose blocks:

    unless (prose_blocks = document.find_by {|b|
      ((b.content_model == :simple && (b.subs.include? :macros)) || b.context == :list_item) && b.context != :table_cell
    }).nil_or_empty?
      prose_blocks.each do |prose|
        handle_prose_block prose, mathematical, image_output_dir, image_target_dir, format, inline
      end
    end

Alternately, you could add the condition inside the each loop, and call the appropriate method.

mojavelinux commented 3 years ago

This is a duplicate of #61. At the time of writing, a fix is pending.