A Wiki in the Desert
Log in

Module:Infobox

From A Wiki in the Desert
Revision as of 11:39, 21 November 2018 by Brad (talk | contribs) (A new method of implementing more advanced Infoboxes using the power of Lua)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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:' .. template.args["image"] .. '|250px|' .. 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.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] = tonumber(colTwo[k])
	end
	
	for k,v in spairs(myTable, function(t,a,b) return t[b] < t[a] end) 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