From 50a9c33f8c55925d690a29f9c3812a79c1a19087 Mon Sep 17 00:00:00 2001 From: Gabriel Arazas Date: Thu, 11 May 2023 00:07:45 +0800 Subject: [PATCH] Add custom extension for package indices --- lib/asciidoctor/foodogsquared-extensions.rb | 5 ++ .../package-indices-link-macro/extension.rb | 56 +++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 lib/asciidoctor/package-indices-link-macro/extension.rb diff --git a/lib/asciidoctor/foodogsquared-extensions.rb b/lib/asciidoctor/foodogsquared-extensions.rb index 26df17f..f2ad5ce 100644 --- a/lib/asciidoctor/foodogsquared-extensions.rb +++ b/lib/asciidoctor/foodogsquared-extensions.rb @@ -15,6 +15,7 @@ require_relative 'gitlab-raw-content-include-processor/extension' require_relative 'chat-block-processor/extension' require_relative 'git-blob-include-processor/extension' require_relative 'wikipedia-inline-macro/extension' +require_relative 'package-indices-link-macro/extension' Asciidoctor::Extensions.register do inline_macro ManInlineMacro @@ -33,4 +34,8 @@ Asciidoctor::Extensions.register do preprocessor GitContentBranchAttributePreprocessor inline_macro WikipediaInlineMacro + + inline_macro CtanLinkInlineMacro + inline_macro PypiLinkInlineMacro + inline_macro CratesIOLinkInlineMacro end diff --git a/lib/asciidoctor/package-indices-link-macro/extension.rb b/lib/asciidoctor/package-indices-link-macro/extension.rb new file mode 100644 index 0000000..62ebd1f --- /dev/null +++ b/lib/asciidoctor/package-indices-link-macro/extension.rb @@ -0,0 +1,56 @@ +# frozen_string_literal: true + +# I'm fairly sure this could be programmed since Ruby has nice metaprogramming +# capabilities. Though, we'll be keeping it manually defining classes for now +# for initial versions since there could be additional features for each macro. + +class CtanLinkInlineMacro < Asciidoctor::Extensions::InlineMacroProcessor + use_dsl + + named :ctan + name_positional_attributes 'caption' + + def process(parent, target, attrs) + doc = parent.document + text = attrs['caption'] || target + url = %(https://ctan.org/pkg/#{target}) + + doc.register :links, url + + create_anchor parent, text, type: :link, target: url + end +end + +class PypiLinkInlineMacro < Asciidoctor::Extensions::InlineMacroProcessor + use_dsl + + named :pypi + name_positional_attributes 'caption' + + def process(parent, target, attrs) + doc = parent.document + text = attrs['caption'] || target + url = %(https://pypi.org/project/#{target}) + + doc.register :links, url + + create_anchor parent, text, type: :link, target: url + end +end + +class CratesIOLinkInlineMacro < Asciidoctor::Extensions::InlineMacroProcessor + use_dsl + + named :cratesio + name_positional_attributes 'caption' + + def process(parent, target, attrs) + doc = parent.document + text = attrs['caption'] || target + url = %(https://crates.io/crates/#{target}) + + doc.register :links, url + + create_anchor parent, text, type: :link, target: url + end +end