summaryrefslogtreecommitdiff
path: root/docs/modules/extensions/pages/inline-macro-processor.adoc
blob: b16b4854421b7b35f04d2ea31bcaa5c10ccfd78b (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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
= Inline Macro Processor Extension Example
:navtitle: Inline Macro Processor

Purpose::
Create an inline macro named `man` that links to a manpage.

== sample-with-man-link.adoc

[source,asciidoc]
----
See man:gittutorial[7] to get started.
----

== ManpageInlineMacro

[source,ruby]
----
require 'asciidoctor'
require 'asciidoctor/extensions'

class ManInlineMacro < Asciidoctor::Extensions::InlineMacroProcessor
  use_dsl

  named :man
  name_positional_attributes 'volnum'

  def process parent, target, attrs
    text = manname = target
    suffix = ''
    target = %(#{manname}.html)
    suffix = if (volnum = attrs['volnum'])
      "(#{volnum})"
    else
      nil
    end
    parent.document.register :links, target
    %(#{(create_anchor parent, text, type: :link, target: target).convert}#{suffix})
  end
end
----

== Usage

[source,ruby]
----
Asciidoctor::Extensions.register do
  inline_macro ManInlineMacro
end

Asciidoctor.convert_file 'sample-with-man-link.adoc', safe: :safe
----