Jump to content

Module:PHL sports team/testtable

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by McVahl (talk | contribs) at 00:50, 12 March 2020 (Undid revision 945133066 by McVahl (talk)). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
local p = {}
local div = { 'short', 'men', 'women', 'boys', 'girls', 'name' }
local mod = 'PHL sports team'

local function labels()
	local lbls = {}
	for k, text in pairs(div) do
		local lbl = mw.html.create(''):tag('span'):css('display', 'inline-block'):css('width', '30px'):cssText(style).wikitext(text)
		table.insert(lbls, tostring(lbl))
	end
	return lbls
end

local function table_row_all(frame, l, t, c)
	local res = mw.html.create('')
	
	local fname = frame:preprocess(string.format('{{#invoke:%s|name|%s|short}}<br>', mod, t))
	local frt = frame:preprocess(string.format('{{#invoke:%s|rt|%s|short}}<br>', mod, t))
	local fmain = frame:preprocess(string.format('{{#invoke:%s|main|%s|short}}<br>', mod, t))
	
	for i=3,6 do
		fmain = fmain .. frame:preprocess(string.format('{{#invoke:%s|main|%s|%s}}<br>', mod, t, i))
		frt = frt .. frame:preprocess(string.format('{{#invoke:%s|rt|%s|%s}}<br>', mod, t, i))
		fname = fname .. frame:preprocess(string.format('{{#invoke:%s|name|%s|%s}}<br>', mod, t, i))
	end
	
	fname = fname .. frame:preprocess(string.format('{{#invoke:%s|name|%s|name=Custom Team Name}}<br>', mod, t))
	frt = frt .. frame:preprocess(string.format('{{#invoke:%s|rt|%s|name=Custom Team Name}}<br>', mod, t))
	fmain = fmain .. frame:preprocess(string.format('{{#invoke:%s|main|%s|name=Custom Team Name}}', mod, t))
	
	res:tag('td'):wikitext(t)
	res:tag('td'):attr('align', 'center'):wikitext(c[1] or '—')
	res:tag('td'):wikitext(frame:preprocess(string.format('{{#invoke:%s|name|%s|2}}', mod, t)))
	res:tag('td'):wikitext(fmain or '—')
	res:tag('td'):attr('align', 'right'):wikitext(frt or '—')
	res:tag('td'):wikitext(fname or '—')
	return tostring(res)
end

local function get_row_tname(root, row, align, text, tag)
	local yesno = require('Module:Yesno')
	if yesno(tag or 'y') then
		row = root:tag('tr')
	end
	
	row:tag('td'):attr('align', align):wikitext(text)
	row:tag('td'):attr('align', 'left'):wikitext('<code><nowiki>' .. text .. '</nowiki></code>')
	return row
end

local function table_row_tname(frame, l, t, c)
	local tname = frame.args['tname'] or 'UAAPteam'
	local align = frame.args['align'] or 'left'
	local rdef = string.format('{{%s|%s}}', tname, t)
	local rwom = string.format('{{%s|%s|women}}', tname, t)
	local rjnr = string.format('{{%s|%s|junior}}', tname, t)
	local rgrl = string.format('{{%s|%s|girls}}', tname, t)
	local rins = string.format('{{%s|%s|inst}}', tname, t)
	local rsrt = string.format('{{%s|%s|short}}', tname, t)
	local rcst = string.format('{{%s|%s|name=Custom Team Name}}', tname, t)
	
	local root = mw.html.create('')
	local row = root:tag('tr')
	
	row:tag('td'):css('border-bottom', '2px solid black'):cssText('style'):attr('rowspan',7):wikitext(t)
	row:tag('td'):css('border-bottom', '2px solid black'):cssText('style'):attr('rowspan',7):attr('align', 'center'):wikitext(c[1] or '—')
	row = get_row_tname(root, row, align, rdef, 'n')
	row = get_row_tname(root, row, align, rwom)
	row = get_row_tname(root, row, align, rjnr)
	row = get_row_tname(root, row, align, rgrl)
	row = get_row_tname(root, row, align, rins)
	row = get_row_tname(root, row, align, rsrt)
	row = get_row_tname(root, row, align, rcst)
	row:css('border-bottom', '2px solid black'):cssText('style')
	return tostring(root)
end

function p._testtable(frame)
	-- build table
	local root = mw.html.create('table')
	local temp = {}
	
	root:addClass('wikitable sortable')
		:css('background', 'transparent')
		:css('font-size', '90%')
	
	local row = root:tag('tr')
	local tname = frame.args['tname']
	
	if (tname == nil) then
		root:css('line-height', '100%')
		row:tag('th'):attr('rowspan',2):wikitext('Team')
		row:tag('th'):attr('rowspan',2):wikitext('Code')
		row:tag('th'):attr('rowspan',2):wikitext('School')
		row:tag('th'):wikitext('Left-sided icon')
		row:tag('th'):wikitext('Right-sided icon')
		row:tag('th'):wikitext('No icon')
		row = root:tag('tr')
		row:tag('th'):wikitext('(Function: \'\'main\'\')')
		row:tag('th'):wikitext('(Function: \'\'rt\'\')')
		row:tag('th'):wikitext('(Function: \'\'name\'\')')
	else
		row:tag('th'):wikitext('Team')
		row:tag('th'):wikitext('Code')
		row:tag('th'):wikitext('Output')
		row:tag('th'):wikitext('Usage')
	end
	
	root:cssText(style)
	
	local args = (frame.args[1] ~= nil) and frame.args or frame:getParent().args
	local data_module = string.format('Module:%s/%s', mod, args['level'] or 'collegiate')
	local data = mw.loadData(data_module)
	
	for k, teams in pairs(data) do
		table.insert(temp, k)
	end
	
	table.sort(temp)
	table.insert(temp, '{{{team1}}}')
	table.insert(temp, 'XYZ')

	-- local lbls = labels()
	for k, team in pairs(temp) do
		row = root:tag('tr')
		if tname == nil then
			row:wikitext(table_row_all(frame, lbls, team, data[team] or ''))
		else
			row:wikitext(table_row_tname(frame, lbls, team, data[team] or ''))
		end
	end

	return tostring(root)
end

function p.testtable(frame)
	return frame:preprocess(p._testtable(frame))
end

return p