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

查看:166
本文介绍了为使用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用YAML元数据中声明的信息替换占位符"(即key-onekey-two等)?这将导致:

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

在《用户指南》上显示:

On the User's Guide, it says:

如果未设置变量,则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天全站免登陆