Module:table of contents

From Linguifex
Revision as of 11:15, 6 August 2024 by Sware (talk | contribs)
Jump to navigation Jump to search


local export = {}

local m_languages = require("Module:languages")
local title = mw.title.getCurrentTitle()

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

local function link(queryType, target, display, lang_code, script_code)
	local url = makeUrl(queryType, target)
	if url then
		local ext_link = "[" .. url .. " " .. (display or target) .. "]"
		ext_link = mw.html.create("span"):wikitext(ext_link)
		ext_link = lang_code and ext_link:attr("lang", lang_code) or ext_link
		ext_link = script_code and ext_link:addClass(script_code) or ext_link
		return tostring(ext_link)
	end
end

function export.show(frame)
	local output = {}
	
	local params = {
		[1] = {list = true},
		["lang"] = {type = "string"},
		["subcat"] = {type = "boolean"},
		["top"] = {},
	}
	
	local args = require("Module:parameters").process(frame.args[1] and frame.args or frame:getParent().args, params)
	
	local lang = args.lang and m_languages.getByCode(args.lang) or nil
	
	local targets = args[1]
	local displays = {}
	
	for i, char in ipairs(targets) do
		if char:find(":") then
			local target, display = char:match("([^:]+):(.+)")
			if target then
				targets[i] = lang and (lang:makeSortKey(target)) or target
				displays[i] = display
			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
		else
			targets[i] = lang and (lang:makeSortKey(char)) or char
			displays[i] = char
		end
	end
	
	local script_code
	if lang then
		script_code = lang:findBestScript(table.concat(displays)):getCode()
	elseif table.concat(displays) ~= "" then
		script_code = require("Module:scripts").findBestScriptWithoutLang(table.concat(displays)):getCode()
	end
	
	local subcat, queryType = args.subcat
	if subcat then
		queryType = "subcatfrom"
	else
		queryType = "from"
	end
	
	if args.top then
		local link = link(nil, args.top)
		table.insert(output, link)
		if targets and targets[1] then
			table.insert(output, " – ")
		end
	end
	
	for i, char in ipairs(targets) do
		local link = link(queryType, char, displays[i], lang and lang:getCode() or nil, script_code)
		table.insert(output, link)
	end
	
	if not lang then
		table.insert(output, "[[Category:User:Theknightwho/table of contents]]")
	end
	
	return table.concat(output, " ")
end

function export.full(frame)
	local params = {
		[1] = {list = true},
		["lang"] = {type = "string", required = true},
		["subcat"] = {type = "boolean"}
	}
	
	local args = require("Module:parameters").process(frame.args[1] and frame.args or frame:getParent().args, params)
	
	local lang = m_languages.getByCode(args.lang)
	local lang_code = lang:getCode()
	
	local targets = args[1]
	local displays = {}
		
	local script_code = lang:findBestScript(table.concat(args[1])):getCode()
	
	local subcat, queryType = args.subcat
	if subcat then
		queryType = "subcatfrom"
	else
		queryType = "from"
	end
	
	local output = mw.html.create("table")
		:attr("id", "toc")
		:addClass("toc plainlinks")
		:attr("summary", "Contents")
		:attr("cellpadding", "5")
		:attr("border", "yes")
		:css("text-align", "center")
		:tag("tr")
			:tag("th")
				:attr("colspan", #args[1])
				:wikitext("Contents (" .. link(nil, "Top") .. ")")
				:allDone()
	
	local row = output:tag("tr")
	local cell = {}
	for i, char in ipairs(args[1]) do
		local target = lang:makeSortKey(char)
		table.insert(cell, link(queryType, target, char, lang_code, script))
	end
	cell = row:tag("td"):wikitext(table.concat(cell, " "))
		:attr("colspan", #args[1])
	
	for i, char1 in ipairs(args[1]) do
		local row = output:tag("tr")
			:css("text-align", "center")
		for i, char2 in ipairs(args[1]) do
			local cell = row:tag("td")
			local display = char1 .. char2
			if lang:hasDottedDotlessI() then
				display = display
					:gsub("İ", "i")
					:gsub("I", "ı")
			end
			display = display:ulower()
			local target = lang:makeSortKey(display)
			cell = cell:wikitext(link(queryType, target, display, lang_code, script))
		end
	end
	
	local row = output:tag("tr")
	local cell = {}
	for i = 0, 9 do
		local display = tostring(i)
		local target = lang:makeSortKey(display)
		table.insert(cell, link(queryType, target, display, lang_code, script))
	end
	cell = row:tag("td"):wikitext(table.concat(cell, " "))
		:attr("colspan", #args[1])
	
	return tostring(output)
end

return export