Pandoc Lua 过滤器:如何在函数 Str 中检查参数是否来自章节或节的标题

实际上,我在 markdown 转换为 LaTeX 的过程中进行了一些过滤。我使用 lua 过滤器来完成这个任务。现在我需要做这样的事情:

function Str (el)
  if is_in_a_title(el) then
    -- 做这个
  else
    -- 做那个
  end
end

也就是说,我想在 Str 中检查参数是否属于章节或节的标题。有没有办法做到这一点?

点赞
用户2425163
用户2425163

将我在邮件列表中写的内容重新发布:

这可以通过运行子过滤器来完成:

local header_filter = {
  Str = function (el)
    -- do this
  end
}

function Header (h)
  return pandoc.util.walk_block(h, header_filter)
end

header_filter 就像普通的 Lua 过滤器一样。 walk_block 函数将该过滤器应用于标题下面的所有元素,仅对这些元素进行过滤。


更近期的 pandoc 版本允许在块上使用 walk 方法;对于这些版本,上述内容可以编写为

function Header (h)
  return h:walk {
    Str = function (el)
      -- do this
    end
  }
end
2020-06-17 06:55:43