根据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/