asciidoctor-foodogsquared-e.../lib/asciidoctor/swhid-inline-macro/extension.rb

27 lines
798 B
Ruby
Raw Normal View History

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})
default_caption = if attrs.key? 'full-option'
swhid
else
swhid.split(';').at(0)
end
text = attrs['caption'] || default_caption
2023-03-05 01:47:44 +00:00
target = %(https://archive.softwareheritage.org/#{swhid})
doc.register :links, target
create_anchor parent, text, type: :link, target: target
end
end