使用 YAML 元数据块为 pandoc 转换声明任意变量 [英] declaring arbitrary variables for pandoc conversion using YAML metadata block

查看:27
本文介绍了使用 YAML 元数据块为 pandoc 转换声明任意变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近才发现 Pandoc,所以我仍然习惯了它的许多功能.它看起来像是一个非常有用的工具,我很高兴能找到它的一些应用程序.我一直在查阅用户指南,虽然有 一节我想要知道,我似乎无法获得所需的输出.我不确定我是否正确阅读了该条目.

I've only recently discovered Pandoc, so I'm still getting to used to it a lot of its features. It looks like an incredibly useful tool and I'm excited to find out some applications for it. I've been consulting the User's Guide, and while there is a section on what I'd like to know, I can't seem to get the desired output. I'm not sure if I'm reading the entry correctly.

简单地说,我在 .markdown 中有一个文档,它充当模板.通过这个模板,我想生成几个其他文档(可能是 .odt.docx).除了我想更改的几条信息之外,这些文档将大部分相同.我想知道的是,是否可以通过在文档顶部的 YAML 元数据中声明一个变量来更改这些信息.

Put simply, I have a document in .markdown which acts as a template. From this template, I'd like to produce several other documents (probably .odt and .docx). These documents will be mostly identical, apart from a few pieces of information which I'd like to change. What I'd like to know is, is it possible to change these pieces of information by declaring a variable in the YAML metadata at the top of document.

例如,假设我的 .markdown 模板中有以下内容:

For example, say I had the following in my .markdown template:

---
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 用声明的信息替换占位符",即 key-onekey-two 等在 YAML 元数据中?这将导致:

Is there a way that I can get pandoc to replace the 'placeholders' i.e. key-one, key-two, etc., with the information that is declared in the YAML metadata? This would result in:

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

在用户指南上,它说:

如果未设置变量,pandoc 将在文档的元数据中查找键 - 可以使用 YAML 元数据块或使用 --metadata 选项进行设置.

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 模板 的信息,但我不确定我应该怎么做编辑这些(或创建自定义)以获得所需的输出.

From what I can find on the User's Guide, it seems that I can't declare values arbitrarily in the metadata. I have found some information about Pandoc templates, but I'm not sure how I would have to edit these (or create a custom one) to get the desired output.

如果有任何不清楚的地方,请告诉我,我会尽量说得更具体.提前致谢.

Please let me know if anything isn't clear and I will try to be more specific. Thanks in advance.

推荐答案

Pandoc 模板仅包含文档正文周围的页眉/页脚等,它们放置在您看到 $body$ 的位置模板.因此模板不能用于替换文档正文中的变量.

Pandoc templates only contain the header/footer etc. around the document body text, which gets placed where you see $body$ in the template. So templates cannot be used to substitute variables in the document body.

为此,您可以使用 这个 pandoc 过滤器,将其保存到说 meta-vars.lua:

For that, you could use this pandoc filter, save it to say 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

这篇关于使用 YAML 元数据块为 pandoc 转换声明任意变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆