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 |
|||
⚫ | |||
-------------------------------------------------------------------------------------------------- |
-------------------------------------------------------------------------------------------------- |
||
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') |
|||
⚫ | |||
return linktypes[linktype]() |
return linktypes[linktype]() |
||
end |
end |
||
Line 169: | Line 176: | ||
end |
end |
||
local function |
local function getSingleLink(args) |
||
local linktype = args[1] |
|||
⚫ | |||
if not linktype then |
|||
⚫ | |||
return ' |
return err('No link type specified', 'UserLinks transclusions with invalid link types') |
||
end |
end |
||
return getLink(linktype) |
|||
⚫ | |||
end |
|||
⚫ | |||
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 |
local function makeWrapper(func) |
||
return function (frame) |
|||
-- If called via #invoke, use the args passed into the invoking template. |
|||
-- |
-- 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 |
|||
if frame == mw.getCurrentFrame() then |
|||
origArgs = frame:getParent().args |
|||
for k, v in pairs(frame.args) do |
|||
origArgs = frame.args |
|||
break |
|||
⚫ | |||
⚫ | |||
origArgs = frame |
|||
end |
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 |
||
⚫ | |||
⚫ | |||
⚫ | |||
return err('No username detected', 'UserLinks transclusions without usernames') |
|||
end |
|||
⚫ | |||
⚫ | |||
return |
return func(args) |
||
⚫ | |||
end |
end |
||
return |
return { |
||
main = makeWrapper(getLinks), |
|||
single = makeWrapper(getSingleLink) |
|||
} |