asciidoctor-foodogsquared-e.../lib/asciidoctor/github-link-inline-macro/extension.rb

30 lines
683 B
Ruby
Raw Normal View History

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