使用胡子语法的pandoc变量替换过滤器
pandoc-mustache的Python项目详细描述
Date: | May 11, 2019 |
---|
内容
pandoc-mustache: Variable Substitution in Pandoc
pandoc mustache过滤器允许您将变量放入 pandoc文档文本,其值存储在单独的文件中。什么时候? 运行pandoc变量将替换为其值。
技术说明:此pandoc筛选器不是 这是Mustache template spec。只有 支持变量替换:不支持其他tag types 当前支持。
Example
此文档,在document.md:
--- mustache: ./le_gaps.yaml --- The richest American men live {{diff_le_richpoor_men}} years longer than the poorest men, while the richest American women live {{diff_le_richpoor_women}} years longer than the poorest women.
结合这些变量定义,在le_gaps.yaml:
diff_le_richpoor_men:"14.6"diff_le_richpoor_women:"10.1"
将由pandoc document.md --filterpandoc-mustache转换为:
The richest American men live 14.6 years longer than the poorest men, while the richest American women live 10.1 years longer than the poorest women.
Usage
在pandoc文档中,通过将 双“胡子”中的变量名,即花括号,如 {{this}}。
变量在一个或多个单独的文件中定义,使用yaml 格式化的键值对。例如:
place:Montrealtemperature:'7'
包含胡子变量的pandoc文档指向 包含变量定义的yaml文件。这些 文件使用YAML metadata block中的胡子字段来指示,通常 放在pandoc文档的顶部。绝对路径和相对路径 支持路径:相对路径是相对于 运行pandoc的工作目录。
例如:
---title:My Reportauthor:Jane Smithmustache:./vars.yaml---The temperature in {{place}} was {{temperature}} degrees.
或者,使用多个文件:
---title:My Reportauthor:Jane Smithmustache:-./vars.yaml-./additional_vars.yaml---The temperature in {{place}} was {{temperature}} degrees.The humidity was {{humidity}}%.
运行pandoc并将文档中的所有变量替换为 通过将--filterpandoc-mustache添加到pandoc命令的值。
Tips and Tricks
在yaml中定义变量时,不需要将字符串括起来 引述。但是如果你想用引号把数字括起来 以完全相同的格式出现在文档中。一些 示例:
unquoted_string:Montreal # becomes:Montrealquoted_string:'Montreal'# becomes: Montrealtrailingzero_num:7.40 # becomes:7.4trailingzero_string:'7.40'# becomes: 7.40
如果你在写一份报告计算结果的文件, 您可以编写代码(使用r、python、stata等语言)来编写 每次生成数字时,都会自动将其发送到yaml文件。由 使用变量而不是 硬编码到文本中,文档可以立即更新 如果结果改变。你可以确定 输出文档反映分析的最新结果。
Contributing
此代码尚未积极开发。它是为了满足我 pandoc的写作需要,目前的功能集对我来说已经足够了。
如果有bug报告,则可以创建问题或提交pull 请求。如果时间允许,我会调查的。
如果您有一个feature请求,那么我不太可能 为你实现它。您可以创建问题以生成讨论。如果 如果你实现了一个特性,你可以提交拉取请求,我会检查它 最后,如果时间允许。如果你对专业感兴趣 添加到代码中,我很高兴欢迎新的维护人员来到 项目。
License
此存储库中的所有文件都发布到公共域 在CC0 license到 允许尽可能广泛的重复使用。
Acknowledgements
这个pandoc过滤器是用sergio correia的 panflute包。这个 panflute存储库也 作为组织这个知识库的灵感。