Module:Infobox Var

From RuneRealm Wiki
Jump to navigation Jump to search

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

--------------------------
-- Module for [[Template:Infobox Var]]
------------------------
local p = {}

local onmain = require('Module:Mainonly').on_main
local commas = require('Module:Addcommas')._add
local infobox = require('Module:Infobox')
VariablesLua = mw.ext.VariablesLua

local smwName = ''

function p._main(args)
	local ret = infobox.new(args)

	ret:defineParams{
		{ name = 'type', func = 'has_content' },
		{ name = 'index', func =  index_arg },
		{ name = 'name', func = { name = name_arg, params = { 'name', 'type', 'index' } } },
		{ name = 'content', func = content_arg },
		{ name = 'raw_content', func = { name = raw_content_arg, params = { 'content' }, flag = { 'p' } } },
		{ name = 'class', func = 'has_content' },
		{ name = 'link', func = { name = create_link, params = { 'type', 'index' } } },
        { name = 'usesinfobox', func = { name = tostring, params = { 'Var' }, flag = 'r' } },
	}

	ret:defineLinks({ hide = true })

	ret:create()
	ret:cleanParams()

	ret:defineName('Infobox Var')
	ret:addClass('infobox-var')

	ret:addRow{
		{ tag = 'argh', content = 'name', class='infobox-header', colspan = '30' }
	}

	:pad(30)
	
	:addRow{
		{ tag = 'th', content = 'Type', colspan = '10' },
		{ tag = 'argd', content = 'type', colspan = '20' }
	}
	
	:addRow{
		{ tag = 'th', content = 'Index', colspan = '10' },
		{ tag = 'argd', content = 'index', colspan = '20' }
	}
	:addRow{
		{ tag = 'th', content = 'Content', colspan = '10' },
		{ tag = 'argd', content = 'raw_content', colspan = '20' }
	}
	:addRow{
		{ tag = 'th', content = 'Class', colspan = '10' },
		{ tag = 'argd', content = 'class', colspan = '20' }
	}
	
	if ret:paramDefined('type') and ret:paramDefined('index') then
		ret:addRow{
			{ tag = 'th', content = 'Link', colspan = '10' },
			{ tag = 'argd', content = 'link', colspan = '20' }
		}
	end

	ret:pad(30)
	
	local content = ret:param('content')
	local raw_content = ret:param('raw_content')
	VariablesLua.vardefine('content_var', content)
	VariablesLua.vardefine('raw_content_var', raw_content)
	local smw_data = {
		content = 'variable_content',
		name = 'variable_name',
		index = 'variable_index',
		usesinfobox = 'Uses infobox',
	}

	ret:useSMWSubobject(smw_data)

	return ret:tostring()..'[[Category:Var]]'
end

function p.main(frame)
	local args = frame:getParent().args
	return p._main(args)
end

function index_arg(ind)
	if infobox.isDefined(ind) then
		return ind or -1
	else
		return -1
	end
end

function raw_content_arg(content)
	if infobox.isDefined(content) then
		return content
	else
		return 'Unknown'
	end
end

function content_arg(content)
	if infobox.isDefined(content) then
		local s, _ = string.gsub(content, '[%[%]]', '')
		return s
	else
		return 'Unknown'
	end
end

-- Create a name if no name param is provided.
-- Defaults to "Var[bit/player] X" if type and index are set.
-- Defaults to pagename minus namespace with /s replaced if either type or index are not set.
function name_arg(name, typ, ind)
	newname = name
	if not infobox.isDefined(name) then
		if infobox.isDefined(typ) and infobox.isDefined(ind) then
			newname = typ .. ' ' .. ind
		else
			newname = string.gsub(mw.title.getCurrentTitle().text, '/', ' ')
		end
	end
	smwName = newname
	return newname
end

function create_link(typ, ind)
	if infobox.isDefined(typ) and infobox.isDefined(ind) then
		return '[https://chisel.weirdgloop.org/varbs/display?' .. string.lower(typ) .. '=' .. ind .. ' Chisel]'
	end
	return ''
end

-- red ERR span with title hover for explanation
function badarg(argname, argmessage)
	return '<span '..
			'title="The parameter «'..argname..'» '..argmessage..'" '..
			'style="color:red; font-weight:bold; cursor:help; border-bottom:1px dotted red;">'..
			'ERR</span>'
end

return p