local p =
local lang -- Lazy initializelocal function formatDate(fmt, d) lang = lang or mw.language.getContentLanguage local success, newDate = pcall(lang.formatDate, lang, fmt, d) if success then return newDate else error(string.format("invalid date '%s' passed to getDate", tostring(date) )) endend local function caltoc(days, unk, footer, month, year) local weekdays = local j = tonumber(formatDate('N','1 ' .. month .. ' ' .. year)) local N = tonumber(formatDate('t','1 ' .. month .. ' ' .. year)) local res =
table.insert(res, '
' .. month .. ' ' .. year .. ' | |||
---|---|---|---|
' .. table.concat(weekdays, ' | ') .. ' | ||
' .. d .. ' | ' .. d .. ' | ' .. d .. ' | 1 and ' colspan=' .. cs or ) .. '>' .. v .. ' |
' .. unk .. ' | |||
') for k,v in ipairs(footer) do table.insert(res, '* ' .. v .. '') end table.insert(res, ' | ' .. table.concat(footer,) .. ' |
local function listtoc(founddays, days, unk, footer, month) local starttxt = local closetxt =
'mf' then table.insert(entries, ': ' .. d .. '') end end if unk ~= nil then table.insert(entries, ': ' .. unk .. '') end for k,v in ipairs(footer) do table.insert(entries, ': ' .. v .. '') end return starttxt .. table.concat(entries,"\n") .. closetxtend
local function getYear(s,y) if y and mw.ustring.match(y, '^%d+$') then return y end y = mw.ustring.gsub(s, '^.-(%d+).-$', '%1') return yend
local function getMonth(s,m) local mnames = 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 end
function p.main(frame) local args = frame.args local pargs = frame:getParent.args local current_title = mw.title.getCurrentTitle local pagename = current_title.text local content = current_title:getContent local outfmt = args['format'] or pargs['format'] or local unknown = nil if args['_demo'] or pargs['_demo'] then content = args['_demo'] or pargs['_demo'] or end if not content then error "The current page has no content" end -- Remove comments content = mw.ustring.gsub(content, '', )
-- Get the month and year local month = getMonth(pagename, args['month'] or pargs['month'] or ) 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 = end local validfooter = for k,v in ipairs(footerlinks) do validfooter[v] = 1 end -- Get all the level two headings for days of the month local days = local founddays = local footer = for v in mw.ustring.gmatch(content, "%f[^\n]
%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
-- Now generate the TOC if outfmt ~= 'list' then return caltoc(days, unknown, footer, month, year) end
return listtoc(founddays, days, unknown, footer, month)end
return p