website/gems/lib/asciidoctor/gitlab-link-inline-macro/extension.rb

27 lines
637 B
Ruby
Raw Normal View History

2023-03-06 17:04:21 +00:00
# frozen_string_literal: true
2023-03-05 06:57:10 +00:00
require 'uri'
class GitLabLinkInlineMacro < Asciidoctor::Extensions::InlineMacroProcessor
use_dsl
named :gitlab
name_positional_attributes 'caption'
default_attributes 'domain' => 'gitlab.com'
2023-03-06 17:04:21 +00:00
def process(parent, target, attrs)
2023-03-05 06:57:10 +00:00
doc = parent.document
text = attrs['caption'] || target
uri = URI.parse %(https://#{attrs['domain']}/#{target})
uri.path += %(/-/tree/#{attrs['rev']}) if attrs['rev']
uri.path += %(/#{attrs['path']}) if attrs['path']
target = uri.to_s
doc.register :links, target
create_anchor parent, text, type: :link, target: target
end
end