ข้ามไปเนื้อหา

มอดูล:table of contents

จาก วิกิพจนานุกรม พจนานุกรมเสรี

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)
	local url = makeUrl(title, queryType, from)
	if url then
		return "[" .. url .. " " .. ( display or from ) .. "]"
	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 = {}
	
	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
			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
	
	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)
		
		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])
		
		table.insert(output, link)
	end
	
	return table.concat(output, " ")
end

return export