tomduck / pandoc-xnos

Library code for pandoc-fignos/eqnos/tablenos/secnos.
GNU General Public License v3.0
106 stars 27 forks source link

Fixed bug https://github.com/tomduck/pandoc-eqnos/issues/30 #5

Closed Gjacquenot closed 6 years ago

Gjacquenot commented 6 years ago

This commit fixes bug that appears in another repository: https://github.com/tomduck/pandoc-eqnos/issues/30. The reported error indicates that problem occurs in pandocxnos/core.py. When one declares an equation without identifiers, it appears that a key is missing in value[0] dict. I fixed the problem by creating the missing the missing entry and initialized it with an empty list. With this patch, the proposed bug.mdfile can be filtered correctly and pandoc runs smoothly.

Here is a copy of the reported bug:

When using xnos-number-sections: On, it seems like all equations need an identifier {#eq:}. If not, an error is thrown. When NOT using this option, equation is not numbered and that is fine. We would expect the same behaviour with this option

Below is minimal markdown file that reproduce the error:

I use the latest versions of pandoc-eqnos with command

pip install -U pandoc-eqnos

File bug.md

---
xnos-number-sections: On
...

# Title 1

$$ x $$ {#eq:}

Symbol $x$ has no reference

Pandoc command:

pandoc --mathml --filter pandoc-eqnos -o bug.html bug.md

Error message:

Traceback (most recent call last):
  File "c:\program files\python36\lib\runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "c:\program files\python36\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "C:\Program Files\Python36\Scripts\pandoc-eqnos.exe\__main__.py", line 9, in <module>
  File "c:\program files\python36\lib\site-packages\pandoc_eqnos.py", line 301, in main
    detach_attrs_math], blocks)
  File "c:\program files\python36\lib\site-packages\pandoc_eqnos.py", line 298, in <lambda>
    altered = functools.reduce(lambda x, action: walk(x, action, fmt, meta),
  File "c:\program files\python36\lib\site-packages\pandocfilters.py", line 113, in walk
    array.append(walk(item, action, format, meta))
  File "c:\program files\python36\lib\site-packages\pandocfilters.py", line 124, in walk
    x[k] = walk(x[k], action, format, meta)
  File "c:\program files\python36\lib\site-packages\pandocfilters.py", line 111, in walk
    item['c'] if 'c' in item else None, format, meta)
  File "c:\program files\python36\lib\site-packages\pandocxnos\core.py", line 893, in insert_secnos
    value[0][2].insert(0, ['secno', s])
KeyError: 2
Error running filter pandoc-eqnos:
Filter returned error status 1
Gjacquenot commented 6 years ago

Hello @tomduck , I hope you are less buzy and have some time to look at pull requests on GitHub ;-)

tomduck commented 6 years ago

Thanks for this. I took a little bit of a different approach. The underlying problem was that I didn't check for attached attributes before manipulating them.

The bug fix is in the nextrelease branch.

Thanks for identifying the problem, and for your patience.

Cheers, Tom