Add custom extension for package indices

This commit is contained in:
Gabriel Arazas 2023-05-11 00:07:45 +08:00
parent d4e3e70e6f
commit 50a9c33f8c
2 changed files with 61 additions and 0 deletions

View File

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

View File

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