---  
local function makeInvokeFunction(funcName)
-- Makes a function that can be accessed from #invoke. This is only required
-- for functions that need to access arguments.
return function
mArguments = require('Module:Arguments')
local args = mArguments.getArgs
return p[funcName](args)
end
end  
p.main = makeInvokeFunction('_main')  
function p._main(args)
-- The main function. This is the one called from Template:User-multi,
-- via p.main.
local options = p.getOptions(args)
local snippets = p.getSnippets(args)
local codes = p.getCodes(args)
local links = p.getLinks(snippets)
-- Overload the built-in Lua error function to generate wikitext errors
-- meant for end users to see. This makes things harder to debug when
-- real errors occur, but it is the only realistic way to show wikitext
-- errors and and still have sane code when using metatables, etc.
local success, result = pcall(p.export, codes, links, options)
if success then
return result
else
return makeWikitextError(result, options.isDemo)
end
end  
function p.getOptions(args)
-- Gets the options from the args table, so that we don't have to pass
-- around the whole args table all the time.
local options = {}
options.isDemo = yesno(args.demo) or false
options.noPing = yesno(args.noPing) or yesno(args.noping) or yesno(args.np) or false
options.toolbarStyle = yesno(args.small) and 'font-size: 90%;' or nil
options.sup = yesno(args.sup, true)
options.separator = args.separator
options.span = args.span
return options
end  
function p.getCodes(args)
-- Gets the link codes from the arguments. The codes aren't validated
-- at this point.
mTableTools = maybeLoadModule('Module:TableTools')
local codes
if mTableTools then
codes = mTableTools.compressSparseArray(args)
else
codes = {}
for i, code in ipairs(args) do
codes[i] = code
end
end
return codes
end  
function p.export(codes, links, options)
-- Make the user link.
local userLink = options.noPing and links.np or links.u  
-- If we weren't passed any link codes, just return the user link.
if #codes < 1 then
return userLink
end  
-- Make the toolbar.
mToolbar = require('Module:Toolbar')
local toolbarArgs = {}
for i, code in ipairs(codes) do
local link = links[code]
toolbarArgs[#toolbarArgs + 1] = link
end
toolbarArgs.style = options.toolbarStyle
toolbarArgs.separator = options.separator or 'dot'
toolbarArgs.span = options.span
local toolbar = mToolbar.main(toolbarArgs)  
-- Apply the sup option.
if options.sup then
toolbar = '<sup>' .. toolbar .. '</sup>'
end  
-- If we are transcluding, add a non-breaking space, but if we are substing
-- just use a normal space
local space = mw.isSubsting and ' ' or '&nbsp;'  
return userLink .. space .. toolbar
end  
---
-- Single link function
---  
p.single = makeInvokeFunction('_single')  
function p._single(args)
-- Fetches a single link from the link table.
local options = p.getOptions(args)
local snippets = p.getSnippets(args)
local links = p.getLinks(snippets)
local code = args[1]
local success, link = pcall(p.exportSingle, links, code)
if success then
return link
else
return makeWikitextError(link, options.isDemo)
end
end  
function p.exportSingle(links, code)
-- If any errors occur, they will probably occur here. This function
-- exists purely so that all the errors that will occur in p._single can
-- be handled using a single pcall.
if not code then
raiseError(
message('error-nolinkcode'),
message('error-nolinkcode-section')
end
return links[code]
end  
---
-- Link table
---  
function p.linktable
-- Returns a wikitext table of link codes, with an example link for each
-- one. This function doesn't take any arguments, so it can be accessed
-- directly from wiki pages without using makeInvokeFunction.
local args = {user = 'Example'}
local snippets = p.getSnippets(args)
local links = p.getLinks(snippets)  
-- Assemble the codes and links in order
local firstCodes = {'u', 't', 'c'}
local firstLinks, firstCodesKeys = {}, {}
for i, code in ipairs(firstCodes) do
firstCodesKeys[code] = true
firstLinks[#firstLinks + 1] = {code, links[code]}
end
local secondLinks = {}
for code, link in pairs(links) do
if not firstCodesKeys[code] then
secondLinks[#secondLinks + 1] = {code, link}
end
end
table.sort(secondLinks, function(t1, t2)
return t1[1] < t2[1]
end)
local links = {}
for i, t in ipairs(firstLinks) do
links[#links + 1] = t
end
for i, t in ipairs(secondLinks) do
links[#links + 1] = t
end  
-- Output the code table in table format
local ret = {}
ret[#ret + 1] = '{|'
ret[#ret + 1] = '|-'
ret[#ret + 1] = '! ' .. message('linktable-codeheader')
ret[#ret + 1] = '! ' .. message('linktable-previewheader')
for i, t in ipairs(links) do
local code = t[1]
local link = t[2]
ret[#ret + 1] = '|-'
ret[#ret + 1] = "| **" .. code .. "**"
ret[#ret + 1] = '| ' .. link
end
ret[#ret + 1] = '|}'
return table.concat(ret, '\n')
end  
return p