forked from asciidoctor/asciidoctor-extensions-lab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
license-url-docinfoprocessor.rb
36 lines (31 loc) · 997 Bytes
/
license-url-docinfoprocessor.rb
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
require 'asciidoctor/extensions' unless RUBY_ENGINE == 'opal'
include ::Asciidoctor
# A docinfo processor that emits an appropriate license URL into a document.
# Requires that the license attribute contain one or more http or https URL.
#
# Usage
#
# :license: http://opensource.org/licenses/MIT
#
Extensions.register do
docinfo_processor do
at_location :header
process do |doc|
next unless doc.attr? 'license'
backend = doc.backend
result = []
# Support dual licensing.
(doc.attr 'license').split(/\s+/).each do |url|
next unless url.start_with? 'http://', 'https://'
if backend == 'docbook5'
result << %(<dct:license xmlns:dct="http://purl.org/dc/terms/">#{url}</dct:license>)
elsif backend == 'html5'
result << %(<link rel="license" href="#{url}">)
elsif backend == 'xhtml5'
result << %(<link rel="license" href="#{url}" />)
end
end
result * EOL
end
end
end