Module:UserLinks

From the Croc Wiki, the Croc encyclopedia
Revision as of 10:26, June 12, 2013 by wikipedia>Mr. Stradivarius (save initial version - user talk links only for the moment)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

Documentation for this module may be created at Module:UserLinks/doc

p = {}

local toolbar = require('Module:Toolbar')
local project, username

local function makeTalkLink()
    local plink = (project and (':' .. project .. ':')) or ''
    return '[[' .. plink .. 'User talk:' .. username .. '|talk]]'
end

local function getLink(linktype)
    local linktypes = {
        t = makeTalkLink
    }
    return linktypes[linktype]()
end

local function makeToolbar(args)
    local targs = {}
    for k, v in pairs(args) do
        if type(k) == 'number' then
            targs[k] = getLink(v)
        end
    end
    targs.style = args.small and 'font-size: 90%;'
    targs.separator = args.separator or 'dot'
    return toolbar.main(targs)
end

local function getLinks(args)
    -- If the username is absent or blank, return an html error and a tracking category.
    if args.user == '' or (not args.user and (not args.User or args.User == '')) then
        return '<span class="error">Error: No username detected by [[Module:UserLinks]].</span>[[Category:UserLinks transclusions without usernames]]'
    else
        username = args.user or args.User
    end
    -- Get the values of the other module-wide variables.
    project = args.Project
    small = args.small
    separator = args.separator
 
    return makeToolbar(args)
end    

function p.main(frame)
    -- 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
    if frame == mw.getCurrentFrame() then
        origArgs = frame:getParent().args
        for k, v in pairs(frame.args) do
            origArgs = frame.args
            break
        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
 
    return getLinks(args)
end

return p