通过pandoc lua过滤器添加元数据字段

我正在处理一个 markdown 文档,例如

---
标题:”假标题“
强调:”c“
突出显示:”突出显示的“
---
正文

转换为高度定制的 latex 模板,其中,“c”作为更大的 tex 宏的一部分被突出显示。最后一个步骤阻碍了我:假的元数据“highlighted”不应该在这里指定,但我找不到一种在下面的 lua 过滤器中生成它的方法:

text = require 'text'
newstring = '\\textit{a,b,c,d}'

function meta_vars (m)
       -- m.highlighted = 'highlighted' -- 不起作用
        highlight = m.highlight
  return m
end

function replace (elem)
      if elem.text == 'highlighted' then
        newstring = newstring:gsub(pandoc.utils.stringify(highlight),
                       '{\\bfseries '..pandoc.utils.stringify(highlight)..'}')
        return pandoc.RawInline("latex", newstring)
        else
        return elem
      end
    end

return {{Meta = meta_vars}, {Str = replace}}

使用以下自定义模板

\documentclass[12pt]{article}

\begin{document}

\title{$title$ -- $highlighted$}
\maketitle
$body$
\end{document}

运行此示例的命令是

pandoc +RTS -K512m -RTS test.utf8.md --to latex --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash --output test.tex --template _tpl.tex --lua-filter=filter.lua

生成,如广告

enter image description here

如何摆脱假的 highlighted 标签在我的 YAML 头?

点赞
用户4536527
用户4536527

我并不完全理解你在这里使用 LaTeX 宏的具体操作,但你的问题似乎是关于添加一个新的元数据元素。你已经标记了 --not working 的那一行对我来说似乎可以正常工作。你可能需要显式地设置类型,如下所示:

m.highlighted=pandoc.MetaString('highlighted')

但我认为这不会改变任何东西。

我还注意到,按照现有的写法,只要文本中出现单词 highlighted,newstring 变量就会被修改。如果有两行 highlighted,那么 newstring 就会等于 \textit{a,b,{\bfseries {\bfseries c}},d}

也许你可以重述你的问题,将 LaTeX 宏相关的操作删除,只聚焦于你想要筛选器实现的功能。

2019-10-24 16:01:42