summaryrefslogtreecommitdiff
path: root/docs/modules/extensions/pages/include-processor.adoc
blob: cf627562d3f939aca57c421ae6a7a2e618dfdf9b (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
= Include Processor Extension Example
:navtitle: Include Processor

Purpose::
Include a file from a URI.

TIP: Asciidoctor supports including content from a URI out of the box if you set the `allow-uri-read` attribute (not available if the safe mode is `secure`).

== sample-with-uri-include.adoc

[source,asciidoc]
....
:source-highlighter: coderay

.Gemfile
[source,ruby]
----
\include::https://cdn.jsdelivr.net/gh/asciidoctor/asciidoctor/Gemfile[]
----
....

== UriIncludeProcessor

[source,ruby]
----
class UriIncludeProcessor < Asciidoctor::Extensions::IncludeProcessor
  def handles? target
    target.start_with? 'https://', 'https://'
  end

  def process doc, reader, target, attributes
    content = (::OpenURI.open_uri target).readlines
    reader.push_include content, target, target, 1, attributes
    reader
  end
end
----

== Usage

[source,ruby]
----
Asciidoctor::Extensions.register do
  include_processor UriIncludeProcessor
end

Asciidoctor.convert_file 'sample-with-uri-include.adoc', safe: :safe
----