Module:TOC

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search
Lua

CodeDiscussionEditHistoryLinksLink count Subpages:DocumentationTestsResultsSandboxLive code All modules


Usage

[edit]

{{#invoke:TOC|letters}}

Creates lists of anchors for {{CategoryTOC}}. Specify list items using numbered parameters. You can specify alternate display titles for each items using the "...T" paramters (|1T=, |2T=, |3T=, etc) based on the position in the list, or the "...L" parameters (|AL=, |BL=, |CL=, etc.) based on the name of each list item.

If you are specifying a range of numbers, instead of listing each item, you can use |start= and |end=, e.g. {{#invoke:TOC|letters|start=1|end=100}} (these values will be ignored if they are not numbers), and an optional increment |by= (whose default is 1). All number will be 0-padded to the length of the longest number, but display without 0-padding (see second example below).

You can also specify an optional |prefix= (default is empty) to the sort keys used for indexing the categories members and to navigate to them from the generated links list.

Examples

[edit]
  1. "{{#invoke:TOC|letters}}" → ""
  2. "{{#invoke:TOC|letters|0|1|2|3|4|5|6|7|8|9}}" → ""
  3. "{{#invoke:TOC|letters|A|B|C|1T=First|2T=Second|3T=Third}}" → ""
  4. "{{#invoke:TOC|letters|start=1|end=31}}" → ""
  5. "{{#invoke:TOC|letters|start=1|end=31|by=2}}" → ""
  6. "{{#invoke:TOC|letters|start=1900|end={{CURRENTYEAR}}|by=10|prefix=+}}" → ""
  7. "{{#invoke:TOC|letters|start=7|end=12|01L={{Int:January}}|02L={{Int:February}}|03L={{Int:March}}|04L={{Int:April}}|05L={{Int:May}}|06L={{Int:June}}|07L={{Int:July}}|08L={{Int:August}}|09L={{Int:September}}|10L={{Int:October}}|11L={{Int:November}}|12L={{Int:December}}}}" → ""

Code

return {
	letters = function(frame)
		local origArgs = frame.args
		if not origArgs[1] and not(tonumber(origArgs['start']) and tonumber(origArgs['start'])) then
			origArgs = frame:getParent().args
			if not origArgs[1] and not(tonumber(origArgs['start']) and tonumber(origArgs['start'])) then
				return ''
			end
		end
		
		local args
		local prefix = origArgs.prefix or ''
		local start = tonumber(origArgs['start'])
		local limit = tonumber(origArgs['end'])
		if start and limit then
			args = {}
			local incr = tonumber(origArgs.by) or 1
			local form = '%0' .. math.floor(math.log10(limit) + 1) .. 'd'
			local i = 1
			for v = start, limit, incr do
				v = string.format(form, v)
				args[i] = v
				if tonumber(v) ~= (prefix .. v) and not origArgs[i .. 'T'] then
					origArgs[i .. 'T'] = tonumber(v)
				end
				i = i + 1
			end
		else
			args = origArgs	
		end
		
		local out = {}
		local currentTitle = mw.title.getCurrentTitle()
		for i, v in ipairs(args) do
			local title = origArgs[v .. 'L'] or origArgs[i .. 'T'] or v
			if currentTitle.namespace == 14 then --Category namespace
				local url = currentTitle:fullUrl('from=' .. prefix .. v)
				table.insert(out, '<li>[' .. url .. ' ' .. title .. ']</li>')
			else
				table.insert(out, '<li>[[#' .. prefix .. v .. '|' .. title .. ']]</li>')
			end
		end
		
		return '<ul class="hlist plainlist" style="display:inline;margin:0;padding:0">' .. table.concat(out, ' ') .. '</ul>'
	end
}