asciidoc:如何使用页码等进行数学运算?

asciidoc: How to operate mathematically with page-number et al?

设置

我导出带有以下页脚的 PDF:

footer:
  height: 0.75in
  line_height: 1
  recto_content:
    right: '{page-number}/{page-count}'

问题:

我想将 page-numberpage-count 都增加 page-offset

到目前为止我尝试过的:

我找到了 a possibly related discussion 并尝试了

{page-offset} // works, so page-offset is known here

{calc:page-number + page-offset} // might be not working due to "-" vs. "+", so:

{calc:{page-number} + {page-offset}} // just replaces vars: "{calc:1 + 42}"

:pagenum: calc:[{page-number} + {page-offset}]
recto_content:
  right: '{pagenum}' // no output at all

所以我想我需要先实现 calc 才能使用它,但我该怎么做呢?我找到了a second possibly related thread,但是我应该把这样的宏放在哪里呢?

更新:

我找到了 "Math Expressions & Functions"-section,它似乎只适用于变量。所以我尝试将 page-numberpage-offset 转换为变量,然后再对它们求和:

footer:
  foo:
    a: '{page-number}'
    b: '{page-offset}'
    bar: $footer_foo_a + $footer_foo_b
  height: 0.75in
  line_height: 1
  recto_content:
    right: $footer_foo_bar

但是它们被当作字符串对待;渲染输出为“1 + 42”...

所以基本上这个问题是:How to operate with page-number and/or how to convert it into a number?

按照 this comment 中的建议,我添加了一个内联宏。关于如何注册宏(就此而言,and/or 扩展名)有很好的记录,但不完全是 哪里 来注册它们。所以我们开始吧:

build.gradle中包含扩展文件,如果需要则传递偏移量:

asciidoctor {
  attributes 'some-x': 'x',
        'some-y': 'y',
        'page-offset': System.getProperty('pageOffset', '0')

  requires = ['./src/docs/asciidoc/lib/pagenum-inline-macro.rb']

  // ...
}

src/docs/asciidoc/lib/pagenum-inline-macro.rb 中注册了分机:

RUBY_ENGINE == 'opal' ? (require 'pagenum-inline-macro/extension') : (require_relative 'pagenum-inline-macro/extension')

Asciidoctor::Extensions.register do
  if @document.basebackend? 'html'
    inline_macro PagenumInlineMacro
  end
end

最后但同样重要的是,实际功能在 src/docs/asciidoc/lib/pagenum-inline-macro/extension.rb:

require 'asciidoctor/extensions' unless RUBY_ENGINE == 'opal'

include Asciidoctor

class PagenumInlineMacro < Extensions::InlineMacroProcessor
  use_dsl
  named :pagenum

  def process parent, target, attributes
    doc = parent.document
    page_offset = (doc.attr 'page-offset', 0).to_i
    page_num = (doc.attr 'page-number', 0).to_i + page_offset
    page_total = (doc.attr 'page-count', 0).to_i + page_offset
    %(#{page_num}/#{page_total})
  end
end

我在 theme.yml:

中使用它
footer:
  height: 0.75in
  columns: <25% =50% >25%
  recto:
    right:
      content: pagenum:[][]

没有为 [][] 找到更优雅的解决方案,但我同意。