summaryrefslogtreecommitdiff
path: root/docs/modules/extensions/pages/block-macro-processor.adoc
blob: af3e6ea4760bcd3e7d4f8d0b3acee86b2535b892 (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
= Block Macro Processor Extension Example
:navtitle: Block Macro Processor

Purpose::
Create a block macro named `gist` for embedding a gist.

== sample-with-gist-macro.adoc

[,asciidoc]
----
.My Gist
gist::123456[]
----

== GistBlockMacro

[,ruby]
----
class GistBlockMacro < Asciidoctor::Extensions::BlockMacroProcessor
  use_dsl

  named :gist

  def process parent, target, attrs
    title_html = (attrs.has_key? 'title') ?
        %(<div class="title">#{attrs['title']}</div>\n) : nil

    html = %(<div class="openblock gist">
#{title_html}<div class="content">
<script src="https://gist.github.com/#{target}.js"></script>
</div>
</div>)

    create_pass_block parent, html, attrs, subs: nil
  end
end
----

== Usage

[,ruby]
----
Asciidoctor::Extensions.register do
  block_macro GistBlockMacro if document.basebackend? 'html'
end

Asciidoctor.convert_file 'sample-with-gist.adoc', safe: :safe
----