website/gems/lib/asciidoctor/flathub-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

36 lines
1011 B
Ruby

# frozen_string_literal: true
require 'json'
require 'open-uri'
class FlathubLinkInlineMacro < Asciidoctor::Extensions::InlineMacroProcessor
use_dsl
named :flathub
name_positional_attributes 'caption'
def process(parent, target, attrs)
doc = parent.document
# FlatHub API seems to have no documentation aside from the source code.
# You can easily infer the API with its source code at
# https://github.com/flathub/website.
app_id = target
app_metadata_uri = %(https://flathub.org/api/v2/appstream/#{app_id})
headers = {
'Accept' => 'application/json',
'User-Agent' => ::Asciidoctor::FoodogsquaredCustomExtensions::USER_AGENT
}
if attrs['caption'].nil?
metadata = OpenURI.open_uri(app_metadata_uri, headers) { |f| JSON.parse(f.read) }
attrs['caption'] = metadata['name']
end
url = %(https://flathub.org/apps/#{app_id})
doc.register :links, url
create_anchor parent, attrs['caption'], type: :link, target: url
end
end