Module:UserLinks

From the Croc Wiki, the Croc encyclopedia
Revision as of 11:27, June 12, 2013 by wikipedia>Mr. Stradivarius (output user links)
Jump to navigationJump to search

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

p = {}

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

local function makeUserLink()
    return '[[' .. project .. 'User:' .. username .. '|' .. username .. ']]'
end

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

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

local function makeToolbar(args)
    local targs = {}
    local numArgsExist = false
    for k, v in pairs(args) do
        if type(k) == 'number' then
            numArgsExist = true
            targs[k] = getLink(v)
        end
    end
    targs.style = args.small and 'font-size: 90%;'
    targs.separator = args.separator or 'dot'
    
    if numArgsExist == false then
        return -- Don't return a toolbar if no numeric arguments exist.
    else
        return ToolbarBuilder.main(targs)
    end
end

local function getLinks(args)
    -- Get the username value. If it 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 project value.
    if args.Project then
        project = ':' .. args.Project .. ':'
    else
        project = ''
    end
    
    local result = makeToolbar(args)
    if result then
        if args.sup then
            result = '<sup>' .. result .. '</sup>'
        end
        result = '&nbsp;' .. result
    else
        result = ''
    end
    result = '<span>' .. makeUserLink() .. result .. '</span>'
    
    return result
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