Content deleted Content added
add missing error check to p.single |
convert p.getLinkFunctions to p.getLinks, so that we don't load the /extra module every time; __pairs function still buggy |
||
Line 3:
-- Lazily initialise modules that we might or might not need
local mExtra -- [[Module:UserLinks/extra]]
local mArguments -- [[Module:Arguments]]
local mToolbar -- [[Module:Toolbar]]
local mCategoryHandler -- [[Module:Category handler]]
local
local interwikiTable -- [[Module:InterwikiTable]], loaded with mw.loadData
-- Load shared helper functions
Line 22 ⟶ 23:
--------------------------------------------------------------------------------
function p.
local links, linkFunctions = {}, {}
local extraLinkFunctions
--
local function validateCode(code)
if type(code) == 'string' and code ~= '' then
return code
else
return nil
end
end
local function loadExtra()
local success
success, module = pcall(require, 'Module:UserLinks/extra')
if success then
else
mExtra = false
end
end
end
local function getExtraLinkFunctions()
if extraLinkFunctions ~= nil then
return extraLinkFunctions
end
loadExtra()
if type(mExtra) == 'table' and type(mExtra.linkFunctions) == 'table' then
extraLinkFunctions = mExtra.linkFunctions
else
extraLinkFunctions = false
end
return extraLinkFunctions
end
-- Define the metatable.
setmetatable(links, {
__index = function (t, key)
local code = validateCode(key)
if not code then
raiseError('invalid link code detected|Invalid link code')
end
local linkFunction = linkFunctions[code]
local link
if linkFunction then
link = linkFunction(snippets)
links[code] = link
else
extraLinkFunctions = getExtraLinkFunctions()
if extraLinkFunctions then
local extraLinkFunction = extraLinkFunctions[code]
if type(extraLinkFunction) == 'function' then
link = extraLinkFunction(snippets)
links[code] = link
end
end
end
return link
end,
__pairs = function ()
return function (t, key)
local nextBuiltInKey, linkFunction = next(linkFunctions, key)
if linkFunction then
local link = linkFunction(snippets)
links[nextBuiltInKey] = link
return nextBuiltInKey, link
else
extraLinkFunctions = getExtraLinkFunctions()
if extraLinkFunctions then
local nextExtraKey, extraLinkFunction = next(extraLinkFunctions, key)
if validateCode(nextKey) and type(extraLinkFunction) == 'function' then
local link = extraLinkFunction(snippets)
links[nextExtraKey] = link
return nextExtraKey, link
end
end
end
return nil, nil
end
end
})
-- Define the built-in link functions.
function linkFunctions.u(snippets)
-- User page
Line 174 ⟶ 238:
end
return
end
Line 352 ⟶ 416:
local snippets = p.getSnippets(args)
local codes = p.getCodes(args)
local
local success, result = pcall(p.export, codes,
if success then
return result
Line 377 ⟶ 441:
end
function p.export(codes,
-- Make the user link.
local userLink =
-- If we weren't passed any link codes, just return the user link.
Line 390 ⟶ 454:
local toolbarArgs = {}
for i, code in ipairs(codes) do
local
toolbarArgs[#toolbarArgs + 1] = link
end
Line 425 ⟶ 485:
end
local snippets = p.getSnippets(args)
local
-- Define a function so we can get the link with pcall, as indexing the
-- links table might produce an error because of how the metatable is set up.
local function getLink(code)
return links[code]
end
local success, link = pcall(getLink, code)
if success then
return
else
return makeWikitextError(
end
end
|