Add FlatHub link inline macro

This commit is contained in:
Gabriel Arazas 2023-05-27 18:53:34 +08:00
parent 620e99fdf2
commit 9af827eae5
No known key found for this signature in database
GPG Key ID: ADE0C41DAB221FCC
3 changed files with 70 additions and 1 deletions

View File

@ -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`.

View File

@ -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

View File

@ -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