Jump to content

Module:State tournaments box

From Wiki - Scioly.org

Documentation for this module may be created at Module:State tournaments box/doc

local p = {};
local getArgs = require('Module:Arguments').getArgs
local stringMod = require('Module:String')

local columns = require("Module:State tournaments box/common").columns
local version_key = "version"

function p.main(frame)
    local args = getArgs(frame)
    if args[version_key] == "2" then
    	local html_table = mw.html.create("table")
    	html_table:attr("class", "wikitable")
    	html_table:attr("id", "state-tournaments-box")
    	
    	local header_row = mw.html.create("tr")
    	for _, column in ipairs(columns) do
    		local header = mw.html.create("th")
    		header:wikitext(column.display)
    		header_row:node(header)
    	end
    	html_table:node(header_row)
    	
    	for _,v in ipairs(args) do
    		html_table:node(v)	
    	end
    	
    	return html_table 
    end
    -- Legacy code To be deprecated and removed once all state pages and state
    -- tournament boxes have moved to version 2
    mw.addWarning("Detected use of legacy <code>State tournaments box</code>." .. 
    	" [[Template:State tournaments box#Migrating_Legacy_to_v2|Please migrate this template to version 2.]]")
    table = "<table class='wikitable' id='state-tournaments-box'>"
    header = "<tr><th>Name</th><th>Type</th><th>Date</th><th>Division</th><th>Format</th></tr>"
    table = table .. header
    finalStr = ""
    maxRow = 1
    for k, v in pairs(args) do
        num = tonumber(string.match(k, '%d+'))
        if num > maxRow then
            maxRow = num
        end
    end
    for i = 1,maxRow do
        table = table .. "<tr>"
        terms = {"name", "type", "date", "div", "format"}
        for f = 1,#terms do
            found = false
            for k, v in pairs(args) do
                if string.find(k, terms[f]) ~= nil and string.match(k, '%d+') == tostring(i) then
                    table = table .. "<td>" .. v .. "</td>"
                    found = true
                    break
                end
            end
            if not found then
                table = table .. "<td></td>"
            end
        end
        table = table .. "</tr>"
    end
    table = table .. "</table>"
    return table
end

return p;