Module:UserLinks: Difference between revisions

From the Croc Wiki, the Croc encyclopedia
Jump to navigationJump to search
Content added Content deleted
(fix bug where the username error wasn't showing up properly, add more comments)
(add capability to link to a single link, use a custom error function, and generate html errors for invalid link types)
Line 1: Line 1:
local p = {}

local ToolbarBuilder = require('Module:Toolbar')
local ToolbarBuilder = require('Module:Toolbar')
local u = {} -- Table for user-data helper strings.
local u = {} -- Table for user-data helper strings.

-- Define a custom error message for this module.
local function err(msg, cat)
cat = (cat and ('[[Category:' .. cat .. ']]')) or ''
return '<span class="error">[[Module:UserLinks]] error: ' .. msg .. '.</span>' .. cat
end


--------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------
Line 137: Line 141:
rfa = makeRfaLink
rfa = makeRfaLink
}
}
if not linktypes[linktype] then
return err('"' .. linktype .. '" is not a valid link code', 'UserLinks transclusions with invalid link types')
end
return linktypes[linktype]()
return linktypes[linktype]()
end
end
Line 169: Line 176:
end
end


local function getLinks(args)
local function getSingleLink(args)
local linktype = args[1]
-- If the username is absent or blank, return an html error and a tracking category.
if not linktype then
if args.user == '' or (not args.user and (not args.User or args.User == '')) then
return '<span class="error">Error: No username detected by [[Module:UserLinks]].</span>[[Category:UserLinks transclusions without usernames]]'
return err('No link type specified', 'UserLinks transclusions with invalid link types')
end
end
return getLink(linktype)
-- Generate the user data strings.
end
generateUserDataStrings(args)

local function getLinks(args)
-- Build the template output.
-- Build the template output.
local result = makeToolbar(args) -- Get the toolbar contents.
local result = makeToolbar(args) -- Get the toolbar contents.
Line 192: Line 200:
end
end


function p.main(frame)
local function makeWrapper(func)
return function (frame)
-- If called via #invoke, use the args passed into the invoking template.
-- Otherwise, for testing purposes, assume args are being passed directly in.
-- If called via #invoke, use the args passed into the invoking template.
-- Otherwise, for testing purposes, assume args are being passed directly in.
local origArgs
local origArgs
if frame == mw.getCurrentFrame() then
origArgs = frame:getParent().args
if frame == mw.getCurrentFrame() then
for k, v in pairs(frame.args) do
origArgs = frame:getParent().args
origArgs = frame.args
for k, v in pairs(frame.args) do
break
origArgs = frame.args
break
end
else
origArgs = frame
end
end
else
origArgs = frame
end
-- Strip whitespace, and treat blank arguments as nil.
-- Strip whitespace, and treat blank arguments as nil.
-- 'user', 'User', and 'separator' have different behaviour depending on
-- 'user', 'User', and 'separator' have different behaviour depending on
-- whether they are blank or nil, so keep them as they are.
-- whether they are blank or nil, so keep them as they are.
local args = {}
local args = {}
for k, v in pairs(origArgs) do
for k, v in pairs(origArgs) do
v = mw.text.trim(v)
v = mw.text.trim(v)
if v ~= '' or k == 'user' or k == 'User' or k == 'separator' then
if v ~= '' or k == 'user' or k == 'User' or k == 'separator' then
args[k] = v
args[k] = v
end
end
end

end
-- If the username is absent or blank, return an error and a tracking category.
if args.user == '' or (not args.user and (not args.User or args.User == '')) then
return err('No username detected', 'UserLinks transclusions without usernames')
end
-- Generate the user data strings.
generateUserDataStrings(args)
return getLinks(args)
return func(args)
end
end
end


return p
return {
main = makeWrapper(getLinks),
single = makeWrapper(getSingleLink)
}