布尔运算符在Pandoc模板中

huangapple go评论58阅读模式
英文:

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 ifs.

$-- 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 ifs.

$-- 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.

huangapple
  • 本文由 发表于 2023年6月29日 15:28:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76578891.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定