Editing Module:Userlinks
Jump to navigation
Jump to search
The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then publish the changes below to finish undoing the edit.
Latest revision | Your text | ||
Line 1: | Line 1: | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
-- | -- UserLinks -- | ||
-- This module creates a list of links about a given user. It can be used on -- | -- This module creates a list of links about a given user. It can be used on -- | ||
-- its own or from a template. See the /doc page for more documentation. -- | -- its own or from a template. See the /doc page for more documentation. -- | ||
Line 9: | Line 9: | ||
-- Lazily initialise modules that we might or might not need | -- Lazily initialise modules that we might or might not need | ||
local mExtra -- [[Module: | local mExtra -- [[Module:UserLinks/extra]] | ||
local mArguments -- [[Module:Arguments]] | local mArguments -- [[Module:Arguments]] | ||
local mToolbar -- [[Module:Toolbar]] | local mToolbar -- [[Module:Toolbar]] | ||
Line 17: | Line 17: | ||
-- Load shared helper functions | -- Load shared helper functions | ||
local mShared = require('Module: | local mShared = require('Module:UserLinks/shared') | ||
local raiseError = mShared.raiseError | local raiseError = mShared.raiseError | ||
local maybeLoadModule = mShared.maybeLoadModule | local maybeLoadModule = mShared.maybeLoadModule | ||
Line 38: | Line 38: | ||
-- indexed. This is made possible by the metatable and by the various link | -- indexed. This is made possible by the metatable and by the various link | ||
-- functions, some of which are defined here, and some of which are defined | -- functions, some of which are defined here, and some of which are defined | ||
-- at [[Module: | -- at [[Module:UserLinks/extra]]. | ||
--]=] | --]=] | ||
local links, linkFunctions = {}, {} | local links, linkFunctions = {}, {} | ||
Line 58: | Line 58: | ||
snippets.username | snippets.username | ||
) | ) | ||
end | end | ||
Line 351: | Line 340: | ||
end | end | ||
---------------------------------------------------------------------------- | ---------------------------------------------------------------------------- | ||
-- End of link functions | -- End of link functions | ||
Line 377: | Line 345: | ||
-- Define the metatable that memoizes the link functions, and fetches link | -- Define the metatable that memoizes the link functions, and fetches link | ||
-- functions from [[Module: | -- functions from [[Module:UserLinks/extra]] if necessary. | ||
-- Lazily initialise the extraLinkFunctions table. We only want to load | -- Lazily initialise the extraLinkFunctions table. We only want to load | ||
-- [[Module: | -- [[Module:UserLinks/extra]] as necessary, so it has a low transclusion | ||
-- count. | -- count. | ||
local extraLinkFunctions | local extraLinkFunctions | ||
Line 408: | Line 376: | ||
-- Here we use the distinction between false and nil to record | -- Here we use the distinction between false and nil to record | ||
-- whether we have already tried to load the /extra module. | -- whether we have already tried to load the /extra module. | ||
mExtra = maybeLoadModule('Module: | mExtra = maybeLoadModule('Module:UserLinks/extra') | ||
end | end | ||
if type(mExtra) == 'table' | if type(mExtra) == 'table' | ||
Line 503: | Line 471: | ||
-- | -- | ||
-- If you add a new snippet, please document it at | -- If you add a new snippet, please document it at | ||
-- [[Module: | -- [[Module:UserLinks#Adding new links]]. | ||
--]=] | --]=] | ||
local snippets, snippetFunctions = {}, {} | local snippets, snippetFunctions = {}, {} | ||
Line 669: | Line 637: | ||
-- End snippet functions. If you add a new snippet function, please | -- End snippet functions. If you add a new snippet function, please | ||
-- document it at [[Module: | -- document it at [[Module:UserLinks#Adding new links]]. | ||
return snippets | return snippets | ||
Line 730: | Line 698: | ||
local options = {} | local options = {} | ||
options.isDemo = yesno(args.demo) or false | options.isDemo = yesno(args.demo) or false | ||
options.toolbarStyle = yesno(args.small) and 'font-size: 90%;' or nil | options.toolbarStyle = yesno(args.small) and 'font-size: 90%;' or nil | ||
options.sup = yesno(args.sup, true) | options.sup = yesno(args.sup, true) | ||
Line 756: | Line 723: | ||
function p.export(codes, links, options) | function p.export(codes, links, options) | ||
-- Make the user link. | -- Make the user link. | ||
local userLink = | local userLink = links.u | ||
-- If we weren't passed any link codes, just return the user link. | -- If we weren't passed any link codes, just return the user link. | ||
Line 819: | Line 786: | ||
return links[code] | return links[code] | ||
end | 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] = '{| class="wikitable plainlinks sortable"' | |||
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 | return p |