filter - 如何访问 panfrude 中的全局变量(Pandoc 过滤器)

标签 filter pandoc

根据the API documentation , 人们应该能够通过 doc variable 访问全局变量不知何故。 但具体如何,没有解释。

全局变量可以在 YAML header 中定义:

---
title: Document Title
author: Who wrote this
key0: val0

Here starts the document content ...

或在 pandoc 命令行上:

pandoc -V key1=val1 --variable key2=val2 input.md

我试图访问它们:

    print("key1=%s" % doc.key1)
    print("key1=%s" % doc.get_metadata('key1', 'NOT-FOUND'))

两者都失败了。

最佳答案

第一种方法(将变量放入文档中)失败,因为 YAML block 未关闭,因此该 block 不会作为元数据读取,而是作为正常的 Markdown 读取。在元数据后面添加结束 --- 行应该可以解决此问题。

---
title: Document Title
author: Who wrote this
key0: val0
---

Here starts the document content ...

要了解命令行版本失败的原因,必须查看 pandoc 在模板变量元数据之间的区别。 doc for -M/--metadata说:

Like --variable, --metadata causes template variables to be set. But unlike --variable, --metadata affects the metadata of the underlying document (which is accessible from filters and may be printed in some output formats) and metadata values will be escaped when inserted into the template.

因此,使用 -M 而不是 -V 应该可以解决您的问题。

关于filter - 如何访问 panfrude 中的全局变量(Pandoc 过滤器),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57537916/

相关文章:

javascript - 如何取消选中 jquery 复选框并使剩余的复选框结果仍然可用?

wordpress - 如何对特定页面模板使用 add_filter ?

javascript - 过滤数字数组,其中 0 是有效输入

python - 从 Pandas 数据框中删除低频项

google-sheets - 如何过滤 View 两列 OR 而不是 AND?

markdown - 根据带有 Markdown/Pandoc 的项目符号使用特定的类

markdown - Markdown 中的签名行

latex - pandoc LaTex 改变`反引号高亮

markdown - 是否可以在 pandoc 中启用全局双间距以进行 Markdown 到 RTF 的转换?

r - 尝试渲染站点时 pandoc 错误