Module:Table of contents
This module provides a way to generate tables of contents for books.
Usage
editThis module, in order to be used, must be invoked directly, which means there is no intermediary template. In theory, an intermediary template could be created, but it would only support a limited number of arguments, while the direct invocation of the modules allows an unlimited number of arguments.
This module recognizes an unlimited number of sequences of four numbered parameters that respectively indicate the name of a section, the page of that section (the name of the page on the wiki to which the section label will link), the description of that section and the completion status of that section (a multiple of 25 smaller than 100 followed by a percentage sign). Only the first is required, and the format of the table of contents will adapt automatically depending on which parameters are given values to.
The module recognizes some named parameters that are described in the next section. Parameters, both named and numbered, are considered to have a value when they are provided an argument that does not contain only whitespace.
Parameters
editAll the parameters described here are optional.
- indent
- This is the length of the left margin of the section list, specified as a CSS margin length.
- list style
- The value of this parameter will be used as the value for the
list-style
CSS property of the section list. The most common use is to remove the bullets of a list by setting it tonone
. - page name prefix
- This is a prefix added to all page names given for sections. It is most commonly used to add the name of the book to the page names with
Module:Table of contents
. - unordered
- The list of sections will be ordered unless an argument is provided for this parameter.
Examples
editThis module will, depending on whether descriptions are provided and on whether the section list is ordered or not, adapt the layout and appearance of the table of contents.
Described sections
edit{{#invoke:table of contents|generate | Section 1 | Section 1 | This is the first section. | 100% | Section 2 | Section 2 | This is the second section. | 50% | Section 3 | Section 3 | This is the third section. | 75% | Section 4 | | This is the fourth section. | 75% | Section 5 | Section 5 | This is the fifth section. | 25% | Section 6 | Section 6 | This is the sixth section. | 100% | Section 7 | Section 7 | This is the seventh section. | 25% | Section 8 | Section 8 | This is the eighth section. | 50% }}
- Section 1
- This is the first section.
- Section 2
- This is the second section.
- Section 3
- This is the third section.
- Section 4
- This is the fourth section.
- Section 5
- This is the fifth section.
- Section 6
- This is the sixth section.
- Section 7
- This is the seventh section.
- Section 8
- This is the eighth section.
Ordered sections with no completion status
edit{{#invoke:table of contents|generate | Section 1 | Section 1 | | | Section 2 | Section 2 | | | Section 3 | Section 3 | | | Section 4 | | | | Section 5 | Section 5 | | | Section 6 | Section 6 | | | Section 7 | Section 7 | | | Section 8 | Section 8 | | }}
Unordered sections
edit{{#invoke:table of contents|generate | Section 1 | Section 1 | | 100% | Section 2 | Section 2 | | 50% | Section 3 | Section 3 | | 75% | Section 4 | | | 75% | Section 5 | Section 5 | | 25% | Section 6 | Section 6 | | 100% | Section 7 | Section 7 | | 25% | Section 8 | Section 8 | | 50% | unordered = true }}
Unordered sections with no indentation, marker or completion status
edit{{#invoke:table of contents|generate | Section 1 | Section 1 | | | Section 2 | Section 2 | | | Section 3 | Section 3 | | | Section 4 | | | | Section 5 | Section 5 | | | Section 6 | Section 6 | | | Section 7 | Section 7 | | | Section 8 | Section 8 | | | unordered = true | indent = 0 | list style = none }}
local table_of_contents = {}
local html = mw.html
local function exists(arg)
return arg ~= nil and not arg:match('^%s*$')
end
local function trim_arguments(args)
local result = {}
for key, value in pairs(args) do
result[key] = mw.text.trim(value)
end
return result
end
local function are_section_descriptions_present(args)
for key, value in pairs(args) do
if type(key) == 'number' and (key + 1) % 4 == 0 and exists(value) then
return true
end
end
return false
end
local function generate_section_label(name, page, description, completion_status)
return (description and html.create('dt') or html.create('li')):wikitext((exists(page) and "[[" .. page .. "|" .. name .. "]]" or name) .. (completion_status and ' ' .. completion_status or '')), (description and description[1]) and html.create('dd'):wikitext(description[1]) or label
end
local function get_section_page_name(page_name, page_name_prefix)
return exists(page_name) and (page_name_prefix or '') .. page_name or nil
end
function table_of_contents.generate(frame)
local args = trim_arguments(frame.args)
local is_a_description_list = are_section_descriptions_present(args)
local toc = (is_a_description_list and html.create('dl') or (args.unordered and html.create('ul') or html.create('ol'))):attr('id', 'generated-table-of-contents')
toc = exists(args.indent) and toc:css('margin-left', args.indent) or toc
toc = exists(args['list style']) and toc:css('list-style', args['list style']) or toc
local section_name = args[1]
local section_page = get_section_page_name(args[2], args['page name prefix'])
local section_description = args[3]
local section_completion_status = args[4]
local section_marker = 5
while section_name do
for _, node in next, {generate_section_label(section_name, section_page, is_a_description_list and {section_description} or nil, (exists(section_completion_status) and frame:expandTemplate{title = 'stage short', args = {section_completion_status}} or nil))} do
toc:node(node)
end
section_name = args[section_marker]
section_page = get_section_page_name(args[section_marker+1], args['page name prefix'])
section_description = args[section_marker+2]
section_completion_status = args[section_marker+3]
section_marker = section_marker + 4
end
return tostring(toc)
end
return table_of_contents