Module:Calendar TOC: Difference between revisions

Content deleted Content added
No edit summary
remove format support: no one was using the hlist version; some code style adjustments in the main function
 
(36 intermediate revisions by 5 users not shown)
Line 1:
local p = {}
 
local startlang =-- [[Lazy initialize
local function formatDate(fmt, d)
__NOTOC__<!--
lang = lang or mw.language.getContentLanguage()
--><div role="navigation" id="toc" class="toc plainlinks hlist" aria-labelledby="tocheading" style="text-align:left;">
local success, newDate = pcall(lang.formatDate, lang, fmt, d)
<div id="toctitle" class="toctitle" style="text-align:center;"><span id="tocheading" style="font-weight:bold;">Contents</span></div>
if success then
<div style="margin:auto;white-space:nowrap;">
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 = {}
 
table.insert(res, '__NOTOC__<table role="navigation" id="toc" class="calendar-toc wikitable toc" style="text-align:center">')
local close = [[</div></div>]]
table.insert(res, '<tr><th colspan=7 style="background:inherit"><span style="font-weight:bold">' .. month .. ' ' .. year .. '</span></th></tr>')
table.insert(res, '<tr><th scope="col">' .. table.concat(weekdays, '</th><th scope="col">') .. '</th></tr>')
 
local d = 1-j
local skip = false
while d <= N do
table.insert(res, '<tr>')
for i=1,7 do
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)
local v = ''
if d > N and cs > 2 and unk then
v = '[[#' .. unk .. '|' .. unk .. ']]'
unk = nil
end
if cs < 7 or v ~= '' then
table.insert(res, '<td' .. (cs > 1 and ' colspan=' .. cs or '') .. '>' .. v .. '</td>')
end
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
if #footer > 0 then
table.insert(res, '<tr>')
if #footer > 1 then
table.insert(res, '<td colspan=7 style="padding: 0.2em;">')
for k,v in ipairs(footer) do
table.insert(res, '* [[#' .. v .. '|' .. v .. ']]')
end
table.insert(res, '</td>')
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
 
local function getYear(s,y)
Line 47 ⟶ 114:
 
function p.main(frame)
local current_title = mw.title.getCurrentTitle()
local pagename = current_title.text
local content = current_title:getContent()
local args = frame.args
local pargs = frame:getParent().args
local current_title = mw.title.getCurrentTitle()
local content = current_title:getContent()
if args['_demo'] or pargs['_demo'] then
content = args['_demo'] or pargs['_demo'] or ''
end
if not content then
Line 57 ⟶ 127:
end
-- Remove comments
content = mw.ustring.gsub(content, '<!--.-?-->', '')
 
-- Get the month and year
local pagename = current_title.text
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 days = {}
-- Find uppermost headers containing the days of the month.
for day in mw.ustring.gmatch(content, "%f[^\n]==%s*(%d)%s*" + month + "%s*==%f[^=]") do
days[day] = 'df'
end
for day in mw.ustring.gmatch(content, "%f[^\n]==%s*" + month + "%s*(%d)%s*==%f[^=]") do
days[day] = 'mf'
end
local extra = args['extra'] or pargs['extra'] or ''
local footerlinks = {}
Line 74 ⟶ 141:
footerlinks = mw.text.split(extra, '%s*=%s*')
else
footerlinks = {"Unknown date", "See also", "References", "Notes", "Further reading", "External links"}
end
local validfooter = {}
 
local footer = {}
for k,v in ipairs(footerlinks) do
validfooter[v] = 1
if mw.ustring.match(content, "%f[^\n]==%s*" + v + "%s*==%f[^=]") then
table.insert(footer, v)
end
end
-- Get all the level two headings for days of the month
local entries = { ';' + month + ':'}
forlocal ddays = 1,31 do{}
local founddays = {}
if days[d] then
local footer = {}
if days[d] == 'df' then
local unknown = nil
table.insert(entries, ': [[#' + d + ' ' + month + '|' + d + ']]')
for v in mw.ustring.gmatch(content, "%f[^\n]==%s*([^\r\n]-)%s*==%f[^=]") do
elseif days[d] == 'mf' then
v = mw.ustring.gsub(v,'^[=%s]*(.-)[%s=]*', '%1')
table.insert(entries, ': [[#' + month + ' ' + d + '|' + d + ']]')
local df = mw.ustring.gsub(v,'^(%d+[%-–%d]*)%s*' .. month .. '$', '%1')
end
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
 
for k,v in ipairs(footer) do
return caltoc(days, unknown, footer, month, year)
table.insert(entries, ': [[#' + v + '|' + v + ']]')
end
return start .. entries:concat("\n") .. "\n\n" .. rest:concat("\n") .. close
end