Class: Asciidoctor::Godoc::InlineMacro

Inherits:
Extensions::InlineMacroProcessor
  • Object
show all
Defined in:
lib/asciidoctor/godoc/inline_macro.rb

Overview

Provides the ‘godoc’ inline macro. This macro allows asciidoc documents to reference Go packages, types, functions, and methods with fully-qualified import paths, and generates links to their reference documentation.

Instance Method Summary collapse

Instance Method Details

#generate_text(parent, importpath, id) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/asciidoctor/godoc/inline_macro.rb', line 26

def generate_text(parent, importpath, id)
  # For just import paths,
  # use the import path as-is.
  return importpath unless id

  if id.split(".").count > 1
    # If the target is a method or a struct,
    # use $parent.$target.
    text = id
  else
    # Otherwise use $pkg.$target.
    pkg = pkgmap(parent.document.attributes)[importpath]
    pkg ||= importpath.split("/")[-1]
    text = "#{pkg}.#{id}"
  end

  node = create_inline parent, :quoted, text, { type: :monospaced }
  node.convert
end

#pkgmap(attrs) ⇒ Object



19
20
21
22
23
24
# File 'lib/asciidoctor/godoc/inline_macro.rb', line 19

def pkgmap(attrs)
  pkgs = attrs["gopkgs"]
  return {} unless pkgs

  pkgs.split(/\s*;\s*/).to_h { |e| e.split(/\s*=\s*/) }
end

#process(parent, target, attrs) ⇒ Object



46
47
48
49
50
51
52
53
54
55
# File 'lib/asciidoctor/godoc/inline_macro.rb', line 46

def process(parent, target, attrs)
  importpath, id = target.split("#", 2)

  text = attrs["text"] || generate_text(parent, importpath, id)
  target = %(https://pkg.go.dev/#{importpath})
  target << "##{id}" if id

  parent.document.register :links, target
  (create_anchor parent, text, type: :link, target: target).convert.to_s
end