Module:Hatnote: Difference between revisions
Jump to navigation
Jump to search
format p._main to accept a list of page/display tables as input
>Mr. Stradivarius (update main function description) |
>Mr. Stradivarius (format p._main to accept a list of page/display tables as input) |
||
Line 202: | Line 202: | ||
-- Produces a link to a main article or articles. If used in category or | -- 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". | -- category talk space, produces "The main article for this category is xxx". | ||
-- Otherwise, produces "Main article: xxx". Accepts | -- Otherwise, produces "Main article: xxx". Accepts an unlimited number of | ||
-- | -- page/display tables. Non-table inputs will result in an error. The first | ||
-- not in mainspace, uses "page" instead of "article". If more than one page is | -- value in the table should be the page name. Omitting this will result in an | ||
-- error, except in the case of the first table, which uses the page name as a | |||
-- | -- fallaback. The second value in the table is an optional display value for | ||
-- the link. If the first page name is not in mainspace, the output uses "page" | |||
-- instead of "article". If more than one page is specified, the function uses | |||
-- plural forms. | |||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
function p._main( | function p._main(...) | ||
-- | -- Get the list of pages. If no first page was specified we use the current | ||
local | -- page name. | ||
local pages = {...} | |||
local currentTitle = mw.title.getCurrentTitle() | local currentTitle = mw.title.getCurrentTitle() | ||
local firstPageTable = pages[1] | |||
local firstPage | |||
local | if firstPageTable then | ||
if | firstPage = firstPageTable[1] | ||
firstPage = | |||
else | else | ||
firstPage = currentTitle.text | firstPage = currentTitle.text | ||
firstPageTable = {firstPage} | |||
pages[1] = firstPageTable | |||
end | end | ||
for i, | |||
local link = | -- Make the list of formatted links | ||
local display = | local links = {} | ||
for i, t in ipairs(pages) do | |||
local link = t[1] | |||
local display = t[2] | |||
links[#links + 1] = formatLink(link, display) | links[#links + 1] = formatLink(link, display) | ||
end | end | ||
Line 261: | Line 267: | ||
end | end | ||
p.main = makeInvokeFunction( | function f.main(args) | ||
local pages = {} | |||
for k, v in pairs(args) do | |||
if type(k) == 'number' then | |||
local display = args['l' .. tostring(k)] | |||
local page = {v, display} | |||
pages[k] = page | |||
end | |||
end | |||
pages = mTableTools.compressSparseArray(pages) | |||
return p._main(unpack(pages)) | |||
end | |||
p.main = makeInvokeFunction(f.main) | |||
return p | return p |