使用 Pandoc 的 JSON AST 在段落中插入 table 的正确语法是什么?

What is the correct syntax to insert a table inside a paragraph using Pandoc's JSON AST?

我使用 Pandoc 进行技术报告并修改了 Python pandocfilters 包示例 metavars.py 以提供变量替换。 Markdown 中的 %{test} 被 YAML 元数据中的 test 值替换。效果很好!

它适用于单行和 "block" 风格的 YAML。

我想做同样的事情,但不是将文本块放入 Markdown 格式 table 中。这是我的最小测试文档 (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

我被困在这里了!我的假设是我已经构建了一个有效的 JSON 文档,它是无效的 Pandoc AST。我找不到关于 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

我真的很接近,从不同的角度来看:

有什么建议可以让我的 Table 插入到 Para 中吗?我希望我不会陷入无法做到的境地。

受@mb21 对我的问题的评论启发:

Table 元素是 AST 中 Para 元素的对等元素,因此禁止在 Para 中嵌套 Table。解决此问题的最佳方法是 提高 AST 水平。对于 AST 扫描中的每个 Para 元素,查看它是否包含一个嵌套元素,然后包含一个变量(例如 %{test}),该变量将被 Table 替换。如果是,则将整个 Para 元素替换为 Table.

这是 metavars.py 的更新版本,可以按预期工作。

再次...感谢@mb21 的帮助。