diff --git a/gems/lib/asciidoctor/foodogsquared-extensions.rb b/gems/lib/asciidoctor/foodogsquared-extensions.rb index d98ce3d..7b40f45 100644 --- a/gems/lib/asciidoctor/foodogsquared-extensions.rb +++ b/gems/lib/asciidoctor/foodogsquared-extensions.rb @@ -4,10 +4,12 @@ require_relative 'man-inline-macro/extension' require_relative 'swhid-inline-macro/extension' require_relative 'github-raw-content-include-processor/extension' require_relative 'gitlab-raw-content-include-processor/extension' +require_relative 'gitlab-link-inline-macro/extension' Asciidoctor::Extensions.register do inline_macro ManInlineMacro inline_macro SWHInlineMacro include_processor GitHubRawIncludeProcessor include_processor GitLabRawIncludeProcessor + inline_macro GitLabLinkInlineMacro end diff --git a/gems/lib/asciidoctor/gitlab-link-inline-macro/README.adoc b/gems/lib/asciidoctor/gitlab-link-inline-macro/README.adoc new file mode 100644 index 0000000..db596db --- /dev/null +++ b/gems/lib/asciidoctor/gitlab-link-inline-macro/README.adoc @@ -0,0 +1,33 @@ += GitLab link inline macro +:toc: + + +An inline macro for easily linking objects from GitLab instances. + + +== Synopsis + +[source, asciidoc] +---- +gitlab:$OWNER/$REPO[$CAPTION] +---- + + +== Attributes + +- `domain` is the base domain of the GitLab instance. +By default, it points to the official instance of `gitlab.com`. + +- `rev` is the commit of the repo. +By default. it doesn't point to anything which should be in the default branch of the repository. + +- `path` is the filepath to be linked. + + +== Example usage + +- `gitlab:gitlab-org/gitlab[]` will link to link:https://gitlab.com/gitlab-org/gitlab[the GitLab's source code with the default domain]. + +- `gitlab:gitlab-org/gitlab[rev=0c9f77389424b6c5fd8e96b227e9125a13a07cb3, path=README.md]` should link to the link:https://gitlab.com/gitlab-org/gitlab/-/blob/0c9f77389424b6c5fd8e96b227e9125a13a07cb3/README.md[GitLab's README from 3 years ago]. + +- `gitlab:GNOME/mutter[domain=gitlab.gnome.org, rev=df653b95adf6462fc731998eb53b0860baa7253c, path=meson.build]` should link to link:https://gitlab.gnome.org/GNOME/mutter/-/blob/df653b95adf6462fc731998eb53b0860baa7253c/meson.build[Mutter v44.beta `meson.build` from GNOME GitLab instance]. diff --git a/gems/lib/asciidoctor/gitlab-link-inline-macro/extension.rb b/gems/lib/asciidoctor/gitlab-link-inline-macro/extension.rb new file mode 100644 index 0000000..37c75ab --- /dev/null +++ b/gems/lib/asciidoctor/gitlab-link-inline-macro/extension.rb @@ -0,0 +1,24 @@ +require 'uri' + +class GitLabLinkInlineMacro < Asciidoctor::Extensions::InlineMacroProcessor + use_dsl + + named :gitlab + name_positional_attributes 'caption' + default_attributes 'domain' => 'gitlab.com' + + def process parent, target, attrs + doc = parent.document + + text = attrs['caption'] || target + uri = URI.parse %(https://#{attrs['domain']}/#{target}) + + uri.path += %(/-/tree/#{attrs['rev']}) if attrs['rev'] + uri.path += %(/#{attrs['path']}) if attrs['path'] + + target = uri.to_s + + doc.register :links, target + create_anchor parent, text, type: :link, target: target + end +end