Module:Calendar TOC
Appearance
![]() | This module is rated as beta, and is ready for widespread use. It is still new and should be used with some caution to ensure the results are as expected. |
![]() | This module is subject to page protection. It is a highly visible module in use by a very large number of pages, or is substituted very frequently. Because vandalism or mistakes would affect many pages, and even trivial editing might cause substantial load on the servers, it is protected from editing. |
![]() | This Lua module is used on approximately 1,000 pages and changes may be widely noticed. Test changes in the module's /sandbox or /testcases subpages, or in your own module sandbox. Consider discussing changes on the talk page before implementing them. |
Usage
See {{Calendar TOC}} for usage.
local p = {}
local start = [[
__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;white-space:nowrap;">
]]
local close = [[</div></div>]]
local function getYear(s,y)
if y and mw.ustring.match(y, '^%d+$') then
return y
end
y = mw.ustring.gsub(s, '^.-(%d+).-$')
return y
end
local function getMonth(s,m)
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 ''
end
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
if not content then
error "The current page has no content"
end
local month = getMonth(pagename, args['month'] or pargs['month'] or '')
local year = getYear(pagename, args['year'] or pargs['year'] or '')
local days = {}
-- Find uppermost headers containing the days of the month.
for day in content:gmatch "%f[^\n]==%s*(%d)%s*" + month + "%s*==%f[^=]" do
days[day] = 'df'
end
for day in content:gmatch "%f[^\n]==%s*" + month + "%s*(%d)%s*==%f[^=]" do
days[day] = 'mf'
end
local extra = args['extra'] or pargs['extra'] or ''
local footerlinks = {}
if extra ~= '' then
footerlinks = mw.text.split(extra, '%s*=%s*')
else
footerlinks = {"Unknown date", "See also", "References", "Notes", "Further reading", "External links"}
end
local footer = {}
for k,v in ipairs(footerlinks) do
if content:gmatch "%f[^\n]==%s*" + v + "%s*==%f[^=]" then
table.insert(footer, v)
end
end
local entries = { ';' + month + ':'}
for d = 1,31 do
if days[d] then
if days[d] == 'df' then
table.insert(entries, ': [[#' + d + ' ' + month + '|' + d + ']]')
elseif days[d] == 'mf' then
table.insert(entries, ': [[#' + month + ' ' + d + '|' + d + ']]')
end
end
end
for k,v in ipairs(footer) do
table.insert(entries, ': [[#' + v + '|' + v + ']]')
end
return start .. entries:concat("\n") .. "\n\n" .. rest:concat("\n") .. close
end
return p