如何访问 panflute 中的全局变量(Pandoc 过滤器)
How to access global variables in panflute (Pandoc filter)
根据the API documentation,
人们应该能够以某种方式通过 doc
variable 访问全局变量。
但是,没有解释。
全局变量可以在YAML中定义headers:
---
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 块未关闭,因此该块未作为元数据读取,而是作为普通 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
应该可以解决您的问题。
根据the API documentation,
人们应该能够以某种方式通过 doc
variable 访问全局变量。
但是,没有解释。
全局变量可以在YAML中定义headers:
---
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 块未关闭,因此该块未作为元数据读取,而是作为普通 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
应该可以解决您的问题。