A Wiki in the Desert
Log in

Module:Infobox

From A Wiki in the Desert
Revision as of 13:34, 27 May 2021 by Brad (talk | contribs) (Added OpenGraph image hint)

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

local p = {}

function p.start( frame )
	template = frame:getParent()
	infobox = '<table class="infobox ' .. template.args["ibclass"] .. '">\n'
	infobox = infobox .. '<tr>\n'
	infobox = infobox .. '<th class="ibname">'
	infobox = infobox .. template.args["ibname"] .. ''
	infobox = infobox .. '</th>\n'
	infobox = infobox .. '</tr>\n'
	
	if (notempty(template.args["image"])) then
		infobox = infobox .. '<tr>\n'
		infobox = infobox .. '<td align="center" class="ibimage">'
		infobox = infobox .. '[[File:{{#setmainimage:' .. template.args["image"] .. '}}|250px|' .. template.args["ibname"] .. ']]'
		infobox = infobox .. '</td>\n'
		infobox = infobox .. '</tr>\n'
	end
	
	if (notempty(template.args["icon"])) then
		infobox = infobox .. '<tr>\n'
		infobox = infobox .. '<td align="center" class="ibimage">'
		infobox = infobox .. '[[File:' .. template.args["icon"] .. '||' .. template.args["ibname"] .. ']]'
		infobox = infobox .. '</td>\n'
		infobox = infobox .. '</tr>\n'
	end
	
	if (notempty(template.args["ibtype"])) then
		infobox = infobox .. '<tr>\n'
		infobox = infobox .. '<th class="ibtype">'
		infobox = infobox .. '(' .. template.args["ibtype"] .. ')'
		infobox = infobox .. '</th>\n'
		infobox = infobox .. '</tr>'
	end

	return infobox
end

function p.row( frame )
	infobox = '<tr>\n'
	infobox = infobox .. '<th>' .. frame.args[1] .. '</th>\n'
	infobox = infobox .. '</tr>\n'
	infobox = infobox .. '<tr>\n'
	infobox = infobox .. '<td>' .. frame.args[2] .. '</td>\n'
	infobox = infobox .. '</tr>\n'
	
	-----
	
	infobox = '<tr>\n'
	infobox = infobox .. '<td>\n'
	
	infobox = infobox .. '<table class="ib-row">'

	-----

	infobox = infobox .. '<tr>\n'
	infobox = infobox .. '<th>' .. frame.args[1] .. '</th>\n'
	infobox = infobox .. '<td>' .. frame.args[2] .. '</td>\n'
	infobox = infobox .. '</tr>\n'
		
	-----
	
	infobox = infobox .. '</table>\n'
	
	infobox = infobox .. '</td>\n'
	infobox = infobox .. '</tr>\n'
	
	-----
	
	return infobox
end

function p.stacked( frame )
	infobox = '<tr class="ib-stacked">\n'
	infobox = infobox .. '<th>' .. frame.args[1] .. '</th>\n'
	infobox = infobox .. '</tr>\n'
	infobox = infobox .. '<tr class="ib-stacked">\n'
	infobox = infobox .. '<td>' .. frame.args[2] .. '</td>\n'
	infobox = infobox .. '</tr>\n'
	
	return infobox
end

function p.tabulated( frame ) -- Using "tabulated" because the word "table" is a reserved word in Lua... :(
	template = frame:getParent()

	infobox = '<tr>\n'
	infobox = infobox .. '<td>\n'
	
	infobox = infobox .. '<table class="ib-tabulated">'
	infobox = infobox .. '<tr>\n'
	infobox = infobox .. '<th colspan="2" class="ibtype">' .. frame.args[1] .. '</th>\n'
	infobox = infobox .. '</tr>\n'
	
	colOne = {}
	colTwo = {}
	
	for k, v in pairs( template.args ) do
		if (string.find(k, frame.args[2], 1, true)) then
			key = string.sub(k, (#frame.args[2]+1))
			colOne[key] = v
		end
	end
	
	for k, v in pairs( template.args ) do
		if (string.find(k, frame.args[3], 1, true)) then
			key = string.sub(k, (string.len(frame.args[3])+1))
			colTwo[key] = v
		end
	end
	
	myTable = {}

	for k, v in pairs( colOne ) do
		myTable[v] = colTwo[k]
	end
	
	for k,v in spairs(myTable) do
		
		infobox = infobox .. '<tr>\n'
		infobox = infobox .. '<td class="ib-tabulated-left">' .. k .. '</td>\n'
		infobox = infobox .. '<td class="ib-tabulated-right">' .. comma_value(v) .. '</td>\n'
		infobox = infobox .. '</tr>\n'
		
	end
	
	infobox = infobox .. '</table>\n'
	
	infobox = infobox .. '</td>\n'
	infobox = infobox .. '</tr>\n'
	
	return infobox
end

function p.finish( frame )
	infobox = '</table>\n'

	return infobox
end

function notempty( variable )
	if (variable and #variable > 0) then
		return true
	else
		return false
	end
end

function spairs(t, order)
    local keys = {}
    for k in pairs(t) do keys[#keys+1] = k end

    if order then
        table.sort(keys, function(a,b) return order(t, a, b) end)
    else
        table.sort(keys)
    end

    local i = 0
    return function()
        i = i + 1
        if keys[i] then
            return keys[i], t[keys[i]]
        end
    end
end

function comma_value(amount)
	local formatted = amount
	while true do  
		formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", '%1,%2')
		if (k==0) then
		  break
		end
	end
	return formatted
end

return p