Pandoc: Template with YAML metadata

邮差的信 提交于 2019-11-30 04:38:57

问题


I use pandoc for generate index.html with YAML metadata. I know iterate associative arrays from pandoc template:

YAML:

- Author: Mastropiero
- Author: Gunter Fraggen

TEMPLATE:

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

But... How to iterate lists without key?

YAML:

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

TEMPLATE:

$for(author)$
  ... // how works?
$endfor$

回答1:


As your template shows, within a loop pandoc makes a local variable with the same name as the array ('author' in your case). So to iterate through the inner list, simply use the same 'for' mechanism on the inner variable.

Thus, you should use

TEMPLATE

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

You can also use $sep$ to specify a separator to use between the elements of the list.

Note that if the inner list has elements with different meanings (rather than just a list) then you should use a list of dictionaries.

YAML

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

TEMPLATE

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


来源:https://stackoverflow.com/questions/26483499/pandoc-template-with-yaml-metadata

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!