asciidocasciidoctorasciidoctor-pdf

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


Setup

I export a PDF with the following footer:

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

Question:

I want to increase both page-number and page-count by page-offset.

What I've tried so far:

I found a possibly related discussion and tried stuff like

{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

So I guess I need to implement calc before I can use it, but how do I do so? I found a second possibly related thread, but where would I put such a macro?

UPDATE:

I found the "Math Expressions & Functions"-section, which seems to work with variables only. So I tried to convert page-number and page-offset into variables before summing them up:

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

But they are treated as the Strings they are; rendered output is "1 + 42"...

So basically this question is: How to operate mathematically with page-number and/or how to convert it into a number?


Solution

  • As suggested in this comment I added an inline macro. It's kind of well documented how to register macros (and/or extensions, for that matter), but not exactly where to register them. So here we go:

    Include the extension file in build.gradle, pass the offset if required:

    asciidoctor {
      attributes 'some-x': 'x',
            'some-y': 'y',
            'page-offset': System.getProperty('pageOffset', '0')
    
      requires = ['./src/docs/asciidoc/lib/pagenum-inline-macro.rb']
    
      // ...
    }
    

    in src/docs/asciidoc/lib/pagenum-inline-macro.rb the extension is registered:

    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
    

    and, last but nor least, the actual functionality is in 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
    

    I use it in my theme.yml:

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

    Didn't find a more elegant solution for [][], but I'm ok with that.