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
 
(16 intermediate revisions by 5 users not shown)
Line 15:
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))
Line 21:
local res = {}
 
table.insert(res, '__NOTOC__<table role="navigation" id="toc" class="calendar-toc wikitable toc plainlinks" style="text-align:center">')
table.insert(res, '<tr><th colspan=7 id="toctitle" style="background:inherit"><span id="tocheading" 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>')
Line 39 ⟶ 40:
table.insert(res, '<td>' .. d .. '</td>')
end
else skip = false
table.insertelseif (res,skip '<td></td>'== false) then
local cs = (d <= 0) and (1 - d) or (8 - i)
local fmtv = days[d] ''
if d > N and cs > 2 and unk then
table.insert(entries, v ':= '[[#' .. vunk .. '|' .. vunk .. ']]')
unk = nil
end
if fmtcs =< 7 or v ~= 'df' then
table.insert(entriesres, ': [[#<td' .. d(cs ..> 1 and ' colspan=' .. monthcs or '') .. '|>' .. dv .. ']]</td>')
end
skip = true
end
end
table.insert(res, '</tr>')
end
if outfmtunk ~= 'list'nil then
 
table.insert(entriesres, ':<tr><td colspan=7>[[#' .. month .. ' ' .. dunk .. '|' .. dunk .. ']]</td></tr>')
end
if #footer > 0 then
table.insert(res, '<tr><td colspan=7 style="padding: 0.2em;"><center><div class="hlist">')
forif k,v#footer in> ipairs(footer)1 dothen
table.insert(res, '*<td [[#'colspan=7 .. vstyle="padding: 0.. '|' .. v .. ']]2em;">')
for k,v in ipairs(footer) do
table.insert(res, '</td></tr>* [[#' .. 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, '</div></centertr>')
table.insert(res, '</td></tr>')
end
table.insert(res, '</table>')
return table.concat(res, '\n')
end
 
local function listtoc(founddays, days, footer, month)
local starttxt = [[
__NOTOC__<!--
--><div role="navigation" id="toc" class="toc plainlinks hlist" aria-labelledby="tocheading" style="text-align:left;">
<div id="toctitle" class="toctitle" style="text-align:center;"><span id="tocheading" style="font-weight:bold;">Contents</span></div>
<div style="margin:auto;">
]]
local closetxt = [[</div></div>]]
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
for k,v in ipairs(footer) do
table.insert(entries, ': [[#' .. v .. '|' .. v .. ']]')
end
return starttxt .. table.concat(entries,"\n") .. closetxt
end
 
Line 122 ⟶ 117:
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 ''
if args['_demo'] or pargs['_demo'] then
local outfmt content = args['format_demo'] or pargs['format_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 pagename = current_title.text
local month = getMonth(pagename, args['month'] or pargs['month'] or '')
local year = getYear(pagename, args['year'] or pargs['year'] or '')
Line 140 ⟶ 141:
footerlinks = mw.text.split(extra, '%s*=%s*')
else
footerlinks = {"Unknown date", "See also", "References", "Notes", "Further reading", "External links"}
end
local validfooter = {}
Line 151 ⟶ 152:
local founddays = {}
local footer = {}
local starttxtunknown = [[nil
for v in mw.ustring.gmatch(content, "%f[^\n]==%s*([^\r\n]-)%s*==%f[^=]") do
v = mw.ustring.gsub(v,'^[=%s]*(.-)[%s=]*', '%1')
Line 161 ⟶ 163:
days[df] = 'mf'
table.insert(founddays, mf)
elseif fmtv == 'mf'"Unknown date" then
unknown = "Unknown date"
elseif validfooter[v] then
table.insert(footer, v)
Line 166 ⟶ 170:
end
 
return caltoc(days, unknown, footer, month, year)
-- Now generate the TOC
if outfmt ~= 'list' then
return caltoc(days, footer, month, year)
end
 
return listtoc(founddays, days, footer, month)
end