包含处理器扩展示例

目的

从 URI 包含文件。

如果设置了 allow-uri-read 属性,Asciidoctor 开箱即支持从 URI 包含内容(如果安全模式为 secure,则不可用)。

sample-with-uri-include.adoc

:source-highlighter: coderay

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

UriIncludeProcessor

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

用法

Asciidoctor::Extensions.register do
  include_processor UriIncludeProcessor
end

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