Add gitref macro to reference a git hash

With this extension a git hash can be easily referenced.

The porters handbook versions chapter uses this extension
as an example in the FreeBSD 14.0 version table.

An example of use

gitref:hash[repository="src|doc|ports",length=hash-longitude]

The repository should be src, doc or ports.
And if no length it's specified the macro will use 12 characters
of the specified hash.

PR:             253050
Submitted by:   jhb@
This commit is contained in:
Sergio Carlavilla Delgado 2021-02-13 22:16:57 +01:00
parent ba631cb51b
commit fb36cfdfb2
5 changed files with 36 additions and 6 deletions

View file

@ -0,0 +1,25 @@
require 'asciidoctor/extensions' unless RUBY_ENGINE == 'opal'
include ::Asciidoctor
class GitReferencesMacro < Asciidoctor::Extensions::InlineMacroProcessor
use_dsl
named :gitref
def process parent, target, attrs
hash = target
repository = if (repository = attrs['repository'])
"#{repository}"
else
"src"
end
length = if (length = attrs['length'])
length.to_i
else
12
end
url = %(https://cgit.freebsd.org/#{repository}/commit/?id=#{hash})
%(<a href="#{url}">#{hash[0, length]}</a>)
end
end

5
shared/lib/git-macro.rb Normal file
View file

@ -0,0 +1,5 @@
RUBY_ENGINE == 'opal' ? (require 'GitReferencesMacro/extension') : (require_relative 'GitReferencesMacro/extension')
Asciidoctor::Extensions.register do
inline_macro GitReferencesMacro
end