英文:
Boolean Operator in Pandoc Template
问题
Pandoc模板支持if
条件和for
循环。例如,
$if(foo)$
部分一
$else$
部分二
$endif$
如何在if
条件参数中进行布尔逻辑操作?例如,
$if(foo AND bar)$
都是
$endif$
和
$if(foo OR bar)$
都是
$endif$
英文:
Pandoc Template supports if
-clauses and for
-loops. For example,
$if(foo)$
part one
$else$
part two
$endif$
How to do boolean logic inside the if
-clause argument? For example,
$if(foo AND bar)$
both
$endif$
and
$if(foo OR bar)$
both
$endif$
答案1
得分: 1
The template language has no support for this. Boolean AND
can be simulated by using two if
s.
$-- foo AND bar
$if(foo)$
$if(bar)$
both
$endif$
$endif$
Boolean OR
is not really possible though; the best method would be to use a partial to avoid too much repetition:
$-- foo OR bar
$if(foo)$
$my.partial()$
$else$
$if(bar)$
$my.partial()$
$endif$
$endif$
It is often easier to move the calculations to a (Lua) filter for even mildly complicated logic.
function Meta (meta)
meta['foo-and-bar'] = meta.foo or meta.bar
return meta
end
Drawback: if foo
or bar
are not part of the metadata but of the set of variables, then this won't work, as filters don't have access to variables. Use a custom writer in that case.
英文:
The template language has no support for this. Boolean AND
can be simulated by using two if
s.
$-- foo AND bar
$if(foo)$
$if(bar)$
both
$endif$
$endif$
Boolean OR
is not really possible though; the best method would be to use a partial to avoid too much repetition:
$-- foo OR bar
$if(foo)$
$my.partial()$
$else$
$if(bar)$
$my.partial()$
$endif$
$endif$
It is often easier to move the calculations to a (Lua) filter for even mildly complicated logic.
function Meta (meta)
meta['foo-and-bar'] = meta.foo or meta.bar
return meta
end
Drawback: if foo
or bar
are not part of the metadata but of the set of variables, then this won't work, as filters don't have access to variables. Use a custom writer in that case.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论