Module:Hatnote: Difference between revisions
Jump to navigation
Jump to search
add Template:Main
>Mr. Stradivarius (split out the namespace-detecting part of formatLink to a new function) |
>Mr. Stradivarius (add Template:Main) |
||
Line 82: | Line 82: | ||
end | end | ||
end | end | ||
local function formatPages(...) | |||
-- Formats a list of pages using formatLink and returns it as an array. Nil | |||
-- values are not allowed. | |||
local pages = {...} | |||
local ret = {} | |||
for i, page in ipairs(pages) do | |||
ret[i] = formatLink(page) | |||
end | |||
return ret | |||
end | |||
local function makeWikitextError(msg) | local function makeWikitextError(msg) | ||
Line 170: | Line 182: | ||
function p._further(...) | function p._further(...) | ||
local | local links = formatPages(...) | ||
local text = 'Further information: ' .. mw.text.listToText(links) | |||
local text = 'Further information: ' .. mw.text.listToText( | |||
return p._rellink(text) | return p._rellink(text) | ||
end | end | ||
Line 188: | Line 196: | ||
p.further = makeInvokeFunction(f.further) | p.further = makeInvokeFunction(f.further) | ||
-------------------------------------------------------------------------------- | |||
-- Main | |||
-- | |||
-- Produces a link to a main article or articles. If used in category or | |||
-- category talk space, produces "The main article for this category is xxx". | |||
-- Otherwise, produces "Main article: xxx". Accepts an unlimited number of | |||
-- positional parameters, each of which is a page name. If the first positional | |||
-- parameter is not in mainspace, uses "page" instead of "article". If more | |||
-- than one page is specified, the function uses plural forms. | |||
-------------------------------------------------------------------------------- | |||
function p._main(args) | |||
-- Initialize variables. | |||
local links, firstPage | |||
local currentTitle = mw.title.getCurrentTitle() | |||
-- Make the list of formatted links and find the link for the first page. | |||
local nums = mTableTools.numKeys(args) | |||
if nums[1] then | |||
firstPage = args[nums[1]] | |||
links = {} | |||
else | |||
firstPage = currentTitle.text | |||
links = {formatLink(firstPage)} | |||
end | |||
for i, num in ipairs(nums) do | |||
local link = args[num] | |||
local display = args['l' .. tostring(num)] | |||
links[#links + 1] = formatLink(link, display) | |||
end | |||
-- Find the pagetype. | |||
local firstPageNs = findNamespaceId(firstPage) | |||
local pagetype = firstPageNs == 0 and 'article' or 'page' | |||
-- Build the text. | |||
local isPlural = #links > 1 | |||
local currentNs = currentTitle.namespace | |||
local isCategoryNamespace = currentNs - currentNs % 2 == 14 | |||
links = mw.text.listToText(links) | |||
local stringToFormat | |||
if isCategoryNamespace then | |||
if isPlural then | |||
stringToFormat = 'The main %ss for this' | |||
.. ' [[Wikipedia:Categorization|category]] are %s' | |||
else | |||
stringToFormat = 'The main %s for this' | |||
.. ' [[Wikipedia:Categorization|category]] is %s' | |||
end | |||
else | |||
if isPlural then | |||
stringToFormat = 'Main %ss: %s' | |||
else | |||
stringToFormat = 'Main %s: %s' | |||
end | |||
end | |||
local text = string.format(stringToFormat, pagetype, links) | |||
-- Pass the text to p._rellink. | |||
local extraclasses = 'relarticle mainarticle' | |||
return p._rellink(text, extraclasses) | |||
end | |||
p.main = makeInvokeFunction(p._main) | |||
return p | return p |