Module:Sports roster
Appearance
![]() | This module is rated as beta, and is ready for widespread use. It is still new and should be used with some caution to ensure the results are as expected. |
![]() | This module is subject to page protection. It is a highly visible module in use by a very large number of pages, or is substituted very frequently. Because vandalism or mistakes would affect many pages, and even trivial editing might cause substantial load on the servers, it is protected from editing. |
![]() | This module depends on the following other modules: |
Usage
This module implements {{sports roster}} and {{sports roster/entry}}. Please see {{sports roster}} for usage.
require('Module:No globals')
local p = {}
local getArgs = require('Module:Arguments').getArgs
local tracking = ''
local function addflags(frame, names)
for k,v in ipairs(names) do
if v['nat'] then
names[k]['nat'] = '<span data-sort-value="' .. v['nat'] .. '">'
.. frame:expandTemplate{title = 'flagicon', args = {v['nat']}} .. '</span>'
end
end
return names
end
local function linkschools(frame, names)
for k,v in ipairs(names) do
if v['college'] or v['school'] then
names[k]['college'] = frame:expandTemplate{title = 'college', args = {v['college'] or v['school']}}
end
end
return names
end
local function linknames(names, fmt)
for k,v in ipairs(names) do
local link = v['link'] or v['name'] or ((v['first'] or '') .. ' ' .. (v['last'] or '') .. (v['dab'] and ' (' .. v['dab'] .. ')' or '')) or ''
local text = v['last'] or link
if fmt == 'lf' then
text = v['alt'] or v['name'] or ((v['last'] or '') .. ', ' .. (v['first'] or '')) or ''
elseif fmt == 'fl' or fmt == 'fil' then
if fmt == 'fil' and v['first'] then
v['first'] = string.upper(string.sub(v['first'] .. ' ', 1, 1)) .. '.'
end
text = v['alt'] or v['name'] or ((v['first'] or '') .. ' ' .. (v['last'] or '')) or ''
end
if link:match('^[,%s]*$') then
if text:match('^[,%s]*$') then
text = ''
end
else
if text:match('^[,%s]*$') then
text = '[[' .. link .. ']]'
elseif link == text then
text = '[[' .. link .. ']]'
else
text = '[[' .. link .. '|' .. text .. ']]'
end
end
names[k]['name'] = text
end
return names
end
local function parseEntry(s, keys)
local res = {}
for k,v in pairs(mw.text.split(s, '%s*<[Tt][Dd]%s*')) do
v = mw.ustring.gsub(v, '%s*</[Tt][RrDd]>%s*', '')
if v:find('^.-class%s*=%s*[\'"][^\'"]*sports%-roster%-([A-Za-z]+)%s*[\'"][^>]*>%s*([^%s].-)%s*$') then
local kk = mw.ustring.gsub(v, '^.-class%s*=%s*[\'"][^\'"]*sports%-roster%-([A-Za-z]+)%s*[\'"][^>]*>%s*([^%s].-)%s*$', '%1')
res[kk] = mw.ustring.gsub(v, '^.-class%s*=%s*[\'"][^\'"]*sports%-roster%-([A-Za-z]+)%s*[\'"][^>]*>%s*([^%s].-)%s*$', '%2')
keys[kk] = 1
end
end
return keys, res
end
local function getEntries(args)
local i = 2
local res, keys = {}, {}
local v
while args[i] ~= nil do
keys, v = parseEntry(args[i], keys)
table.insert(res, v)
i = i + 1
end
return res, keys
end
function p.entry(frame)
local args = getArgs(frame)
local res = ''
for k,v in pairs(args) do
if type(k) == 'string' then
res = res .. '<td class="sports-roster-' .. k .. '">' .. v .. '</td>'
end
end
if res ~= '' then
return '<tr>' .. res .. '</tr>'
end
return res
end
function p.roster(frame)
local args = getArgs(frame)
local players, keys = getEntries(mw.text.split(args['players'] or '', '%s*<[Tt][Rr]>%s*'))
local hcoaches, hckeys = getEntries(mw.text.split(args['head_coach'] or '', '%s*<[Tt][Rr]>%s*'))
local acoaches, ackeys = getEntries(mw.text.split(args['asst_coach'] or '', '%s*<[Tt][Rr]>%s*'))
local staff, skeys = getEntries(mw.text.split(args['staff'] or '', '%s*<[Tt][Rr]>%s*'))
local p_style
if args['style'] and mw.title.new('Module:Sports roster/' .. args['style']) then
p_style = require('Module:Sports roster/' .. args['style'])
else
p_style = require('Module:Sports roster/default')
end
-- flags
if keys['nat'] then
players = addflags(frame, players)
end
if hckeys['nat'] then
hcoaches = addflags(frame, hcoaches)
end
if ackeys['nat'] then
acoaches = addflags(frame, acoaches)
end
-- college links
if keys['college'] or keys['school'] then
players = linkschools(frame, players)
end
if hckeys['college'] or hckeys['school'] then
hcoaches = linkschools(frame, hcoaches)
end
if ackeys['college'] or ackeys['school'] then
acoaches = linkschools(frame, acoaches)
end
-- link names
players = linknames(players, 'lf')
hcoaches = linknames(hcoaches, 'fl')
acoaches = linknames(acoaches, 'fl')
local headings = p_style.headings(args, keys)
local res = mw.html.create('table')
res:addClass('toccolours')
:css('font-size', '85%')
:css('margin', '1em auto')
:css('width', '90%')
local row = res:tag('tr')
row:tag('th')
:attr('colspan', 2)
:cssText(args['titlestyle'])
:css('text-align', 'center')
:wikitext(args['title'])
row = res:tag('tr')
:cssText(args['groupstyle'])
:css('text-align', 'center')
row:tag('th'):wikitext('Players')
row:tag('th'):wikitext('Coaches')
row = res:tag('tr')
local innertable = row:tag('td'):css('vertical-align', 'top'):tag('table')
innertable:addClass('sortable')
:css('background', 'transparent')
:css('margin', 0)
:css('width', '100%')
innertable:wikitext(p_style.headings(args, keys))
innertable:wikitext(p_style.players_roster(args, players, keys))
local cell = row:tag('td'):css('vertical-align', 'top')
cell:wikitext(p_style.coaches_roster(hcoaches, acoaches))
cell:wikitext(p_style.staff_roster(args, staff))
local footer = p_style.footer(args, keys)
if footer ~= '' then
row = res:tag('tr')
row:tag('td')
:attr('colspan', 2)
:css('text-align', 'center')
:wikitext(footer)
end
return tostring(res)
end
function p.navbox(frame)
local args = getArgs(frame)
local players, keys = getEntries(mw.text.split(args['players'] or '', '%s*<[Tt][Rr]>%s*'))
local hcoaches, hckeys = getEntries(mw.text.split(args['head_coach'] or '', '%s*<[Tt][Rr]>%s*'))
local acoaches, ackeys = getEntries(mw.text.split(args['asst_coach'] or '', '%s*<[Tt][Rr]>%s*'))
local p_style
if args['style'] and mw.title.new('Module:Sports roster/' .. args['style']) then
p_style = require('Module:Sports roster/' .. args['style'])
else
p_style = require('Module:Sports roster/default')
end
players = linknames(players, 'l')
hcoaches = linknames(hcoaches, 'fil')
acoaches = linknames(acoaches, 'fil')
table.sort(players, function (a, b)
return (tonumber(a['num']) or 9999) < (tonumber(b['num']) or 9999)
or ((tonumber(a['num']) or 9999) == (tonumber(b['num']) or 9999)
and ((a['last'] or 'ZZZZ') < (b['last'] .. 'ZZZZ')))
end
)
local Navbox = require('Module:Navbox')
local targs = {}
targs['name'] = args['name'] or mw.title.getCurrentTitle().text
targs['title'] = p_style.title(args)
targs['titlestyle'] = p_style.titlestyle(args)
targs['listclass'] = 'hlist'
targs['state'] = args['state'] or 'autocollapse'
targs['list1'] = p_style.players_list(args, players, keys) .. '\n' .. p_style.coaches_list(hcoaches, acoaches)
targs['below'] = p_style.below(args, keys)
targs['belowstyle'] = targs['below'] and require(style).belowstyle(args) or nil
return Navbox._navbox(targs) .. tracking
end
return p