website/gems/lib/asciidoctor/wikipedia-inline-macro/extension.rb

22 lines
559 B
Ruby
Raw Normal View History

2023-04-06 21:58:50 +00:00
# frozen_string_literal: true
require 'uri'
class WikipediaInlineMacro < Asciidoctor::Extensions::InlineMacroProcessor
use_dsl
named :wikipedia
name_positional_attributes 'caption'
default_attributes 'lang' => 'en'
def process(parent, target, attrs)
caption = attrs['caption'] || target
2023-05-15 06:01:58 +00:00
parser = URI::Parser.new
page = parser.escape target
2023-04-06 21:58:50 +00:00
link = %(https://#{attrs['lang']}.wikipedia.org/wiki/#{page})
node = create_anchor parent, caption, type: :link, target: link
create_inline parent, :quoted, node.convert
end
end