Module:Sandbox/Frietjes/Rugby
< Module:Sandbox | Frietjes
-- This module may be used to assist with converting ascii tables to a rugby sports table
local p = {}
local non_standard_abbreviations = {
['HullIonians'] = 'ION',
['SheffieldTigers'] = 'TIG'
}
local function abbreviate(s)
s = mw.ustring.gsub(s, '%s', '')
s = non_standard_abbreviations[s] or s
s = mw.ustring.upper(s)
s = mw.ustring.sub(s .. ' ', 1, 3)
return s
end
function p.main(frame)
-- grab the input
local text = '\n' .. (frame.args[1] or '') .. '\n'
-- split into lines
local lines = mw.text.split(text, '[\r\n]')
-- join split lines
for k,v in ipairs(lines) do
if mw.ustring.match(v, '^%s*%d%d*%s*logo%s*') then
lines[k] = ''
if mw.ustring.match(lines[k+1] or '', '^%s*[A-Z][^0-9]*$') then
if mw.ustring.match(lines[k+2] or '', '^%s*[0-9]') then
lines[k+1] = lines[k+1] .. ' ' .. lines[k+2]
lines[k+2] = ''
end
end
end
end
-- 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 = abbreviate(team)
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