0

设置

我使用以下页脚导出 PDF:

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

问题:

我想同时增加page-numberpage-countpage-offset

到目前为止我已经尝试过:

我找到了一个可能相关的讨论并尝试了类似的东西

{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才能使用它,但我该怎么做呢?我找到了第二个可能相关的线程,但是我会在哪里放置这样的宏?

更新:

我找到了“数学表达式和函数”部分,它似乎只适用于变量。所以我在总结之前尝试将page-number和转换为变量:page-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”...

所以基本上这个问题是:如何进行数学运算page-number和/或如何将其转换为数字?

4

1 回答 1

1

正如此评论中所建议的,我添加了一个内联宏。关于如何注册宏(和/或扩展,就此而言),有很好的记录,但不完全是在哪里注册它们。所以我们开始:

在 中包含扩展文件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:[][]

没有找到更优雅的解决方案[][],但我可以接受。

于 2020-08-25T10:27:04.707 回答