diff --git a/lib/asciidoctor/foodogsquared-extensions.rb b/lib/asciidoctor/foodogsquared-extensions.rb index 4637e22..3d57215 100644 --- a/lib/asciidoctor/foodogsquared-extensions.rb +++ b/lib/asciidoctor/foodogsquared-extensions.rb @@ -13,6 +13,7 @@ require_relative 'github-raw-content-include-processor/extension' require_relative 'gitlab-link-inline-macro/extension' require_relative 'gitlab-raw-content-include-processor/extension' require_relative 'chat-block-processor/extension' +require_relative 'wikipedia-inline-macro/extension' Asciidoctor::Extensions.register do inline_macro ManInlineMacro @@ -26,4 +27,6 @@ Asciidoctor::Extensions.register do inline_macro GitLabLinkInlineMacro include_processor GitLabRawIncludeProcessor + + inline_macro WikipediaInlineMacro end diff --git a/lib/asciidoctor/wikipedia-inline-macro/README.adoc b/lib/asciidoctor/wikipedia-inline-macro/README.adoc new file mode 100644 index 0000000..e86cb8e --- /dev/null +++ b/lib/asciidoctor/wikipedia-inline-macro/README.adoc @@ -0,0 +1,30 @@ += Wikipedia inline macro +:toc: + + +An inline macro for easily creating Wikipedia links (i.e., `wikipedia.org`). + + +== Synopsis + +[source, asciidoc] +---- +wikipedia:PAGE[$CAPTION] +---- + +Where if `$CAPTION` is present, it will be used as the link text. +Otherwise, it will just be the page. + + +== Attributes + +- `lang` is the language domain to be linked into. +By default, it is set to `en`. + + +== Example usage + +- `wikipedia:Diff[]` should link to the link:https://en.wikipedia.org/wiki/Diff[Diff page on English Wikipedia]. +- `wikipedia:Diff[lang=ja]` should link to the link:https://ja.wikipedia.org/wiki/Diff[Diff page on Japanese Wikipedia]. +- `wikipedia:Photosynthesis[lang=simple]` should link to the link:https://simple.wikipedia.org/wiki/Photosynthesis[Photosynthesis page on Simple English Wikipedia]. +- `wikipedia:Diff[diff in Japanese Wikipedia, lang=ja]` should link to the link:https://ja.wikipedia.org/wiki/Diff[Diff page on Japanese Wikipedia] with the `diff in Japanese Wikipedia` as the link text. diff --git a/lib/asciidoctor/wikipedia-inline-macro/extension.rb b/lib/asciidoctor/wikipedia-inline-macro/extension.rb new file mode 100644 index 0000000..c2a0d60 --- /dev/null +++ b/lib/asciidoctor/wikipedia-inline-macro/extension.rb @@ -0,0 +1,20 @@ +# 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 + page = URI.encode_www_form_component target + 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