Module:Sandbox/Frietjes/Rugby

This is an old revision of this page, as edited by Frietjes (talk | contribs) at 20:08, 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)
	-- split the text into lines
	local text = '\n' .. (frame.args[1] or '') .. '\n'
	local lines = mw.text.split(text, '[\r\n]')
	-- hash to keep track of team abbreviations
	local abbrevs = {}
	-- strings to keep the result
	local top, bot = '', ''
	-- 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.gsub(abbr, '^(...).-*', '%1')
			if abbrevs[abbr] then
				abbr = abbr .. k
			else
				abbrevs[abbr] = '1'
			end
			bot = bot .. '| name_' .. abbr .. ' = ' .. team .. '\n'
			-- split the remainder
			local stats = mw.ustring.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' and stats[j] and stats[j] ~= '0') then
						top = top .. '| ' .. labels[j] .. ' = ' .. (stats[j] or '')
					else
						top = top .. '| ' .. labels[j] .. ' = ' .. (stats[j] or '')
					end
				end
			end
			top = top .. '\n'
		end
	end
	return top .. '\n' .. bot
end

return p