如何在 Pandoc 过滤器中启用扩展?

我正在尝试制作一个过滤器,将一些 org-mode 的特性转换为 GitLab-markdown (Pandoc 默认情况下不支持),特别是数学块。

当转换为 markdown 时,该过滤器应该能够工作,但是它不应该按照 $$...$$ 括起来的 markdown 格式编写块,而应该将该块写成

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

我现在的过程是

在 org-mode 中,数学块只是 LaTeX 代码:

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

这被解析为 pandoc AST RawBlock,格式为 latex。然后我删除第一行(\begin{equation})和最后一行(\end{equation}),并构造一个 pandoc CodeBlock,其属性为 {"math"},因此 CodeBlock 对象在 AST 中显示为

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

然后我让 Pandoc 创建 markdown 文档,写出的结果是

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

问题:

我想要裸露的 math,而不是写有 {.math},而无需使用 CLI 选项。

我知道可以将 Writer 扩展 fenced_code_attributes 设置为 false 来实现这一点(例如 $pandoc -w markdown-fenced_code_attributes ...),但我更喜欢在过滤器内部完成这个操作。

还是有可能在过滤器内部设置扩展吗?

这是我尝试制作的 lua-filder:


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
点赞
用户2425163
用户2425163

你可以通过自己创建所需的块来完全控制输出。

比如,不是写local cb=pandoc.CodeBlock(),你可以写

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

因此,你基本上是自己创建 Markdown,这在代码块的情况下相对安全(假设数学公式不包含 ``` ,这非常不寻常)。

至于原始问题:目前无法在过滤器中启用扩展或选项。

2021-04-16 13:54:03