Modül:For
Methods for displaying context links on the wiki.
contextLink.tmplFor(frame)(function)- Formats a simple disambiguation template.
- Parameter:
frameScribunto frame object (Frame) - Returns: Disambiguation template wikitext (string)
- See also: Template:For
contextLink.format(frame)(function)- Formats a single link in a disambiguation template.
- Parameter:
frameScribunto frame object (Frame) - Returns: Formatted link (string)
- See also: Template:Main
formatLink(link)(function • local)- Formats a link in a context link template to include further useful information, such as the destination wiki.
- Parameter:
linkLink to the destination page (string) - Returns: Formatted wikitext link to destination (string)
--- Methods for displaying context links on the wiki.
-- @module contextLink
-- @require [[Module:For/data]]
-- @author [[User:KockaAdmiralac|KockaAdmiralac]]
-- <nowiki>
require('strict')
local contextLink = {}
local data = mw.loadData('Module:For/data')
--- Formats a link in a context link template to include further useful
-- information, such as the destination wiki.
-- @function formatLink
-- @param {string} link Link to the destination page
-- @return {string} Formatted wikitext link to destination
-- @local
local function formatLink(link)
if mw.ustring.find(link, '|', 0, true) then
-- If there is a pipe in the link, the user already formatted it.
return string.format('[[%s]]', link)
else
local text = mw.ustring.gsub(link, '#', ' § ')
local title = mw.title.new(link)
if title.isExternal and data.interwiki[title.interwiki] then
text = mw.ustring.gsub(text, '^[^:]+:(.*)$', '%1 on ' .. data.interwiki[title.interwiki])
elseif title:inNamespace(14) then
link = ':' .. link
end
return string.format('[[%s|%s]]', link, text)
end
end
--- Formats a simple disambiguation template.
-- @function contextLink.tmplFor
-- @param {Frame} frame Scribunto frame object
-- @returns {string} Disambiguation template wikitext
-- @see [[Template:For]]
function contextLink.tmplFor(frame)
local args = frame:getParent().args
local links = {}
for index, value in ipairs(args) do
if tonumber(index) ~= 1 then
table.insert(links, formatLink(value))
end
end
local linkList
if #links == 0 then
return ''
elseif #links == 1 then
linkList = links[1]
elseif #links == 2 then
linkList = table.concat(links, ' or ')
else
links[#links] = 'or ' .. links[#links]
linkList = table.concat(links, ', ')
end
return mw.html.create('div')
:attr('role', 'note')
:addClass('hatnote')
:addClass('navigation-not-searchable')
:wikitext(string.format('For %s, see %s.', args[1], linkList))
:done()
end
--- Formats a single link in a disambiguation template.
-- @function contextLink.format
-- @param {Frame} frame Scribunto frame object
-- @returns {string} Formatted link
-- @see [[Template:Main]]
function contextLink.format(frame)
local link = frame.args[1]
local text = frame.args[2]
if text and text ~= '' then
return string.format('[[%s|%s]]', link, text)
else
return formatLink(link)
end
end
return contextLink
-- </nowiki>