website/gems/lib/asciidoctor/musicbrainz-link-inline-macro/extension.rb
Gabriel Arazas f7e2b3a2b7
Update Asciidoctor extension dependency
Asciidoctor already handles the caching so we don't really need to. On
the plus side, it gives some choice whether the pipeline should have a
cache or not.
2023-05-28 17:49:15 +08:00

48 lines
1.6 KiB
Ruby

# frozen_string_literal: true
require 'json'
require 'open-uri'
require 'uri'
class MusicBrainzLinkInlineMacro < Asciidoctor::Extensions::InlineMacroProcessor
use_dsl
named :musicbrainz
name_positional_attributes 'caption', 'type'
default_attributes 'type' => 'release'
def process(parent, target, attrs)
doc = parent.document
root_endpoint = 'https://musicbrainz.org/ws/2'
begin
headers = {
'Accept' => 'application/json',
'User-Agent' => ::Asciidoctor::FoodogsquaredCustomExtensions::USER_AGENT
}
uri = %(#{root_endpoint}/#{attrs['type']}/#{target})
if attrs['caption'].nil?
metadata = OpenURI.open_uri(uri, headers) { |f| JSON.parse(f.read) }
attrs['caption'] ||= case attrs['type']
when 'artist', 'area', 'events', 'genre', 'instrument', 'label', 'place', 'series'
metadata['name']
when 'recording', 'release-group', 'release', 'cdstub', 'work'
metadata['title']
when 'url'
metadata['resource']
end
end
target = %(https://musicbrainz.org/#{attrs['type']}/#{target})
doc.register :links, target
create_anchor parent, attrs['caption'], type: :link, target: target
rescue
warning = %(error while getting Musicbrainz database object '#{target}: #{e}')
warn_or_raise doc, warning
reader.push_include warning, target, target, 1, attrs
end
end
end