diff --git a/gems/lib/asciidoctor/flathub-link-inline-macro/README.adoc b/gems/lib/asciidoctor/flathub-link-inline-macro/README.adoc new file mode 100644 index 0000000..58b86e1 --- /dev/null +++ b/gems/lib/asciidoctor/flathub-link-inline-macro/README.adoc @@ -0,0 +1,28 @@ += Flathub link inline macro +:toc: + + +A shorthand for linking applications from link:https://flathub.org[FlatHub]. + + +== Synopsis + +[source, asciidoc] +---- +flathub:$APP_ID[$CAPTION] +---- + +Where... + +- `$APP_ID` is the application ID of the program on Flathub. + +- `$CAPTION` is the link text to be used. +By default, it will get the appstream metadata from FlatHub and use the display name. +Take note it creates an additional network request to get the name. + + +== Example usage + +- `flathub:org.gnome.design.IconLibrary[]` links to the link:https://flathub.org/apps/org.gnome.design.IconLibrary[FlatHub page of GNOME Icon Library] with `Icon Library` as the link text. + +- `flathub:org.gnome.design.IconLibrary[GNOME Icon Library]` is the same as the previous list item but with its link text replaced with `GNOME Icon Library`. diff --git a/gems/lib/asciidoctor/flathub-link-inline-macro/extension.rb b/gems/lib/asciidoctor/flathub-link-inline-macro/extension.rb new file mode 100644 index 0000000..961b144 --- /dev/null +++ b/gems/lib/asciidoctor/flathub-link-inline-macro/extension.rb @@ -0,0 +1,36 @@ +# frozen_string_literal: true + +require 'json' +require 'open-uri' +require 'open-uri/cached' + +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 diff --git a/gems/lib/asciidoctor/foodogsquared-extensions.rb b/gems/lib/asciidoctor/foodogsquared-extensions.rb index 72e744d..431a261 100644 --- a/gems/lib/asciidoctor/foodogsquared-extensions.rb +++ b/gems/lib/asciidoctor/foodogsquared-extensions.rb @@ -18,6 +18,7 @@ require_relative 'wikipedia-inline-macro/extension' require_relative 'package-indices-link-macro/extension' require_relative 'fdroid-link-inline-macro/extension' require_relative 'musicbrainz-link-inline-macro/extension' +require_relative 'flathub-link-inline-macro/extension' require_relative 'repology-link-inline-macro/extension' Asciidoctor::Extensions.register do @@ -38,12 +39,16 @@ Asciidoctor::Extensions.register do inline_macro WikipediaInlineMacro + # Package indices inline_macro CtanLinkInlineMacro inline_macro PypiLinkInlineMacro inline_macro CratesIOLinkInlineMacro + + # App stores inline_macro FDroidLinkInlineMacro + inline_macro FlathubLinkInlineMacro + # Databases inline_macro MusicBrainzLinkInlineMacro - inline_macro RepologyLinkInlineMacro end