Module:Sports color/sandbox: Difference between revisions
Appearance
No edit summary |
m Frietjes moved page Module:Basketball color/sandbox to Module:Sports color/sandbox without leaving a redirect: per TfD |
||
(No difference)
|
Revision as of 14:16, 8 July 2018
![]() | This is the module sandbox page for Module:Sports color (diff). |
![]() | This module is rated as ready for general use. It has reached a mature form and is thought to be relatively bug-free and ready for use wherever appropriate. It is ready to mention on help pages and other Wikipedia resources as an option for new users to learn. To reduce server load and bad output, it should be improved by sandbox testing rather than repeated trial-and-error editing. |
![]() | 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 Lua module is used on approximately 72,000 pages and changes may be widely noticed. Test changes in the module's /sandbox or /testcases subpages, or in your own module sandbox. Consider discussing changes on the talk page before implementing them. |
This module is used by
- {{Basketball color}}, {{Basketball color cell}}, {{Basketball color cell2}}
- {{Infobox basketball biography/style}}
- {{Baseball color}}
- {{Baseball primary style}}, {{Baseball secondary style}}
- {{Infobox MLB}}, {{Infobox baseball biography}}
- {{Navbox ice hockey}}
- {{Handball color}}, {{Handball color cell}}, {{Handball color cell2}}
- {{Esports color}}
Usage
{{#invoke:Sports color|color}}
in any of the various color templates{{#invoke:Sports color|colorcell}}
in any of the various color cell templates{{#invoke:Sports color|colorcell2}}
in any of the various color cell2 templates{{#invoke:Sports color|check}}
to check to see if a team has defined colors
Updating team colors
See:
- Module:Sports color/baseball
- Module:Sports color/basketball
- Module:Sports color/esports
- Module:Sports color/handball
- Module:Sports color/ice hockey
- Module:Sports color/unisport
- Module:Sports color/volleyball
Check
{{#invoke:Sports color|check}}
to check to see if a team has defined colors
--
-- This module implements
-- {{Basketball leagues color}}, {{Basketball color cell}}, {{Basketball color cell2}},
-- {{NBA color}}, {{NBA color cell}}, {{NBA color cell2}}
-- {{Euroleague color}}, {{Euroleague color cell}}, {{Euroleague color cell2}},
-- {{NBL colour}}, {{NBL colour cell}}, {{NBL colour cell2}},
-- {{PBA color}}, {{PBA color cell}}, {{PBA color cell2}},
-- {{KBL color}}, {{KBL color cell}}, {{KBL color cell2}}
--
local p = {}
local data_module = "Module:Basketball color/data/sandbox"
local function stripwhitespace(text)
return text:match("^%s*(.-)%s*$")
end
local function get_colors(team, unknown)
team = stripwhitespace(team or '')
unknown = unknown or {"DCDCDC", "000000", "000000", "FFFFFF"}
local use_default = {
[""] = 1,
["retired"] = 1,
["free agent"] = 1,
}
local colors = nil
if ( team and use_default[team:lower()] ) then
colors = {"DCDCDC", "000000", "000000", "FFFFFF"}
else
local all_colors = mw.loadData(data_module)
colors = all_colors[team]
if ( colors and type(colors) == 'string' ) then
colors = all_colors[colors]
end
end
return colors or unknown
end
local function team_color(team, num)
local colors = get_colors(team, nil)
num = tonumber(num:match('[1-4]') or '0')
if ( num ) then
return colors[num]
else
return ''
end
end
local function team_colorcell(team, borderwidth, bg, fg, bd)
local colors = get_colors(team, nil)
local border = ''
borderwidth = borderwidth or ''
if (borderwidth ~= '') then
border = 'border:' .. borderwidth .. 'px solid #' .. stripwhitespace(colors[bd]) .. ';'
end
return 'background-color:#' .. stripwhitespace(colors[bg]) .. ';color:#' .. stripwhitespace(colors[fg]) .. ';' .. border
end
local function team_check(team, unknown)
local colors = get_colors(team, unknown)
if type(colors) == 'table' then
return 'known'
else
return unknown
end
end
local function team_testtable(args, style, data)
local teamlist = {}
if ( data ) then data_module = data end
-- helper function
local function table_row(t, c)
local res = mw.html.create('')
if( c[1] and c[2] and c[3] and c[4] ) then
res:tag('td'):wikitext(t)
else
res:tag('td'):wikitext(t .. ' <span class=error>ERROR</span>')
end
for i=1,4 do
res:tag('td'):css('background-color', '#' .. stripwhitespace(c[i] or 'ERROR'))
end
return tostring(res)
end
-- list of teams
if( args and args[1] ) then
for k, team in pairs(args) do
if type(k) == 'number' then
table.insert(teamlist, team)
end
end
else
local all_colors = mw.loadData(data_module)
for team, colors in pairs( all_colors ) do
table.insert(teamlist, team)
end
table.sort(teamlist)
table.insert(teamlist, 'Free agent')
table.insert(teamlist, 'Retired')
end
-- build table
local root = mw.html.create('table')
root:addClass('wikitable sortable')
:css('font-size', '90%')
:css('line-height', '100%')
:cssText(style)
local row = root:tag('tr')
row:tag('th'):wikitext('Team')
for i=1,4 do
row:tag('th')
:addClass('unsortable')
:wikitext(i)
end
for k, team in pairs( teamlist ) do
row = root:tag('tr')
row:wikitext(table_row(team, get_colors(team)))
end
return tostring(root)
end
function p.color(frame)
local args = (frame.args[1] ~= nil) and frame.args or frame:getParent().args
return team_color(args[1] or '', args[2] or '')
end
function p.colorcell(frame)
local args = (frame.args[1] ~= nil) and frame.args or frame:getParent().args
return team_colorcell(args[1] or '', args['border'] or '', 1, 2, 3)
end
function p.colorcell2(frame)
local args = (frame.args[1] ~= nil) and frame.args or frame:getParent().args
return team_colorcell(args[1] or '', args['border'] or '', 3, 4, 1)
end
function p.check(frame)
local args = (frame.args[1] ~= nil) and frame.args or frame:getParent().args
return team_check(args[1] or '', args[2] or '')
end
function p.testtable(frame)
return team_testtable((frame.args[1] ~= nil) and frame.args or frame:getParent().args, frame.args['style'] or '', frame.args['data'])
end
return p