Jump to content

Module:minitoc

From Wiktionary, the free dictionary

This module provides the backend for {{minitoc}}.


local export = {}

local headword_data_module = "Module:headword/data"
local parameters_module = "Module:parameters"
local utilities_module = "Module:utilities"

local concat = table.concat
local insert = table.insert
local require = require

--[==[
Loaders for functions in other modules, which overwrite themselves with the target function when called. This ensures modules are only loaded when needed, retains the speed/convenience of locally-declared pre-loaded functions, and has no overhead after the first call, since the target functions are called directly in any subsequent calls.]==]
	local function format_categories(...)
		format_categories = require(utilities_module).format_categories
		return format_categories(...)
	end
	
	local function process_params(...)
		process_params = require(parameters_module).process
		return process_params(...)
	end

--[==[
Loaders for objects, which load data (or some other object) into some variable, which can then be accessed as "foo or get_foo()", where the function get_foo sets the object to "foo" and then returns it. This ensures they are only loaded when needed, and avoids the need to check for the existence of the object each time, since once "foo" has been set, "get_foo" will not be called again.]==]
	local L2_list
	local function get_L2_list()
		L2_list, get_L2_list = mw.loadData(headword_data_module).page.L2_list, nil
		return L2_list
	end

function export.main(frame)
	process_params(frame:getParent().args, {})
	local L2s_len, main, pre = (L2_list or get_L2_list()).n
	for i = 1, L2s_len do
		local L2, t = L2_list[i]
		if L2 == "Translingual" or L2 == "English" then
			pre = pre or {}
			t = pre
		else
			main = main or {}
			t = main
		end
		insert(t, "[[#" .. L2 .. "|" .. L2 .. "]]")
	end
	
	return frame:extensionTag("templatestyles", nil, {src="Module:minitoc/styles.css"}) .. "<div class=\"minitoc NavFrame\" data-toggle-category=\"table of contents\" data-minitoc-length=\"" .. L2s_len .. "\"><div class=\"NavHead\">Languages (" .. L2s_len .. ")</div><div class=\"NavContent\">" .. (pre and concat(pre, " •&nbsp;") .. (main and "<hr>" or "") or "") .. (main and concat(main, " •&nbsp;") or "") .. "<hr>[[#catlinks|Page categories]]</div></div><div data-toc-length=\"" .. L2s_len .. "\">__TOC__</div>" .. format_categories("Pages calling Template:minitoc", nil, nil, nil, true)
end

return export