Module:Hatnote: Difference between revisions

(split out the namespace-detecting part of formatLink to a new function)
Line 82:
end
end
 
local function formatPages(...)
-- Formats a list of pages using formatLink and returns it as an array. Nil
-- values are not allowed.
local formattedPagespages = {...}
local ret = {}
for i, page in ipairs(pages) do
formattedPagesret[i] = formatLink(page)
end
return ret
end
 
local function makeWikitextError(msg)
Line 170 ⟶ 182:
 
function p._further(...)
local pageslinks = {formatPages(...})
local text = 'Further information: ' .. mw.text.listToText(formattedPageslinks)
local formattedPages = {}
for i, page in ipairs(pages) do
formattedPages[i] = formatLink(page)
end
local text = 'Further information: ' .. mw.text.listToText(formattedPages)
return p._rellink(text)
end
Line 188 ⟶ 196:
 
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