Modul:Redirect hatnote: Unterschied zwischen den Versionen
Updated from sandbox with a couple of comment tweaks; now uses Module:Hatnote list for a good chunk of its functionality.
(Added tracking for and-gaps) |
(Updated from sandbox with a couple of comment tweaks; now uses Module:Hatnote list for a good chunk of its functionality.) |
||
Zeile 6: | Zeile 6: | ||
local mHatnote = require('Module:Hatnote') | local mHatnote = require('Module:Hatnote') | ||
local mHatList = require('Module:Hatnote list') | |||
local mArguments --lazily initialize | |||
local libraryUtil = require('libraryUtil') | local libraryUtil = require('libraryUtil') | ||
local checkType = libraryUtil.checkType | local checkType = libraryUtil.checkType | ||
Zeile 22: | Zeile 24: | ||
function p.redirect(frame) | function p.redirect(frame) | ||
mArguments = require('Module:Arguments') | |||
local args = mArguments.getArgs(frame, {parentOnly=true}) | |||
local args = {} | --Get number of redirects | ||
local numRedirects = tonumber(frame.args[1]) or 1 | |||
-- Create the options table. | |||
local options = {} | |||
options.selfref = args.selfref | |||
return p._redirect(args, numRedirects, options) | |||
end | |||
-- | function p._redirect(args, numRedirects, options, currentTitle, redirectTitle, targetTitle) | ||
-- Validate the input. Don't bother checking currentTitle, redirectTitle or | |||
-- targetTitle, as they are only used in testing. | |||
checkType('_redirect', 1, args, 'table') | |||
checkType('_redirect', 2, numRedirects, 'number', true) | |||
numRedirects = numRedirects or 1 | |||
checkType('_redirect', 3, options, 'table', true) | |||
options = options or {} | |||
currentTitle = currentTitle or mw.title.getCurrentTitle() | |||
-- Get the table of redirects | |||
local redirect = {} | local redirect = {} | ||
for i = 1, numRedirects do | for i = 1, numRedirects do | ||
Zeile 50: | Zeile 56: | ||
redirect[i] = args[i] | redirect[i] = args[i] | ||
end | end | ||
-- Generate the text. | -- Generate the text. | ||
local formattedRedirect = {} | local formattedRedirect = {} | ||
for k,v in pairs(redirect) do | for k,v in pairs(redirect) do | ||
formattedRedirect[k] = '"' .. v .. '"' | formattedRedirect[k] = '"' .. v .. '"' | ||
end | end | ||
text | local text = { | ||
mHatList.andList(formattedRedirect) .. ' ' .. (#redirect == 1 and 'redirects' or 'redirect') .. ' here.', | |||
mHatList._forSee(args, #redirect + 1, {title = redirect[1]}) | |||
} | |||
text = table.concat(text, ' ') | text = table.concat(text, ' ') | ||
-- Functionality for adding categories | |||
local categoryTable = {} | local categoryTable = {} | ||
function addCategory(cat) | function addCategory(cat) | ||
if cat and cat ~= '' then | if cat and cat ~= '' then | ||
-- Add by index to avoid duplicates | |||
categoryTable[string.format('[[Category:%s]]', cat)] = true | categoryTable[string.format('[[Category:%s]]', cat)] = true | ||
end | end | ||
end | end | ||
--Generate tracking categories | |||
local mhOptions = {} | local mhOptions = {} | ||
for k,v in pairs(redirect) do | for k,v in pairs(redirect) do | ||
-- We don't need a tracking category if the template invocation has been | -- We don't need a tracking category if the template invocation has been | ||
-- copied directly from the docs, or if we aren't in main- or category-space. | -- copied directly from the docs, or if we aren't in main- or category-space. |