首页 > 解决方案 > 是否可以在 pandoc 过滤器上启用扩展?

问题描述

我正在尝试制作一个过滤器,将 org-mode 的某些功能转换为 GitLab-markdown(Pandoc 开箱即用不支持),特别是数学块。

过滤器在转换为 时应该起作用markdown,但不是为数学块(由 括起来$$...$$)提供降价格式,它应该将块写为

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

我现在的前言是

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

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

RawBlock这被解析为带有 format的 pandoc AST latex。然后我删除第一行 () 和\begin{equation}最后一行 ( \end{equation}),并构造一个CodeBlock带有 attributes 的 pandoc {"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过滤器:



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

标签: filterluagitlabmarkdownpandoc

解决方案


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

例如,代替local cb = pandoc.CodeBlock()ff.,你可以写

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

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

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


推荐阅读