Pandoc:带有YAML元数据的模板

10

我使用pandoc生成带有YAML元数据的index.html。我知道如何从pandoc模板中迭代关联数组:

YAML

- Author: Mastropiero
- Author: Gunter Fraggen

模板:

$for(author)$
  $author$
$endfor$

但是...如何在没有键的情况下迭代列表?

YAML:

- Author:
  - [Value1, Value2]
  - [Value1B, Value2B]

模板:

$for(author)$
  ... // how works?
$endfor$
1个回答

19

正如您的模板所示,在循环内,pandoc会创建一个与数组同名的局部变量(在您的情况下为“author”)。因此,要遍历内部列表,只需在内部变量上使用相同的“for”机制即可。

因此,您应该使用

TEMPLATE

$for(author)$
   $for(author)$
      $author$
   $endfor$
$endfor

您还可以使用$sep$来指定列表元素之间要使用的分隔符。

请注意,如果内部列表有具有不同含义的元素(而不仅仅是一个列表),则应该使用字典列表。

YAML

Author:
  - {name: Iain Banks, book: The Algebraist}
  - {name: Isaac Asimov, book: Foundation} 

模板

$for(author)$
    $author.name$ wrote $author.book$
$endfor$

2
非常有帮助!您能否提供一个如何指定$sep$的示例? - cboettig
1
只需在循环内添加 $sep$separator。例如,separator 可以是 \\\ - bytesinflight

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接