我使用Pandoc进行技术报告,并修改了Python pandocfilters包示例metavars.py以提供变量替换。 Markdown中的%{test}
将替换为YAML元数据中test
的值。效果很好!
它适用于单行和"块"风格YAML。
我想做同样的事情,而不是放入Markdown格式化表中的文本块。这是我的最小测试文档(example.md
):
---
test: |-
| a |
|---|
| 1 |
block: |-
This Markdown _formatted_ a block \
of YAML \
text.
...
After this paragraph you should see a block:
%{block}
After this paragraph you should see a table:
%{test}
您可以在此gist看到我修改后的metavars.py
。
如果您注释掉%{test}
文字并运行它,您会看到正确的输出:
$ pandoc -t json example.md | ./metavars.py | pandoc -f json -t plain
After this paragraph you should see a block:
This Markdown _formatted_ a block
of YAML
text.
After this paragraph you should see a table:
当我取消注释%{test}
时,我最终会出现错误并且没有回溯:
$ pandoc -t json example.md | ./metavars.py | pandoc -f json -t plain
test -> [[], [{u'c': [], u't': u'AlignDefault'}], [0.0], [[{u'c':
[{u'c': u'a', u't': u'Str'}], u't': u'Plain'}]], [[[{u'c':
[{u'c': u'1', u't': u'Str'}], u't': u'Plain'}]]]]
pandoc: Error in $[3][0]: expected Object, encountered Array
我被困在这里!我的假设是我构建了一个无效的Pandoc AST的有效JSON文档。我无法找到关于Pandoc AST的文档,当我比较我生成的手动构建的Markdown转换为AST时,我没有看到差异。
这就是我想要得到的结果:
$ pandoc -t json example.md | ./metavars.py | pandoc -f json -t plain
After this paragraph you should see a block:
This Markdown _formatted_ a block
of YAML
text.
After this paragraph you should see a table:
a
---
1
我非常接近,从差异基础来看:
有关将我的表插入Para中的内容的任何建议吗?我希望我没有陷入无法做到这一点的境地。
答案 0 :(得分:0)
受到@ mb21对我的问题的评论的启发:
表元素是AST中的Para元素的对等元素,因此禁止在Para中嵌套Table。解决此问题的最佳方法是在AST中使用 up 级别。对于AST扫描中的每个Para元素,看它是否包含嵌套元素,然后该元素包含一个将被Table替换的变量(例如%{test}
)。如果是,则用表替换整个Para元素。
以下是metavars.py的更新版本,可按预期工作。
再次......感谢@ mb21的帮助。