Pandoc 模板:最后一项的不同分隔符

Fre*_*vje 5 templates pandoc

我正在编写一个自定义 Pandoc 模板,并希望它打印一个作者列表,以便所有作者都用“,”分隔,但最后一个作者用“和”分隔。这能做到吗?

假设这是我的模板:

$if(title)$
$title$
$endif$
$if(author)$
$for(author)$$author$$sep$, $endfor$
$endif$
$body$
Run Code Online (Sandbox Code Playgroud)

哪些输出:

My title
Author 1, Author 2, Author 3
Document body
Run Code Online (Sandbox Code Playgroud)

但我希望它输出:

My title
Author 1, Author 2 and Author 3
Document body
Run Code Online (Sandbox Code Playgroud)

Jan*_*tík 7

您必须使用 Pandoc 模板语法中所谓的“管道”(请参阅​​ https://pandoc.org/MANUAL.html)。

我们将为数组中除最后一项之外的所有项创建一个 for 循环(it被视为循环内的代词,引用来自 的值authors/allbutlast)。对于数组中的这些元素authors,我们将使用“,”作为分隔符,但在循环之外,我们只需放置“最终分隔符”(在本例中为“and”),以及authors数组的最后一个元素(~代表如果您要输出到 LaTeX,则需要获得牢不可破的空间)。

$for(authors/allbutlast)$$it$$sep$, $endfor$ and~$authors/last$
Run Code Online (Sandbox Code Playgroud)

编辑:如果只有一位作者,请使用如下内容:

$if(author/allbutlast)$
$for (author/allbutlast)$
${it}$sep$, 
$endfor$
and ${author/last}
$else$
${author/last}
$endif$
Run Code Online (Sandbox Code Playgroud)