Module:Sandbox/Frietjes/Rugby

This is an old revision of this page, as edited by Frietjes (talk | contribs) at 20:56, 7 September 2024. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
-- This module may be used to assist with converting ascii tables to a rugby sports table
local p = {}

function p.main(frame)
	-- grab the input
	local text = '\n' .. (frame.args[1] or '') .. '\n'
	-- clean the input
	text = mw.ustring.gsub('%s*[%d][%d]*%s*logo%s*[\r\n]([A-Z][^0-9\r\n]*)[\r\n]', '%1')
	-- split into lines
	local lines = mw.text.split(text, '[\r\n]')
	-- hash to keep track of team abbreviations
	local abbrevs = {}
	-- strings to keep the result
	local top, mid, bot = '| team_order = ', '', ''
	-- process the input
	local labels = {'', 'win', 'draw', 'loss', 'pf', 'pa', '', 'tb', 'lb', '', 'adjust_points'}
	for k,v in ipairs(lines) do
		if mw.ustring.match(v, '^%s*[A-Z][^0-9]*%s[0-9].-$') then
			-- strip the team name from the line
			local team = mw.ustring.gsub(v, '^%s*([A-Z][^0-9]*)%s[0-9].-$', '%1')
			v = mw.ustring.gsub(v, '^%s*[A-Z][^0-9]*%s([0-9].-)$', '%1')
			-- team abbreviation
			local abbr = mw.ustring.gsub(team, '%s', '')
			abbr = mw.ustring.upper(abbr)
			abbr = mw.ustring.sub(abbr .. '   ', 1, 3)
			if abbrevs[abbr] then
				abbr = abbr .. k
			else
				abbrevs[abbr] = '1'
			end
			top = top .. abbr .. ', '
			bot = bot .. '| name_' .. abbr .. ' = ' .. team .. '\n'
			-- split the remainder
			local stats = mw.text.split(v or 'ERROR', '%s%s*')
			-- format stats
			for j = 1,#labels do
				if (labels[j] and labels[j] ~= '') then
					if labels[j] == 'adjust_points' then
						if (stats[j] and (0 ~= tonumber(stats[j])) ) then
							mid = mid .. '| ' .. labels[j] .. '_' .. abbr .. ' = ' .. (stats[j] or '')
						end
					else
						mid = mid .. '| ' .. labels[j] .. '_' .. abbr .. ' = ' .. (stats[j] or '')
					end
				end
			end
			mid = mid .. '\n'
		end
	end
	top = mw.ustring.gsub(top, ',%s*$', '')
	return '<pre>' .. top .. '\n\n' .. mid .. '\n' .. bot .. '</pre>'
end

return p