summaryrefslogtreecommitdiff
path: root/docs/modules/extend/examples/inline-pageref-macro.rb
blob: 651c3d91db145fac1720081ee1212012f27ba94e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
Asciidoctor::Extensions.register do
  inline_macro :pageref do
    process do |parent, refid|
      doc = (doc = parent.document).nested? ? doc.parent_document : doc
      if (ref = doc.catalog[:refs][refid])
        section = (closest ref, :section) || doc
        unless (physical_pagenum = section.attr 'pdf-page-start')
          doc.instance_variable_set :@pass, 1 unless (doc.instance_variable_get :@pass) == 2
          next create_inline parent, :quoted, '00' # reserve space for real page number
        end
        attributes = { 'refid' => refid, 'fragment' => refid, 'path' => nil }
        create_anchor parent, %(#{physical_pagenum + 1 - (start_page_number doc)}), { type: :xref, attributes: attributes }
      else
        create_inline parent, :quoted, '???'
      end
    end

    def closest node, context
      node.context == context ? node : ((parent = node.parent) && (closest parent, context))
    end

    def start_page_number doc
      doc.converter.index.start_page_number
    end
  end

  postprocessor do
    process do |doc|
      if (doc.instance_variable_get :@pass) == 1
        doc.instance_variable_set :@pass, 2
        doc.convert # WARNING: this may have side effects
      end
      doc.converter
    end
  end
end