aboutsummaryrefslogtreecommitdiff
path: root/shared/lib/CrossDocumentReferencesMacro/extension.rb
blob: e85efe67f3ea296dde179940fa6190c23c2b4a66 (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
require 'asciidoctor/extensions' unless RUBY_ENGINE == 'opal'

include ::Asciidoctor

class CrossDocumentReferencesMacro < Asciidoctor::Extensions::InlineMacroProcessor
  use_dsl

  # Macro to handle cross references to a different book.
  named :extref
  name_positional_attributes 'attributes'

  def process parent, target, attrs
    destination = target
    text = attrs[1]
    anchor = ""

    unless attrs[2].nil?
      anchor = "#" + attrs[2]
    end

    doc = parent.document

    if doc.attributes['isonline'] == "1"
      (create_anchor parent, text, type: :link, target: %(#{destination}#{anchor})).render
    else
      if doc.attributes['doctype'] == "book"
        (create_anchor parent, text, type: :link, target: %(../#{destination}/index.html#{anchor})).render
      else
        (create_anchor parent, text, type: :link, target: %(#{destination}/index.html#{anchor})).render
      end
    end

  end
end