Module:TOCyears: Difference between revisions
Appearance
Content deleted Content added
Inline functions used only oncwe Tag: Reverted |
|||
Line 7: | Line 7: | ||
local p = {} |
local p = {} |
||
local function makeTocLink(page, display) |
|||
display = display or page |
|||
return string.format('[[#%s|%s]]', page, display) |
|||
end |
|||
local function makeToc(s, args) |
|||
-- Make a TOC box from a string and from the given table of arguments. |
|||
checkType('makeToc', 1, s, 'string') |
|||
checkType('makeToc', 2, args, 'table', true) |
|||
args = args or {} |
|||
-- Root |
|||
local root = mw.html.create() |
|||
local isPrimary = yesno(args.primary) ~= false |
|||
if isPrimary then |
|||
root:wikitext('__NOTOC__') |
|||
end |
|||
-- Top div tag |
|||
local top = root:tag('div') |
|||
top:addClass('toc plainlinks hlist') |
|||
if isPrimary then |
|||
top |
|||
:attr('id', 'toc') |
|||
:attr('class', 'toc') |
|||
end |
|||
local align = args.align and args.align:lower() |
|||
if align == 'left' then |
|||
top |
|||
:css('float', 'left') |
|||
:css('clear', args.clear or 'left') |
|||
elseif align == 'right' then |
|||
top |
|||
:css('float', 'right') |
|||
:css('clear', args.clear or 'right') |
|||
elseif align == 'center' then |
|||
top |
|||
:css('margin', 'auto') |
|||
:css('clear', args.clear or 'none') |
|||
else |
|||
top |
|||
:css('clear', args.clear or 'left') |
|||
end |
|||
top:newline() |
|||
-- Title div tag |
|||
local title = args.title or mw.message.new('Toc'):plain() |
|||
local titleDiv = top:tag('div') |
|||
titleDiv:attr('id', 'toctitle') |
|||
titleDiv:attr('class', 'toctitle') |
|||
if isPrimary then |
|||
titleDiv:wikitext('<h2>' .. title .. '</h2>') |
|||
else |
|||
titleDiv:tag('strong'):wikitext(title) |
|||
end |
|||
-- Content |
|||
top |
|||
:newline() |
|||
:wikitext(s) |
|||
:newline() |
|||
return tostring(root) |
|||
end |
|||
function p._years(args) |
function p._years(args) |
||
Line 19: | Line 84: | ||
year = year .. '-' .. tostring(i + range) |
year = year .. '-' .. tostring(i + range) |
||
end |
end |
||
links[#links + 1] = |
links[#links + 1] = makeTocLink(year) |
||
i = i + step |
i = i + step |
||
end |
end |
||
if #links > 0 then |
if #links > 0 then |
||
links = '* ' .. table.concat(links, ' ') |
links = '* ' .. table.concat(links, ' ') |
||
return makeToc(links, args) |
|||
-- Root |
|||
local root = mw.html.create() |
|||
local isPrimary = yesno(args.primary) ~= false |
|||
if isPrimary then |
|||
root:wikitext('__NOTOC__') |
|||
end |
|||
-- Top div tag |
|||
local top = root:tag('div') |
|||
top:addClass('toc plainlinks hlist') |
|||
if isPrimary then |
|||
top |
|||
:attr('id', 'toc') |
|||
:attr('class', 'toc') |
|||
end |
|||
local align = args.align and args.align:lower() |
|||
if align == 'left' then |
|||
top |
|||
:css('float', 'left') |
|||
:css('clear', args.clear or 'left') |
|||
elseif align == 'right' then |
|||
top |
|||
:css('float', 'right') |
|||
:css('clear', args.clear or 'right') |
|||
elseif align == 'center' then |
|||
top |
|||
:css('margin', 'auto') |
|||
:css('clear', args.clear or 'none') |
|||
else |
|||
top |
|||
:css('clear', args.clear or 'left') |
|||
end |
|||
top:newline() |
|||
-- Title div tag |
|||
local title = args.title or mw.message.new('Toc'):plain() |
|||
local titleDiv = top:tag('div') |
|||
titleDiv:attr('id', 'toctitle') |
|||
titleDiv:attr('class', 'toctitle') |
|||
if isPrimary then |
|||
titleDiv:wikitext('<h2>' .. title .. '</h2>') |
|||
else |
|||
titleDiv:tag('strong'):wikitext(title) |
|||
end |
|||
-- Content |
|||
top |
|||
:newline() |
|||
:wikitext(s) |
|||
:newline() |
|||
return tostring(root) |
|||
else |
else |
||
return '' |
return '' |
||
Line 80: | Line 95: | ||
end |
end |
||
setmetatable(p, {__index = function(t, k) |
|||
function p.years(frame) |
|||
return function (frame) |
|||
local args = require('Module:Arguments').getArgs(frame, { |
|||
local args = require('Module:Arguments').getArgs(frame, { |
|||
wrappers = { |
|||
wrappers = { |
|||
'Template:TOCyears', |
|||
'Template:TOCyears', |
|||
} |
|||
} |
|||
}) |
|||
}) |
|||
return p._years(args) |
|||
return p['_' .. k](args) |
|||
end |
|||
end |
|||
end}) |
|||
return p |
return p |
Revision as of 16:54, 4 October 2020
This module implements {{TOCyears}}
Usage
From wikitext
From wikitext this module should usually be used through {{TOCyears}}. However, it is also possible to use it with the following syntax:
{{#invoke:TOC|years|args}}
See the documentation of {{TOCyears}} for available parameters.
From Lua
Load the module like this:
local mTOCyears = require('Module:TOCyears')
You can then use it with the following syntax:
mTOCyears._years(args)
args variable is a table of arguments to be passed to the function. Please see {{TOCyears}} for available arguments.
-- This module implements various templates that create custom tables of
-- contents.
-- Load necessary modules
local yesno = require('Module:Yesno')
local checkType = require('libraryUtil').checkType
local p = {}
local function makeTocLink(page, display)
display = display or page
return string.format('[[#%s|%s]]', page, display)
end
local function makeToc(s, args)
-- Make a TOC box from a string and from the given table of arguments.
checkType('makeToc', 1, s, 'string')
checkType('makeToc', 2, args, 'table', true)
args = args or {}
-- Root
local root = mw.html.create()
local isPrimary = yesno(args.primary) ~= false
if isPrimary then
root:wikitext('__NOTOC__')
end
-- Top div tag
local top = root:tag('div')
top:addClass('toc plainlinks hlist')
if isPrimary then
top
:attr('id', 'toc')
:attr('class', 'toc')
end
local align = args.align and args.align:lower()
if align == 'left' then
top
:css('float', 'left')
:css('clear', args.clear or 'left')
elseif align == 'right' then
top
:css('float', 'right')
:css('clear', args.clear or 'right')
elseif align == 'center' then
top
:css('margin', 'auto')
:css('clear', args.clear or 'none')
else
top
:css('clear', args.clear or 'left')
end
top:newline()
-- Title div tag
local title = args.title or mw.message.new('Toc'):plain()
local titleDiv = top:tag('div')
titleDiv:attr('id', 'toctitle')
titleDiv:attr('class', 'toctitle')
if isPrimary then
titleDiv:wikitext('<h2>' .. title .. '</h2>')
else
titleDiv:tag('strong'):wikitext(title)
end
-- Content
top
:newline()
:wikitext(s)
:newline()
return tostring(root)
end
function p._years(args)
local i = tonumber(args.startyear) or 1900
local endYear = tonumber(args.endyear) or tonumber(os.date('%Y'))
local range = tonumber(args.range)
local step = tonumber(args.step) or 1
local links = {}
while i <= endYear do
local year = tostring(i)
if range then
year = year .. '-' .. tostring(i + range)
end
links[#links + 1] = makeTocLink(year)
i = i + step
end
if #links > 0 then
links = '* ' .. table.concat(links, ' ')
return makeToc(links, args)
else
return ''
end
end
setmetatable(p, {__index = function(t, k)
return function (frame)
local args = require('Module:Arguments').getArgs(frame, {
wrappers = {
'Template:TOCyears',
}
})
return p['_' .. k](args)
end
end})
return p