使用 YAML 元数据块声明用于 pandoc 转换的任意变量
declaring arbitrary variables for pandoc conversion using YAML metadata block
我最近才发现 Pandoc,所以我仍然习惯了它的许多功能。它看起来像一个非常有用的工具,我很高兴能找到它的一些应用程序。我一直在查阅用户指南,虽然有 a section on what I'd like to know,但我似乎无法获得所需的输出。我不确定我是否正确阅读了条目。
简而言之,我在 .markdown
中有一个文档作为模板。从这个模板中,我想生成其他几个文档(可能是 .odt
和 .docx
)。除了我想更改的几条信息外,这些文件大部分是相同的。我想知道的是,是否可以通过在文档顶部的 YAML 元数据中声明一个变量来更改这些信息。
例如,假设我的 .markdown
模板中有以下内容:
---
key-one: "value-one"
key-two: "value-two"
key-three: "value-three"
---
# DocumentTitle
## DocumentSubtitle
This line contains the first value, called $key-one$
This line contains the second value, called $key-two$
This line contains the third value, called $key-three$
有没有办法让 pandoc 用 YAML 元数据中声明的信息替换 'placeholders',即 key-one
、key-two
等?这将导致:
This line contains the first value, called value-one
This line contains the second value, called value-two
This line contains the third value, called value-three
用户指南上写着:
If a variable is not set, pandoc will look for the key in the document’s metadata – which can be set using either YAML metadata blocks or with the --metadata option.
根据我在用户指南中找到的内容,我似乎无法在元数据中任意声明值。我找到了一些关于 Pandoc templates 的信息,但我不确定如何编辑这些信息(或创建自定义信息)以获得所需的输出。
如果有任何不清楚的地方,请告诉我,我会尽量说得更具体。提前致谢。
Pandoc 模板仅包含文档正文文本周围的 header/footer 等,它们位于您在模板中看到 $body$
的位置。所以模板不能用于替换文档主体中的变量。
为此,您可以使用 this pandoc filter,将其保存为 meta-vars.lua
:
local vars = {}
function get_vars (meta)
for k, v in pairs(meta) do
if v.t == 'MetaInlines' then
vars["$" .. k .. "$"] = {table.unpack(v)}
end
end
end
function replace (el)
if vars[el.text] then
return pandoc.Span(vars[el.text])
else
return el
end
end
return {{Meta = get_vars}, {Str = replace}}
并用 pandoc input.md --lua-filter meta-vars.lua
调用它
我最近才发现 Pandoc,所以我仍然习惯了它的许多功能。它看起来像一个非常有用的工具,我很高兴能找到它的一些应用程序。我一直在查阅用户指南,虽然有 a section on what I'd like to know,但我似乎无法获得所需的输出。我不确定我是否正确阅读了条目。
简而言之,我在 .markdown
中有一个文档作为模板。从这个模板中,我想生成其他几个文档(可能是 .odt
和 .docx
)。除了我想更改的几条信息外,这些文件大部分是相同的。我想知道的是,是否可以通过在文档顶部的 YAML 元数据中声明一个变量来更改这些信息。
例如,假设我的 .markdown
模板中有以下内容:
---
key-one: "value-one"
key-two: "value-two"
key-three: "value-three"
---
# DocumentTitle
## DocumentSubtitle
This line contains the first value, called $key-one$
This line contains the second value, called $key-two$
This line contains the third value, called $key-three$
有没有办法让 pandoc 用 YAML 元数据中声明的信息替换 'placeholders',即 key-one
、key-two
等?这将导致:
This line contains the first value, called value-one
This line contains the second value, called value-two
This line contains the third value, called value-three
用户指南上写着:
If a variable is not set, pandoc will look for the key in the document’s metadata – which can be set using either YAML metadata blocks or with the --metadata option.
根据我在用户指南中找到的内容,我似乎无法在元数据中任意声明值。我找到了一些关于 Pandoc templates 的信息,但我不确定如何编辑这些信息(或创建自定义信息)以获得所需的输出。
如果有任何不清楚的地方,请告诉我,我会尽量说得更具体。提前致谢。
Pandoc 模板仅包含文档正文文本周围的 header/footer 等,它们位于您在模板中看到 $body$
的位置。所以模板不能用于替换文档主体中的变量。
为此,您可以使用 this pandoc filter,将其保存为 meta-vars.lua
:
local vars = {}
function get_vars (meta)
for k, v in pairs(meta) do
if v.t == 'MetaInlines' then
vars["$" .. k .. "$"] = {table.unpack(v)}
end
end
end
function replace (el)
if vars[el.text] then
return pandoc.Span(vars[el.text])
else
return el
end
end
return {{Meta = get_vars}, {Str = replace}}
并用 pandoc input.md --lua-filter meta-vars.lua