词条 | Module:Sports rbr table/sandbox |
释义 | -- This module implements {{Sports rbr table}} local p = {} -- Internationalisationlocal labels = { teamround = 'Team ╲ Round', source = 'Source:', notes = 'Notes:', matches = 'match(es)', updatedto = 'Updated to firstplayed = 'First futuredate = '?', complete = 'complete', future = 'future', grnd = 'Ground', res = 'Result', pos = 'Position' } local templatestyles = 'Sports rbr table/styles.css' local args = nil local preview, tracking = , local hasnotes = false local colorlist = {} local textlist = {} local color_map = { green1='#BBF3BB', green2='#CCF9CC', green3='#DDFCDD', green4='#EEFFEE', blue1='#BBF3FF', blue2='#CCF9FF', blue3='#DDFCFF', blue4='#EEFFFF', yellow1='#FFFFBB', yellow2='#FFFFCC', yellow3='#FFFFDD', yellow4='#FFFFEE', red1='#FFBBBB', red2='#FFCCCC', red3='#FFDDDD', red4='#FFEEEE', black1='#BBBBBB', black2='#CCCCCC', black3='#DDDDDD', black4='#EEEEEE', ['1st']='#FFD700', ['2nd']='#C0C0C0', ['3rd']='#CC9966' } local legend_symbols = {O='W/O'} local legend_order_default = {'A', 'H', 'N', 'B', 'W', 'D', 'L', 'Ab', 'P', 'O'} local function isnotempty(s) return s and s:match( '^%s*(.-)%s*$' ) ~= end local function zeropad(n) if n>=0 and n < 10 then return '00' .. n end if n>=0 and n < 100 then return '0' .. n end return .. n end local function pad_key(k) -- Zero pad, fix ranges and dashes if k then k = k .. ' ' k = mw.ustring.gsub(k, '–', '-') k = mw.ustring.gsub(k, '_([%d][^%d])', '_0%1') k = mw.ustring.gsub(k, '%-([%d][^%d])', '-0%1') k = mw.ustring.gsub(k, '_([%d][%d][^%d])', '_0%1') k = mw.ustring.gsub(k, '%-([%d][%d][^%d])', '-0%1') k = mw.ustring.gsub(k, '([^%d])%-([%d])', '%1000-%2') k = mw.ustring.gsub(k, '([%d])%-%s*$', '%1-999') k = mw.ustring.gsub(k, '^%s*(.-)%s*$', '%1') end return k end local function matches_date(text, m, d) return mw.ustring.gsub(mw.ustring.gsub(text .. , ' end local function escapetag(text) return mw.ustring.gsub(text, '', ' end local function unescapetag(text) return mw.ustring.gsub(text, ' end local function get_color(p) if p then p = mw.ustring.gsub(p, '?[Aa][Bb][Bb][Rr][^<>]*>', ) p = mw.ustring.gsub(p, '<[Ss][Uu][Pp]>[^<>]*[Ss][Uu][Pp]>', ) p = mw.ustring.gsub(p, '?[Ss][^<>]*>', ) p = mw.ustring.gsub(p, '†%s*$', ) p = mw.ustring.gsub(p, '%[%[[^%[%]|]*|([^%[%]|]*)%]%]', '%1') if p:match('^%a%a*$') then if args['text_' .. p] == nil then tracking = tracking .. end end end local c = colorlist[p] or colorlist[zeropad(tonumber(p) or -1)] if c then return color_map[c] or c end p = tonumber(p or '0') or 0 if p <= 0 then return nil end -- ranges in order of specificity local offset1, offset2 = 999, 999 for k,v in pairs( colorlist ) do local r1 = tostring(k):match( '^%s*([%d]+)%-[%d]+%s*$' ) local r2 = tostring(k):match( '^%s*[%d]+%-([%d]+)%s*$' ) if r1 and r2 then r1 = tonumber(r1) r2 = tonumber(r2) if (r1 <= p) and (r2 >= p) then if (c == nil) or ((p - r1) <= offset1 and (r2 - p) <= offset2) then c = color_map[v] or v offset1 = p - r1 offset2 = r2 - p end end end end return c end local function check_arg(k) k = tostring(k) or if k == 'firstround' or k == 'sortable' or k == 'updated' or k == 'update' or k =='source' or k =='notes' or k == 'legendpos' or k == 'date' or k == 'header' or k == 'title' or k == 'start_date' or k == 'toptext' then elseif k == 'legendorder' then tracking = tracking .. elseif k:match( '^text_?(.-)%s*$' ) then elseif k:match( '^colou?r_?(.-)%s*$' ) then elseif k:match( '^team[%d]+%s*$' ) then elseif k:match( '^label[%d]+%s*$' ) then if args['header'] then else tracking = tracking .. end elseif k:match( '^pos[%d]+%s*$' ) then elseif k:match( '^grnd[%d]+%s*$' ) then elseif k:match( '^res[%d]+%s*$' ) then elseif k:match( '^split[%d]+%s*$' ) then elseif k == 'rnd1' then tracking = tracking .. elseif k:match( '^rnd[%d]+%s*$' ) then elseif k:match( '^pos_' ) then elseif k:match( '^grnd_' ) then elseif k:match( '^res_' ) then elseif k:match( '^name_' ) then elseif k:match( '^note_' ) then elseif k:match( '^pos[%d]+_rnd[%d]+_note%s*$' ) then elseif k:match( '^grnd[%d]+_rnd[%d]+_note%s*$' ) then elseif k:match( '^res[%d]+_rnd[%d]+_note%s*$' ) then else local vlen = mw.ustring.len(k) k = mw.ustring.sub(k, 1, (vlen < 25) and vlen or 25) k = mw.ustring.gsub(k, '[^%w\\-_ ]', '?') preview = preview .. 'Unknown: "' .. k .. '" tracking = tracking .. end end local function filterargs(args) local pfx = args['showstyle'] or local ppars = {'source', 'notes', 'legendpos', 'header', 'title', 'toptext'} local jpars = {'source', 'notes', 'toptext'} local newargs = {} if pfx == 'pos' or pfx == 'grnd' or pfx == 'res' then for k, v in pairs( args ) do k = tostring(k) or if k:match('^pos[_%d]') or k:match('^grnd[_%d]') or k:match('^res[_%d]') then if not k:match('^' .. pfx) then args[k] = nil end end for j,key in ipairs({'grnd', 'res', 'pos'}) do if pfx == key then if k:match('^colou?r_' .. pfx) or k:match('^text_' .. pfx) then local p = mw.ustring.gsub(k, '^([a-z]*)_' .. pfx, '%1') newargs[p] = args[k] args[k] = nil end else if k:match('^colou?r_' .. key) or k:match('^text_' .. key) then args[k] = nil end end end for i, p in ipairs(ppars) do if (k == p .. '_' .. pfx) then newargs[p] = v args[k] = nil end end end else for k, v in pairs( args ) do k = tostring(k) or for j,key in ipairs({'grnd', 'res', 'pos'}) do if args[k] and (k:match('^colou?r_' .. key) or k:match('^text_' .. key)) then local p = mw.ustring.gsub(k, '^([a-z]*)_' .. key, '%1') newargs[p] = v args[k] = nil end for i, p in ipairs(jpars) do if (k == p .. '_' .. key) then if args[k] and newargs[p] then newargs[p] = newargs[p] .. ' elseif args[k] and args[p] then newargs[p] = args[p] .. ' elseif args[k] then newargs[p] = v end args[k] = nil end end end end end key = args['showteam'] or if key ~= then for k, v in pairs( args ) do k = tostring(k) or if k:match('^team[%d]+$') and v ~= key then args[k] = nil end end end for k, v in pairs( newargs ) do args[k] = v end return args end function p.table(frame) local getArgs = require('Module:Arguments').getArgs local yesno = require('Module:Yesno') args = filterargs(getArgs(frame)) local rounds = tonumber(args['rounds'] or '0') or 0 local firstround = tonumber(args['firstround'] or 1) or 1 local sortable = yesno(args['sortable'] or 'no') local updated = args['updated'] or args['update'] local source = args['source'] local notes = args['notes'] local delimiter = args['delimiter'] or '/' local addlegend = nil local legendpos = (args['legendpos'] or 'tr'):lower() local header, footer, prenotes = , , -- Lowercase two labels -- labels['complete'] = string.lower(labels['complete']) labels['future'] = string.lower(labels['future']) -- Adjust rounds rounds = rounds - (firstround - 1) -- Tracking if updated and updated:match(' %d%d%d%d$') then local YY = mw.ustring.gsub(updated, '^.*(%d%d)$', '%1') local pn = frame:getParent():getTitle() or if pn:match('^User:') or pn:match('^User talk:') or pn:match('^Draft:') or pn:match('^Talk:') then else if pn:match('%d%d' .. YY) or pn:match('[–%-]' .. YY) then else tracking = tracking .. end end -- Require a source if source == nil and args['date'] then source = frame:expandTemplate{ title = 'citation needed', args = { reason='No source parameter defined', date=date or os.date('%B %Y') } } elseif source and source:match('[^%[]#') then if source:match('eason#') or source:match('%d%d#') then tracking = tracking .. elseif source:match('^[Hh][Tt][Tt][Pp]') then tracking = tracking .. end end -- Add placeholders for missing labels for k, v in pairs( args ) do k = tostring(k) or if k:match( '^pos[%d]+%s*$' ) or k:match( '^grnd[%d]+%s*$' ) or k:match( '^res[%d]+%s*$' ) then local i = tonumber(k:match('^[a-z]+([%d]+)%s*$' )) if i > 0 and args['label' .. i] == nil and args['team' .. i] == nil then args['label' .. i] = ' end end end -- Process team, pos, and color args local team_list = {} local maxrounds = 0 local rowlength = {} local needprefix = 0 for k, v in pairs( args ) do check_arg(k) -- Preprocess ranges if tostring(k):match( '^text_?(.-)%s*$' ) then k = pad_key(k) end if tostring(k):match( '^colou?r_?(.-)%s*$' ) then k = pad_key(k) end -- Create the list of teams and count rounds local i = tonumber( tostring(k):match( '^team([%d]+)%s*$' ) or tostring(k):match( '^label([%d]+)%s*$' ) or '0' ) if ( i > 0 and isnotempty(v) ) then if args['label' .. i] == ' args['label' .. i] = nil end table.insert( team_list, i) local mcount = 0 for j,pfx in ipairs({'grnd', 'res', 'pos'}) do local p = args[pfx .. i] or if args['name_' .. v] then local t = args['team' .. i] or args['label' .. i] or p = args[pfx .. '_' .. t] or end if p ~= then local pos = mw.text.split(escapetag(p), '%s*' .. delimiter .. '%s*') table.insert(rowlength, #pos) maxrounds = (#pos > maxrounds) and #pos or maxrounds mcount = mcount + 1 end end if mcount > 1 then needprefix = needprefix + 1 end end -- Create the list of colors local s = tostring(k):match( '^colou?r_?(.-)%s*$' ) if ( s and isnotempty(v) ) then colorlist[s] = v:lower() end -- Check if we are adding a legend s = tostring(k):match( '^text_?(.-)%s*$' ) if ( s and isnotempty(v) ) then textlist[s] = v addlegend = 1 end end maxrounds = (rounds > maxrounds) and rounds or maxrounds table.sort(rowlength) for k=2,#rowlength do if rowlength[k] ~= rowlength[k-1] then tracking = tracking .. end end -- sort the teams table.sort(team_list) local fs = 95 if ((maxrounds - firstround) > 37 ) then fs = fs - 2*(maxrounds - firstround - 37) fs = (fs < 80) and 80 or fs end -- Build the table local root = mw.html.create('table') root:addClass('wikitable') root:addClass(sortable and 'sortable' or nil) root:addClass('sportsrbrtable') root:css('font-size', fs .. '%') if args['title'] then root:tag('caption'):wikitext(args['title']) end local navbar = if args['template_name'] then navbar = ' -- remove the next part if https://en.wikipedia.org/w/index.php?oldid=832717047#Sortable_link_disables_navbar_links? -- is ever fixed if sortable then navbar = mw.ustring.gsub(navbar, '<%/?abbr[^<>]*>', ' ') end end -- Heading row local row = root:tag('tr') row:tag('th') :attr('rowspan', args['sortable'] and 2 or nil) :wikitext((args['header'] or labels['teamround']) .. navbar) for r=1,maxrounds do row:tag('th') :addClass(args['sortable'] and 'sportsrbrtable-rnd-sort' or 'sportsrbrtable-rnd') :attr('scope', 'col') :wikitext(args['rnd' .. (r + (firstround - 1))] or (r + (firstround - 1))) end if args['sortable'] then row = root:tag('tr') for r=1,maxrounds do row:tag('th') :addClass('sportsrbrtable-rnd-toggle') end end -- Team positions for k=1,#team_list do local i = team_list[k] -- Which rows to show? local showrow = {} for j,pfx in ipairs({'grnd', 'res', 'pos'}) do local t = args['team' .. i] or if args[pfx .. i] then table.insert(showrow, pfx) elseif args['name_' .. t] and args[pfx .. '_' .. t] then table.insert(showrow, pfx) end end for j,pfx in ipairs(showrow) do local t = args['team' .. i] or args['label' .. i] or local p = args[pfx .. i] or local n = args['note' .. i] or local efnname = 'note' .. i if args['name_' .. t] then p = args[pfx .. '_' .. t] or n = args['note_' .. t] or efnname = 'note' .. t t = args['name_' .. t] end local lbl = t if needprefix > 1 then lbl = lbl .. ': ' .. labels[pfx] elseif lbl == or needprefix == 1 then lbl = labels[pfx] end if n ~= then if args['note_' .. n] then n = frame:expandTemplate{ title = 'efn', args = { name='note' .. n, } } else n = frame:expandTemplate{ title = 'efn', args = { name=efnname, n} } end hasnotes = true end row = root:tag('tr') row:tag('th') :addClass(args['team' .. i] and 'sportsrbrtable-team' or 'sportsrbrtable-lbl') :css('text-align', args['labelalign']) :attr('scope', 'row') :wikitext(mw.ustring.gsub(lbl,'^%s*%-%s*$', ' ') .. n) if t:match('<%s*[Cc][Ee][Nn][Tt][Ee][Rr]%s*>') then tracking = tracking .. end local pos = mw.text.split(escapetag(p), '%s*' .. delimiter .. '%s*') for r=1,maxrounds do local s = args['team' .. i .. '_rnd' .. r .. '_' .. 'color'] or args['team' .. i .. '_rnd' .. r .. '_' .. 'colour'] or nil local n = args['team' .. i .. '_rnd' .. r .. '_' .. 'note'] or args[pfx .. i .. '_rnd' .. r .. '_' .. 'note'] or nil if s then s = color_map[s] or s end local posrt = unescapetag(pos[r] or ) local posrc = posrt if posrt:match('^%s*[%d]+[A-Za-z][A-Za-z0-9]*') then posrc = posrc:match('^%s*[%d]+([A-Za-z][A-Za-z0-9]*)') posrt = mw.ustring.gsub(posrt, '^%s*([%d]+)[A-Za-z][A-Za-z0-9]*', '%1') end row:tag('td') :css('background-color', s or get_color(posrc)) :wikitext((legend_symbols[posrt] or posrt) .. (n or )) end end if args['split' .. i] and k ~= #team_list then row = root:tag('tr') :css('background-color', '#BBBBBB') :css('line-height', '3pt') row:tag('td') :attr('colspan', maxrounds + 1) end end -- build the legend if addlegend then -- Sort the keys for the legend local legendkeys = {} for k,v in pairs( textlist ) do table.insert(legendkeys, k) end table.sort(legendkeys) if args['legendorder'] then legendkeys = mw.text.split(args['legendorder'] .. delimiter .. table.concat(legend_order_default, delimiter) .. delimiter .. table.concat(legendkeys, delimiter), '%s*' .. delimiter .. '%s*') else legendkeys = mw.text.split( table.concat(legend_order_default, delimiter) .. delimiter .. table.concat(legendkeys, delimiter), '%s*' .. delimiter .. '%s*') end local lroot if (legendpos == 't' or legendpos == 'b') then lroot = mw.html.create() local firsttag = true for k,v in pairs( legendkeys ) do if v and textlist[v] then if firsttag == false then lroot:wikitext('; ') end local c = colorlist[v] or local l = lroot:tag('span') :css('margin', '0') :css('white-space', 'nowrap') :tag('span') :addClass('legend-text') :css('border', 'none') :css('padding', '1px .3em') :css('background-color', color_map[c] or c) :css('font-size', '95%') :css('border', '1px solid #BBB') :css('line-height', '1.25') :css('text-align', 'center') :wikitext(legend_symbols[v] or (v:match('^[^%d][^%d]?$') and v) or ' ') :done() :wikitext(' = ' .. textlist[v]) textlist[v] = nil firsttag = false end end else lroot = mw.html.create('table') if legendpos == 'tl' or legendpos == 'bl' then lroot:addClass('wikitable') lroot:css('font-size', '88%') else lroot:addClass('infobox') lroot:addClass('bordered') -- lroot:css('width', 'auto') end for k,v in pairs( legendkeys ) do if v and textlist[v] then local c = colorlist[v] or local row = (legendpos == 'tl' or legendpos == 'bl') and lroot or lroot:tag('tr') local l = row:tag('th'):css('background-color', color_map[c] or c) if legend_symbols[v] then l:css('font-weight', 'normal') :css('padding', '1px 3px') :wikitext(legend_symbols[v]) else l:css('width', '10px') end row:tag('td') :css('padding', '1px 3px') :wikitext(textlist[v]) textlist[v] = nil end end end if (legendpos == 'bl' or legendpos == 'br') then footer = footer .. tostring(lroot) elseif (legendpos == 'b') then prenotes = prenotes .. tostring(lroot) elseif (legendpos == 't') then args['toptext'] = (args['toptext'] or ) .. frame:expandTemplate{ title = 'refbegin' } .. tostring(lroot) .. frame:expandTemplate{ title = 'refend' } else header = header .. tostring(lroot) end end -- simplify updated == complete case local lupdated = updated and string.lower(updated) or if lupdated == labels['complete'] or lupdated == 'complete' then lupdated = end -- add note list if hasnotes then footer = footer .. frame:expandTemplate{ title = 'notelist' } end -- build the footer if prenotes ~= or notes or source or lupdated ~= then footer = footer .. frame:expandTemplate{ title = 'refbegin' } if lupdated ~= then local mtext = args['matches_text'] or labels['matches'] if lupdated == labels['future'] or lupdated == 'future' then footer = footer .. matches_date(labels['firstplayed'] .. ' ', mtext, args['start_date'] or labels['futuredate']) else footer = footer .. matches_date(labels['updatedto'] .. ' ', mtext, updated) end end if source then footer = footer .. labels['source'] .. ' ' .. source end if prenotes ~= then if lupdated ~= or source then footer = footer .. ' end footer = footer .. prenotes end if notes then if prenotes ~= or lupdated ~= or source then footer = footer .. ' end footer = footer .. labels['notes'] .. ' ' .. notes end footer = footer .. frame:expandTemplate{ title = 'refend' } end -- add clear right for the legend if necessary footer = footer .. ((addlegend and (legendpos == 'bl' or legendpos == 'br')) and ' if tracking ~= then if frame:preprocess( "{{REVISIONID}}" ) == "" then tracking = preview end end return frame:extensionTag{ name = 'templatestyles', args = { src = templatestyles} } .. header .. (args['toptext'] or ) .. ' end return p |
随便看 |
|
开放百科全书收录14589846条英语、德语、日语等多语种百科知识,基本涵盖了大多数领域的百科知识,是一部内容自由、开放的电子版国际百科全书。