Modul:table of contents

A modult a Modul:table of contents/doc lapon tudod dokumentálni

local export = {}

local function makeUrl(title, queryType, from)
	if title and title.fullUrl then
		if from then
			if queryType then
				local query = { [queryType] = from }
				
				return title:fullUrl(query, "https")
			else
				return title:fullUrl(nil, "https")
			end
		end
	end
end

local function link(title, queryType, from, display, script)
	local url = makeUrl(title, queryType, from)
	if url then
		local ext_link = "[" .. url .. " " .. ( display or from ) .. "]"
		if script then
			return '<span class="' .. script .. '">' .. ext_link .. "</span>"
		else
			return ext_link
		end
	end
end

function export.show(frame)
	local output = {}
	local queryType
	
	local params = {
		[1] = { list = true },
		["subcat"] = { type = "boolean" },
		["top"] = {},
		["CopticDisplay"] = {},	-- for Coptic
	}
	
	local args = require("Module:parameters").process(frame.args[1] and frame.args or frame:getParent().args, params)
	
	local title = mw.title.getCurrentTitle()
	
	local froms = args[1]
	local display = {}
	local all_linktext = {}
	
	for i, from in pairs(froms) do
		if mw.ustring.match(from, ":") then
			local from, linktext = mw.ustring.match(from, "([^:]+):(.+)")
			if from then
				froms[i] = from
				display[i] = linktext
				table.insert(all_linktext, linktext)
			else
				error('Parameter ' .. i .. ' is badly formatted. It should contain a key to use in the link, a colon, and then the displayed text.')
			end
		end
	end
	
	local all_linktext = table.concat(all_linktext)
	local script = require "Module:scripts".findBestScriptWithoutLang(all_linktext)
	mw.log(all_linktext, script)
	
	if args.CopticDisplay then
		for character in mw.ustring.gmatch(args.CopticDisplay, ".") do
			table.insert(display, character)
		end
	end
	
	local subcat = args.subcat
	if subcat then
		queryType = "subcatfrom"
		-- [[Special:WhatLinksHere/Template:tracking/table-of-contents/subcat]]
		require("Module:debug").track("table-of-contents/subcat")
	else
		queryType = "from"
	end
	
	if args.top then
		local link = link(title, nil, args.top, nil)
		
		table.insert(output, link)
	end
	
	if args.top and froms and froms[1] then
		table.insert(output, " – ")
	end
	
	for i, from in ipairs(froms) do
		local link = link(title, queryType, from, display[i], script)
		
		table.insert(output, link)
	end
	
	return table.concat(output, " ")
end

return export