jgm / pandoc

Universal markup converter
https://pandoc.org
Other
34.69k stars 3.39k forks source link

Markdown -> Latex not calling CodeBlock on certain indents #8078

Open stephanlachnit opened 2 years ago

stephanlachnit commented 2 years ago

Explain the problem. mwe.md:

This is code outside of a list:
```math
a^2+b^2=c^2

pandoc-gitlab-math.lua:

function Math(el)
    if el.mathtype == "InlineMath" then
        if el.text:sub(1,1) == '`' and el.text:sub(#el.text) == '`' then
            local text = el.text:sub(2,#el.text-1)
            return pandoc.Math(el.mathtype, text)
        else
            local cont = pandoc.read(el.text)
            return { pandoc.Str("$") } .. cont.blocks[1].content .. { pandoc.Str("$") }
        end
    end
end

function CodeBlock(el)
    if el.classes[1] == "math" then
        return pandoc.Para({ pandoc.Math("DisplayMath", el.text) })
    end
end

Running:

pandoc -f markdown -t latex --listings --lua-filter pandoc-gitlab-math.lua ./mwe.md

Produces:

This is code outside of a list:

\[a^2+b^2=c^2\]

\begin{itemize}
\item
  This is code in \passthrough{\lstinline!-!} list:

  \[a^2+b^2=c^2\]
\item
  This is a code in a \passthrough{\lstinline!-!} list:
  \passthrough{\lstinline!math     a\^2+b\^2=c\^2!}
\end{itemize}

The issue here is that the third CodeBlock is not converted properly by the Lua script.

Pandoc version? Debian Sid, pandoc version:

pandoc 2.18
Compiled with pandoc-types 1.22.2, texmath 0.12.5, skylighting 0.12.3,
citeproc 0.7, ipynb 0.2, hslua 2.2.0
Scripting engine: Lua 5.4
jgm commented 2 years ago

Looks like you're missing the closing fence on that last item.

stephanlachnit commented 2 years ago

Looks like you're missing the closing fence on that last item.

Only missed the last line when copying it here - sorry. In the MWE I run it was there.

jgm commented 2 years ago

Using pandoc -t native, you can see that the code block in the last item is being parsed as inline code. That's the root of the problem:

    , [ Para
          [ Str "This"
          , Space
          , Str "is"
          , Space
          , Str "a"
          , Space
          , Str "code"
          , Space
          , Str "in"
          , Space
          , Str "a"
          , Space
          , Code ( "" , [] , [] ) "-"
          , Space
          , Str "list:"
          , SoftBreak
          , Code ( "" , [] , [] ) "math     a^2+b^2=c^2"
          ]
      ]

Why is that happening, though? I need to investigate.

jgm commented 2 years ago

The -f commonmark (or -f gfm) parsers handle this correctly, by the way.