Search code examples
filterluagitlabmarkdownpandoc

Is it posible to enable extensions on pandoc filters?


I'm trying to make a filter to transform some features of org-mode to GitLab-markdown (not supported by Pandoc out of the box), in particular, the math blocks.

The filter should work when transforming to markdown, but instead of giving the markdown format for the math blocks (enclosed by $$...$$), it should write the blocks as

```  math
a + b = c
``` 

The preces I have now is

In org-mode, the math blocks are simply the latex code:

\begin{equation}
a + b = c
\end{equation}

this is parsed as a pandoc AST RawBlock with format latex. I then remove the first (\begin{equation}) an last line (\end{equation}), and construct a pandoc CodeBlock with atrributes {"math"}, so the CodeBlock object displays in AST as

CodeBlock ("",["math"],[]) "a + b = c\n"

and then I let Pandoc create the markdown document, and the written result is

``` {.math}
a + b = c
``` 

The question:
I want the bare math, not {.math} written, without the use of CLI options.

I am aware that this can be done setting the Writer extension fenced_code_attributes to false (eg. $pandoc -w markdown-fenced_code_attributes ...), but I would much prefer this done inside the filter.

Or is it possible to set the extensions inside the filter?

Here is my atempted lua-filter:



function split(str,pat)
   local tbl = {}
   str:gsub(pat, function(x) tbl[#tbl+1]=x end)
   return tbl
end


function RawBlock(rb)
   if rb.format == "latex" then
      local text = rb.text
      split_text =  split(text, "[^\n]*")
      if split_text[1] == '\\begin{equation}'  and split_text[#split_text-1] == '\\end{equation}' then
         table.remove(split_text, #split_text-1)
         table.remove(split_text, 1)
         text = table.concat(split_text, "\n")
         local cb = pandoc.CodeBlock()
         cb.attr = {"",{"math"}}
         cb.text = text
         return cb
      end
   end
end

Solution

  • You could take full control of the output by creating the desired block yourself.

    E.g., instead of local cb = pandoc.CodeBlock() ff., you could write

    return pandoc.RawBlock('markdown',
      string.format('``` math\n%s\n```\n', text)
    )
    

    So you'd basically be creating the Markdown yourself, which is relatively safe in the case of code blocks (assuming that the math doesn't contain ```, which would be very unusual).

    As for the original question: enabling extensions or options in the filter is currently not possible.