问题描述
设置
我导出具有以下页脚的PDF:
footer:
height: 0.75in
line_height: 1
recto_content:
right: '{page-number}/{page-count}'
问题:
我想将page-number
和page-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-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
进行数学运算和/或如何将其转换为数字?
解决方法
如this comment中的建议,我添加了一个内联宏。关于如何注册宏(和/或扩展名)的文献已经很好地记录了下来,但是在何处却没有确切地注册它们。所以我们开始:
在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:[][]
找不到[][]
的更优雅的解决方案,但是我可以接受。