Add Wikipedia inline macro

This commit is contained in:
Gabriel Arazas 2023-04-07 05:58:50 +08:00
parent b1a64624d1
commit 9a2ba74191
No known key found for this signature in database
GPG Key ID: ADE0C41DAB221FCC
3 changed files with 53 additions and 0 deletions

View File

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

View File

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

View File

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