Module:User

From the Croc Wiki, the Croc encyclopedia
Revision as of 14:26, April 6, 2014 by wikipedia>Mr. Stradivarius (Mr. Stradivarius moved page Module:User:Mr. Stradivarius/user to Module:User without leaving a redirect: use this to implement Template:User - it should be about twice as fast as the existing template)
Jump to navigationJump to search

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

local mUserLinks = require('Module:UserLinks')
local mShared = require('Module:UserLinks/shared')
local yesno = require('Module:Yesno')

local p = {}

local function validateArg(arg)
	if not arg then
		return nil
	end
	arg = arg:match('^%s*(.-)%s*$')
	if arg ~= '' then
		return arg
	end
end

function p.main(frame)
	local argKeys = {
		user = {
			1,
			'User',
			'user'
		},
		project = {
			2,
			'Project',
			'project'
		},
		lang = {
			3,
			'Lang',
			'lang'
		}
	}
	local origArgs = frame:getParent().args
	local args = {}
	for argKey, t in pairs(argKeys) do
		for i, origArgKey in ipairs(t) do
			local value = origArgs[origArgKey]
			value = validateArg(value)
			if value then
				args[argKey] = value
				break
			end
		end
	end
	local options = {
		span = false,
		separator = validateArg(origArgs.separator) or 'dot',
		isDemo = yesno(validateArg(origArgs.demo))
	}
	local codes = {'t', 'c'}
	local snippets = mUserLinks.getSnippets(args)
	local links = mUserLinks.getLinks(snippets)
	local success, result = pcall(mUserLinks.export, codes, links, options)
	if success then
		return result
	else
		return mShared.makeWikitextError(result, options.isDemo)
	end
end

return p