如何在 Rmd 的 YAML 元数据中创建三级对象?

How to create three level object in YAML metadata in Rmd?

我发现了一个很好的例子,如何在 YAML 元数据中创建两级对象 here。例如,为了包含多个作者

---
title:  'This is the title: it contains a colon'
author:
- name: Author One
  affiliation: University of Somewhere
- name: Author Two
  affiliation: University of Nowhere
---

然后在htmltemplate

$for(author)$
$if(author.name)$
$author.name$$if(author.affiliation)$ ($author.affiliation$)$endif$
$else$
$author$
$endif$
$endfor$

是否可以在YAML 中创建三级对象?例如,我希望同一机构有不止一位作者。因此,我的逻辑是

---
title:  'This is the title: it contains a colon'
author:
  - affiliation: 
     - name: Author One
       email: fake@fake.com
     - name: Author Two
       email: fake2@fake.com
  - affiliation: 
     - name: Author Three
       email: fake3@fake.com
---

但是没用。有可能做到吗?我知道它看起来没有任何意义,但它对我的 html template 结构来说是必要的。

感谢您的帮助!

更新

我还添加了一个我在 html template 中创建的块。

$if(author)$
$for(author)$
<div class = "author-section">
<div>$author.affiliation$</div>
<div class = "authors">
$for(author.affiliation)$
<div class = "author">
<div class = "mid-col">
$author.affiliation.name$
</div>
<div class = "right-col">
<a>$author.affiliation.email$</a>
</div>
</div>
$endfor$
</div>
</div>
$endfor$
$endif$

然后,它工作正常,除了 $author.affiliation$ 在我的 YAML 中没有值。因此,渲染后,我在最终的 .html 文件中得到 "true" 字符串。

然而,当我在 YAML 中添加一个用于确认的词时,例如

author:
  - affiliation: University of Somewhere
     - name: Author One
       email: fake@fake.com

渲染时出错

Error in yaml::yaml.load(string, ...) : 
  Scanner error: mapping values are not allowed in this context at line 9, column 19

就我个人而言,我会保留第一个示例中的原样。您可以为两个不同的作者写两次相同的单位名称。

但是,如果您真的想将 YAML 编写给 group/nest 大学作者,它看起来像这样(如果您熟悉 JSON 转换器,请尝试一些在线 YAML JSON):

title:  'This is the title: it contains a colon'
university:
  - label: University of Somehwere
    author:
     - name: Author One
       email: fake@fake.com
     - name: Author Two
       email: fake2@fake.com
  - label: University of Nohwere
    author:
     - name: Author Three
       email: fake3@fake.com

然后 pandoc 模板循环看起来像这样:

$for(university)$
<div>
  $university.label$
  $for(university.author)$
    $university.author.name$
    <span class="">$university.author.email$</span>
  $endfor$
</div>
$endfor$