Modul:Redirect hatnote: Unterschied zwischen den Versionen
(remove the check for "other uses" coming last, as this is not how Template:Redirect currently works) |
(create a formatUseTable to reduce code redundancy) |
||
Zeile 43: | Zeile 43: | ||
iArg = iArg + 2 | iArg = iArg + 2 | ||
local useTable = data[iData] or {} | local useTable = data[iData] or {} | ||
local pages = useTable.pages or {} | |||
local use = args[iArg] | local use = args[iArg] | ||
local page = args[iArg + 1] | local page = args[iArg + 1] | ||
local nextUse = args[iArg + 2] | local nextUse = args[iArg + 2] | ||
pages[#pages + 1] = page | |||
useTable.pages = pages | |||
if use ~= 'and' then | if use ~= 'and' then | ||
useTable.use = use | useTable.use = use | ||
Zeile 61: | Zeile 63: | ||
return p._redirect(redirect, data, options) | return p._redirect(redirect, data, options) | ||
end | |||
local function formatUseTable(useTable, isFirst, redirect) | |||
-- Formats one use table. Use tables are the tables inside the data array. | |||
-- Each one corresponds to one use. (A use might be the word "cats" in the | |||
-- phrase "For cats, see [[Felines]]".) | |||
-- Returns a string, or nil if no use was specified. | |||
-- The isFirst parameter is used to apply special formatting for the first | |||
-- table in the data array. If isFirst is specified, the redirect parameter | |||
useTable = useTable or {} | |||
local use | |||
if isFirst then | |||
use = useTable.use or 'other uses' | |||
elseif not useTable.use then | |||
return nil | |||
elseif tonumber(useTable.use) == 1 then | |||
use = 'other uses' | |||
else | |||
use = useTable.use | |||
end | |||
local pages = useTable.pages | |||
if isFirst then | |||
redirect = redirect or error( | |||
'isFirst was set in formatUseTable, but no redirect was supplied', | |||
2 | |||
) | |||
pages[1] = pages[1] or redirect .. ' (disambiguation)' | |||
else | |||
pages[1] = pages[1] or useTable.use .. ' (disambiguation)' | |||
end | |||
pages = mHatnote.formatPages(unpack(pages)) | |||
pages = mw.text.listToText(pages) | |||
return string.format( | |||
'For %s, see %s.', | |||
use, | |||
pages | |||
) | |||
end | end | ||
Zeile 70: | Zeile 109: | ||
-- Generate the text. | -- Generate the text. | ||
local text = {} | local text = {} | ||
text[#text + 1] = '"' .. redirect .. '" redirects here.' | text[#text + 1] = '"' .. redirect .. '" redirects here.' | ||
text[#text + 1] = formatUseTable(data[1] or {}, true, redirect) | |||
if data[1] and data[1].use and data[1].use ~= 'other uses' then | |||
for i = 2, #data do | for i = 2, #data do | ||
text[#text + 1] = formatUseTable(data[i] or {}, false) | |||
text[#text + 1] = | |||
end | end | ||
end | end | ||
Zeile 115: | Zeile 124: | ||
-- Find whether to add the tracking category. | -- Find whether to add the tracking category. | ||
-- We add the category if: | -- We add the category if all of the following are true: | ||
-- a) redirect isn't any of the keywords 'REDIRECT', 'REDIRECT1', | -- a) redirect isn't any of the keywords 'REDIRECT', 'REDIRECT1', | ||
-- 'REDIRECT2', ..., or 'TERM'. | -- 'REDIRECT2', ..., or 'TERM'. |