local p = {}
local math_module = require("Module:Math")
local lang -- Lazy initialize
local function formatDate(fmt, d)
local function rnd(num, digits)
lang = lang or mw.language.getContentLanguage()
-- This function implements {{rnd}}
local success, newDate = pcall(lang.formatDate, lang, fmt, d)
return math_module._precision_format(tostring(num), digits)
if success then
return newDate
else
error(string.format(
"invalid date '%s' passed to getDate",
tostring(date)
))
end
end
local function caltoc(days, unk, footer, month, year)
local weekdays = {'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'}
local j = tonumber(formatDate('N','1 ' .. month .. ' ' .. year))
local N = tonumber(formatDate('t','1 ' .. month .. ' ' .. year))
local res = {}
function p.ifexists(frame)
table.insert(res, '__NOTOC__<table role="navigation" id="toc" class="wikitable toc plainlinks" style="text-align:center">')
page = frame.args[1]
table.insert(res, '<tr><th colspan=7 id="toctitle" style="background:inherit"><span id="tocheading" style="font-weight:bold">' .. month .. ' ' .. year .. '</th></tr>')
if not page then return (frame.args['no'] or '') end
table.insert(res, '<tr><th scope="col">' .. table.concat(weekdays, '</th><th scope="col">') .. '</th></tr>')
if mw.title.new(page).exists then return (frame.args['yes'] or 'yes') end
return (frame.args['no'] or '')
end
function p.lists(frame)
local s = '\n' .. (frame.args[1] or '') .. '\n'
s = mw.ustring.gsub(s, '([\r\n])%*([^\r\n]*)', '%1<ul><li>%2</li></ul>')
s = mw.ustring.gsub(s, '([\r\n])#([^\r\n]*)', '%1<ol><li>%2</li></ol>')
s = mw.ustring.gsub(s, '</ol>%s*([\r\n]*)<ol>', '%1')
s = mw.ustring.gsub(s, '</ul>%s*([\r\n]*)<ul>', '%1')
s = mw.ustring.gsub(s, '^[\r\n](.*)[\r\n]$', '%1')
return s
end
function p.precision(frame)
return math_module._precision(frame.args[1])
end
function p.wpct(frame)
local d = 1-j
local w = tonumber(frame.args[1]) or 0
local skip = false
local l = tonumber(frame.args[2]) or 0
while d <= N do
local pct = '–'
table.insert(res, '<tr>')
if (w + l) for> i=1,70 dothen
pct = rnd(w / (w + l), 3):gsub('^0', '')
d = d + 1
if d > 0 and d <= N then
local f = days[tostring(d)]
if f and f == 'df' then
table.insert(res, '<td>[[#' .. d .. ' ' .. month .. '|' .. d .. ']]</td>')
elseif f and f == 'mf' then
table.insert(res, '<td>[[#' .. month .. ' ' .. d .. '|' .. d .. ']]</td>')
else
table.insert(res, '<td>' .. d .. '</td>')
end
skip = false
elseif (skip == false) then
local cs = (d <= 0) and (1 - d) or (8 - i)
table.insert(res, '<td' .. (cs > 1 and ' colspan=' .. cs or '') .. '>' .. cs .. '</td>')
skip = true
end
end
table.insert(res, '</tr>')
end
if unk ~= nil then
table.insert(res, '<tr><td colspan=7>[[#' .. unk .. '|' .. unk .. ']]</td></tr>')
end
return pct
if #footer > 0 then
table.insert(res, '<tr>')
if #footer > 1 then
table.insert(res, '<td colspan=7 style="padding: 0.2em;"><div class="hlist">')
for k,v in ipairs(footer) do
table.insert(res, '* [[#' .. v .. '|' .. v .. ']]')
end
table.insert(res, '</td></div>')
else
table.insert(res, '<td colspan=7>[[#' .. table.concat(footer,'') .. '|' .. table.concat(footer,'') .. ']]</td>')
end
table.insert(res, '</tr>')
end
table.insert(res, '</table>')
return table.concat(res, '\n')
end
function p.extractcolor(frame)
local function listtoc(founddays, days, unk, footer, month)
local starttxtstr = frame.args[[1] or ''
local color = mw.ustring.match(';' .. str .. ';', '.*;%s*([Cc][Oo][Ll][Oo][Rr]%s*:%s*.-)%s*;')
__NOTOC__<!--
return color or 'NO MATCH'
--><div role="navigation" id="toc" class="toc plainlinks hlist" aria-labelledby="tocheading" style="text-align:left;">
end
<div id="toctitle" class="toctitle" style="text-align:center;"><span id="tocheading" style="font-weight:bold;">Contents</span></div>
<div style="margin:auto;">
local function getBestStatement(item_id, property_id)
]]
if not(item_id) or not(mw.wikibase.isValidEntityId(item_id)) or not(mw.wikibase.entityExists(item_id)) then
local closetxt = [[</div></div>]]
return false
local entries = (#founddays > 0) and { ';' .. month} or {}
for k,d in ipairs(founddays) do
local fmt = days[d]
if fmt == 'df' then
table.insert(entries, ': [[#' .. d .. ' ' .. month .. '|' .. d .. ']]')
elseif fmt == 'mf' then
table.insert(entries, ': [[#' .. month .. ' ' .. d .. '|' .. d .. ']]')
end
end
local statements = mw.wikibase.getBestStatements(item_id, property_id)
if unk ~= nil then
if not statements or #statements == 0 then
table.insert(entries, ': [[#' .. unk .. '|' .. unk .. ']]')
return false
end
local hasNoValue = ( statements[1].mainsnak and statements[1].mainsnak.snaktype == 'novalue' )
for k,v in ipairs(footer) do
if hasNoValue then
table.insert(entries, ': [[#' .. v .. '|' .. v .. ']]')
return false
end
return statements[1]
return starttxt .. table.concat(entries,"\n") .. closetxt
end
local function getYearp.hasOSM(s,yframe)
return getBestStatement(mw.wikibase.getEntityIdForCurrentPage(), 'P402') and 'yes' or 'no'
if y and mw.ustring.match(y, '^%d+$') then
return y
end
y = mw.ustring.gsub(s, '^.-(%d+).-$', '%1')
return y
end
local function getMonthp.chart(s,mframe)
local chart = require('Module:Chart')['bar-chart']
local mnames = {
['January']=1,
['February']=2,
['March']=3,
['April']=4,
['May']=5,
['June']=6,
['July']=7,
['August']=8,
['September']=9,
['October']=10,
['November']=11,
['December']=12
}
if m and mnames[m] then
return m
end
for k,n in pairs(mnames) do
if mw.ustring.match(s or '', k) then
return k
end
end
return ''chart(frame)
end
function p.main(frame)
local argsgetArgs = framerequire('Module:Arguments').argsgetArgs
local args = getArgs(frame, {parentFirst = true,
local pargs = frame:getParent().args
valueFunc = function (key, val)
local current_title = mw.title.getCurrentTitle()
if key == 'text_IPS' then
local pagename = current_title.text
return nil
local content = current_title:getContent()
end
local outfmt = args['format'] or pargs['format'] or ''
if type(val) == 'string' then
local unknown = nil
val = val:match('^%s*(.-)%s*$')
if val == '' then
return nil
else
return val
end
else
return val
end
end
})
local team_list = {}
local ii = 1
while args['team'..ii] ~= nil do
team_list[args['team'..ii]] = ii
ii = ii + 1
end
local max_team = ii - 1
local first_team, last_team = 1, max_team
if args['_demo'] or pargs['_demo'] then
content = if args['_demoshowteam'] orand pargsteam_list[args['_demoshowteam']] or ''then
first_team = team_list[args['showteam']] - 2
last_team = first_team + 4
if first_team < 1 then
first_team = 1
last_team = first_team + 4
end
if last_team > max_team then
last_team = max_team
first_team = max_team - 4
end
if first_team < 1 then first_team = 1 end
end
local hasnotes = false
local ii = first_team
if not content then
local res = '{| class="wikitable"\n'
error "The current page has no content"
res = res .. '! Pos. !! Team !! Result\n'
while args['team'..ii] ~= nil and (ii <= last_team) do
res = res .. '|-\n'
res = res .. '| ' .. ii .. '\n'
res = res .. '| ' .. (args['name_'..args['team' .. ii]] or '') .. '\n'
local text_result = args['result'..ii] and args['text_'..args['result'..ii]] or ''
local style_text = ''
if text_result:match('fbmulticompefn') then
hasnotes = true
style_text = style_text .. 'padding:0;'
end
style_text = style_text .. (args['result'..ii] and ('background:' .. args['col_'..args['result'..ii]]) or '')
res = res .. '| style="' .. style_text .. '" | ' .. text_result .. '\n'
ii = ii + 1
end
res = res .. '|}'
-- Get the month and year
if hasnotes == true then
local month = getMonth(pagename, args['month'] or pargs['month'] or '')
res = res .. '<b>Table notes:</b>' .. frame:expandTemplate{ title = 'notelist'}
local year = getYear(pagename, args['year'] or pargs['year'] or '')
-- Get list of valid footer links
local extra = args['extra'] or pargs['extra'] or ''
local footerlinks = {}
if extra ~= '' then
footerlinks = mw.text.split(extra, '%s*=%s*')
else
footerlinks = {"See also", "References", "Notes", "Further reading", "External links"}
end
local validfooter = {}
for k,v in ipairs(footerlinks) do
validfooter[v] = 1
end
-- Generate tracking
-- Get all the level two headings for days of the month
if not args['notracking'] then
local days = {}
-- Step 1: Generate a team and result list
local founddays = {}
for k,v in pairs(args) do
local footer = {}
-- nothing
for v in mw.ustring.gmatch(content, "%f[^\n]==%s*([^\r\n]-)%s*==%f[^=]") do
v = mw.ustring.gsub(v,'^[=%s]*(.-)[%s=]*', '%1')
local df = mw.ustring.gsub(v,'^(%d+[%-–%d]*)%s*' .. month .. '$', '%1')
local mf = mw.ustring.gsub(v,'^' .. month .. '%s*(%d+[%-–%d]*)$', '%1')
if tonumber(df) then
days[df] = 'df'
table.insert(founddays, df)
elseif tonumber(mf) then
days[df] = 'mf'
table.insert(founddays, mf)
elseif v == "Unknown date" then
unknown = "Unknown date"
elseif validfooter[v] then
table.insert(footer, v)
end
end
return res
-- Now generate the TOC
if outfmt ~= 'list' then
return caltoc(days, unknown, footer, month, year)
end
return listtoc(founddays, days, unknown, footer, month)
end
|