Anonymous user
Module:UserLinks: Difference between revisions
From the Croc Wiki, the Croc encyclopedia
Jump to navigationJump to search
add capability to link to a single link, use a custom error function, and generate html errors for invalid link types
(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:
local ToolbarBuilder = require('Module:Toolbar')
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
--------------------------------------------------------------------------------------------------
Line 137 ⟶ 141:
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]()
end
Line 169 ⟶ 176:
end
local function
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 err('
end
return getLink(linktype)
-- Generate the user data strings.▼
end
generateUserDataStrings(args) ▼
local function getLinks(args)
-- Build the template output.
local result = makeToolbar(args) -- Get the toolbar contents.
Line 192 ⟶ 200:
end
local function
return function (frame)
--
-- Otherwise, for testing purposes, assume args are being passed directly in.
local origArgs
break
else▼
origArgs = frame
end
▲ else
▲ origArgs = frame
▲ end
-- Strip whitespace, and treat blank arguments as nil.
-- 'user', 'User', and 'separator' have different behaviour depending on
-- whether they are blank or nil, so keep them as they are.
local args = {}
for k, v in pairs(origArgs) do
v = mw.text.trim(v)
if v ~= '' or k == 'user' or k == 'User' or k == 'separator' then
args[k] = v
end
end
▲ end
▲ 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
▲ end
end
return
main = makeWrapper(getLinks),
single = makeWrapper(getSingleLink)
}
|