Difference between revisions of "Module:Gridiron color/sandbox"
Jump to navigation
Jump to search
blackwiki>Ahecht (debug) |
blackwiki>Ahecht (simplify) |
||
| Line 20: | Line 20: | ||
"color: " | "color: " | ||
} | } | ||
| − | |||
| − | |||
| − | |||
| − | |||
local function get_year(colors, year) | local function get_year(colors, year) | ||
| Line 41: | Line 37: | ||
local function get_colors(team, unknown, year) | local function get_colors(team, unknown, year) | ||
| − | team = | + | team = (team or ''):match("^%s*(.-)%s*$") |
year = tonumber(year) | year = tonumber(year) | ||
unknown = unknown or color_data["#default"] or {"#DCDCDC", "", "", "", ""} | unknown = unknown or color_data["#default"] or {"#DCDCDC", "", "", "", ""} | ||
| Line 58: | Line 54: | ||
if mw.ustring.find(team, "%d?%d?%d%dthru%d?%d?%d%d$") then | if mw.ustring.find(team, "%d?%d?%d%dthru%d?%d?%d%d$") then | ||
if (not year or year <= 0) then | if (not year or year <= 0) then | ||
| − | team, year = mw.ustring.match(team, "^(.-) | + | team, year = mw.ustring.match(team, "^(.-)(%d?%d?%d%d)thru%d?%d?%d%d$") |
| − | year = tonumber(year) | + | team, year = team:match("^%s*(.-)%s*$"), tonumber(year) |
if year >= 20 and year < 100 then -- Two-digit years were deprecated in 2018 | if year >= 20 and year < 100 then -- Two-digit years were deprecated in 2018 | ||
| Line 88: | Line 84: | ||
return colors or unknown | return colors or unknown | ||
end | end | ||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
local function bordercss(c, w) | local function bordercss(c, w) | ||
| Line 128: | Line 112: | ||
local args = frame.args.team and frame.args or frame:getParent().args | local args = frame.args.team and frame.args or frame:getParent().args | ||
local s = p.color(frame, 1) .. "; " .. p.color(frame, 2) .. "; " | local s = p.color(frame, 1) .. "; " .. p.color(frame, 2) .. "; " | ||
| − | if | + | if tonumber(args.border) or yesno(args.border) then |
| − | + | args.border =(tonumber(args.border) and (tonumber(args.border) > 0)) and (tostring(args.border) .. "px") or "2px" | |
| − | s = s .. bordercss(p.color(frame, 3), border) | + | s = s .. bordercss(p.color(frame, 3), args.border) |
end | end | ||
return s | return s | ||
Revision as of 00:23, 8 June 2018
Documentation for this module may be created at Module:Gridiron color/sandbox/doc
--
-- This module implements
-- {{Gridiron primary color}} -- {{Gridiron primary color raw}} -- {{Gridiron primary style}}
-- {{Gridiron secondary color}} -- {{Gridiron secondary color raw}}
-- {{Gridiron tertiary color raw}}
-- {{Gridiron alt primary color}} -- {{Gridiron alt secondary color}}
--
local p = {}
local color_data = mw.loadData("Module:Gridiron color/data")
local yesno = require('Module:Yesno')
local prefixes = {
"background: ",
"color: ",
"",
"background: ",
"color: "
}
local function get_year(colors, year)
if colors and colors[6] and type(colors[6] == 'table') then
for team, year_colors in pairs(colors[6]) do
if mw.ustring.find(team, "%d%d%d%dthru%d%d%d%d$") then
local start_year, end_year = mw.ustring.match(team, "(%d%d%d%d)thru(%d%d%d%d)$")
if (tonumber(start_year) <= tonumber(year)) and (tonumber(year) <= tonumber(end_year)) then
return year_colors
end
end
end
end
return colors
end
local function get_colors(team, unknown, year)
team = (team or ''):match("^%s*(.-)%s*$")
year = tonumber(year)
unknown = unknown or color_data["#default"] or {"#DCDCDC", "", "", "", ""}
local use_default = {
[""] = 1,
["retired"] = 1,
["free agent"] = 1,
}
local colors = nil
if ( team and use_default[team:lower()] ) then
colors = unknown
else
if mw.ustring.find(team, "%d?%d?%d%dthru%d?%d?%d%d$") then
if (not year or year <= 0) then
team, year = mw.ustring.match(team, "^(.-)(%d?%d?%d%d)thru%d?%d?%d%d$")
team, year = team:match("^%s*(.-)%s*$"), tonumber(year)
if year >= 20 and year < 100 then -- Two-digit years were deprecated in 2018
year = year + 1900
elseif year < 20 then
year = year + 2000
elseif year < 1000 then
year = nil
end
else
team = mw.ustring.match(team, "^(.-) -%d?%d?%d%dthru%d?%d?%d%d$")
end
end
if year and year > 0 then
--code for handling year parameter
colors = get_year(color_data[team], year)
else
colors = color_data[team]
end
if ( colors and type(colors) == 'string' ) then
-- follow alias recursively
return get_colors (colors, unknown, year)
end
end
return colors or unknown
end
local function bordercss(c, w)
local s = 'inset ' .. w .. 'px ' .. w .. 'px 0 ' .. c
.. ', inset -' .. w .. 'px -' .. w .. 'px 0 ' .. c
return '-moz-box-shadow: ' .. s .. '; -webkit-box-shadow: ' .. s .. '; box-shadow: ' .. s .. ';'
end
function p.test(frame)
local args = frame.args.team and frame.args or frame:getParent().args
local colors = get_colors((args.team or args[1]), nil, args.year)
return '["' .. args.team .. '"] = {{ "' .. colors[1] .. '", '.. colors[2] ..
'", '.. colors[3] .. '", '.. colors[4] .. '", '.. colors[5] .. '"}}'
end
function p.color(frame, column)
column = (column or tonumber(frame.args.column)) or 1
local args = frame.args.team and frame.args or frame:getParent().args
local colors = get_colors((args.team or args[1]), nil, args.year)
if ((not colors[column]) or (colors[column] == '')) and tonumber(frame.args.altcolumn) then
column = tonumber(frame.args.altcolumn)
end
return (yesno(frame.args.raw) and "" or prefixes[column]) ..
mw.ustring.gsub(colors[column],"#",frame:extensionTag('nowiki', '#'))
end
function p.style(frame)
local args = frame.args.team and frame.args or frame:getParent().args
local s = p.color(frame, 1) .. "; " .. p.color(frame, 2) .. "; "
if tonumber(args.border) or yesno(args.border) then
args.border =(tonumber(args.border) and (tonumber(args.border) > 0)) and (tostring(args.border) .. "px") or "2px"
s = s .. bordercss(p.color(frame, 3), args.border)
end
return s
end
return p