Module:Link
Documentation for this module may be created at Module:Link/doc
-- This module is a replacement for [[Template:Lw]] and other link templates.
local p = {}
-- getSubpageLink tests to see if the subpage exists. If it does, it returns a link to the subpage. If not, it returns nil.
local function getSubpageLink(link)
local title
local prefixedText
local result
title = mw.title.getCurrentTitle()
prefixedText = title.prefixedText
title = mw.title.new(prefixedText .. '/' .. link)
if title.exists then
result = '[[/' .. link .. '/]]'
else
error()
end
return result
end
-- getWikiversityLink tests to see if the resource page exists. If it does, it returns a link to the page. If not, it returns nil.
local function getWikiversityLink(link)
local title
local result
title = mw.title.new(link)
if title.exists then
result = '[[' .. link .. ']]'
else
error()
end
return result
end
-- getWikipediaLink returns a link to Wikipedia as w:.
local function getWikipediaLink(link)
return '[[w:' .. link .. ']]'
end
-- getSilentWikipediaLink returns a link to Wikipedia without displaying the w:.
local function getSilentWikipediaLink(link)
return '[[w:' .. link .. '|' .. link .. ']]'
end
-- lsw takes two parameters. The first is a link title. The second is either nil or not.
-- The function will return either a subpage link if it exists, or a Wikiversity link if it exists,
-- or a Wikipedia link if no Wikiversity link exists. If the second parameter is not nil, the
-- Wikipedia link is a silent redirect (no w: displayed).
function p.lsw(frame)
local link
local result
link = mw.text.trim(frame.args[1])
if pcall(function () result = getSubpageLink(link) end) then
return result
end
if pcall(function () result = getWikiversityLink(link) end) then
return result
end
if frame.args[2] == nil then
return getWikipediaLink(link)
else
return getSilentWikipediaLink(link)
end
end
-- lw takes two parameters. The first is a link title. The second is either nil or not.
-- The function will return either a Wikiversity link if it exists, or a Wikipedia link if
-- no Wikiversity link exists. If the second parameter is not nil, the Wikipedia link is
-- a silent redirect (no w: displayed).
function p.lw(frame)
local link
local result
link = mw.text.trim(frame.args[1])
if pcall(function () result = getWikiversityLink(link) end) then
return result
end
if frame.args[2] == nil then
return getWikipediaLink(link)
else
return getSilentWikipediaLink(link)
end
end
return p