2023-03-06 17:04:21 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2023-03-05 01:47:44 +00:00
|
|
|
class SWHInlineMacro < Asciidoctor::Extensions::InlineMacroProcessor
|
|
|
|
use_dsl
|
|
|
|
|
|
|
|
named :swh
|
|
|
|
name_positional_attributes 'caption'
|
|
|
|
|
2023-03-06 17:04:21 +00:00
|
|
|
def process(parent, target, attrs)
|
2023-03-05 01:47:44 +00:00
|
|
|
doc = parent.document
|
|
|
|
|
|
|
|
# We're only considering `swh:` starting with the scheme version. Also, it
|
|
|
|
# looks nice aesthetically.
|
2023-03-06 17:04:21 +00:00
|
|
|
swhid = target.start_with?('swh:') ? target : %(swh:#{target})
|
2023-03-05 01:47:44 +00:00
|
|
|
swhid_core_identifier = (swhid.split ';').at 0
|
|
|
|
|
|
|
|
text = attrs['caption'] || swhid_core_identifier
|
|
|
|
target = %(https://archive.softwareheritage.org/#{swhid})
|
|
|
|
|
|
|
|
doc.register :links, target
|
|
|
|
create_anchor parent, text, type: :link, target: target
|
|
|
|
end
|
|
|
|
end
|