Capil ka isi

Modul:table of contents

Matan Wiktionary

Dokumentasi untuk modul ini dapat dibuat di Modul:table of contents/doc

-- Imported from enwiktionary
-- Source: https://en.wiktionary.org/wiki/Module:table of contents
-- License: CC BY-SA

local export = {}

local title = mw.title.getCurrentTitle()

local concat = table.concat
local html_create = mw.html.create
local insert = table.insert
local split = mw.text.split
local tostring = tostring

local function makeUrl(queryType, target)
	if title and title.fullUrl and target then
		return title:fullUrl(queryType and {[queryType] = target} or nil, "https")
	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 = 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 args = require("Module:parameters").process(frame.args[1] and frame.args or frame:getParent().args, {
		[1] = {list = true},
		["lang"] = {type = "language"},
		["subcat"] = {type = "boolean"},
		["top"] = true,
	})

	local lang, targets, displays = args.lang, args[1], {}

	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 displays_concat, script_code = concat(displays)
	if lang then
		script_code = lang:findBestScript(displays_concat):getCode()
	elseif displays_concat ~= "" then
		script_code = require("Module:scripts").findBestScriptWithoutLang(displays_concat):getCode()
	end

	local subcat, queryType = args.subcat
	if subcat then
		queryType = "subcatfrom"
		-- [[Special:WhatLinksHere/Wiktionary:Tracking/table-of-contents/subcat]]
		require("Module:debug").track("table-of-contents/subcat")
	else
		queryType = "from"
	end

	local output = {}
	if args.top then
		local link = link(nil, args.top)
		insert(output, link)
		if targets and targets[1] then
			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)
		insert(output, link)
	end

	if not lang then
		insert(output, "[[Category:User:Theknightwho/table of contents]]")
	end

	return concat(output, " ") .. require("Module:TemplateStyles")("Module:table of contents/style.css")
end

function export.full(frame)
	local args = require("Module:parameters").process(frame.args[1] and frame.args or frame:getParent().args, {
		[1] = {list = true},
		["char2"] = true,
		["lang"] = {type = "language", required = true},
		["subcat"] = {type = "boolean"}
	})

	local lang, targets = args.lang, args[1]

	local subcat, queryType = args.subcat
	if subcat then
		queryType = "subcatfrom"
		-- [[Special:WhatLinksHere/Wiktionary:Tracking/table-of-contents/subcat]]
		require("Module:debug").track("table-of-contents/subcat")
	else
		queryType = "from"
	end

	local output = html_create("table")
		:attr("id", "toc")
		:addClass("wikitable plainlinks categoryTOC-full")
		:attr("summary", "Contents")
		:css("text-align", "center")
		:tag("tr")
			:tag("th")
				:attr("colspan", #targets)
				:wikitext("Contents (" .. link(nil, "Top") .. ")")
				:allDone()

	local row, cell = output:tag("tr"), {}
	local lang_code = lang:getCode()
	for _, char in ipairs(targets) do
		local target = lang:makeSortKey(char)
		local script_code = lang:findBestScript(char):getCode()
		insert(cell, link(queryType, target, char, lang_code, script_code))
	end
	cell = row:tag("td"):wikitext(concat(cell, " "))
		:attr("colspan", #targets)

	local char2_list = args.char2 and split(args.char2, ",") or targets
	for _, char1 in ipairs(targets) do
		local row = output:tag("tr")
			:attr("class", "tocrow")
		for _, char2 in ipairs(char2_list) 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)
			local script_code = lang:findBestScript(display):getCode()
			cell = cell:wikitext(link(queryType, target, display, lang_code, script_code))
		end
	end

	row, cell = output:tag("tr"), {}
	for i = 0, 9 do
		local display = tostring(i)
		local target = lang:makeSortKey(display)
		local script_code = lang:findBestScript(display):getCode()
		insert(cell, link(queryType, target, display, lang_code, script_code))
	end
	cell = row:tag("td"):wikitext(concat(cell, " "))
		:attr("colspan", #targets)

	return tostring(output) .. require("Module:TemplateStyles")("Module:table of contents/style.css")
end

function export.box(frame)
	local args = require("Module:parameters").process(frame.args[1] and frame.args or frame:getParent().args, {
		[1] = {list = true, required = true}
	})
	return "{| id=\"toc\" class=\"wikitable plainlinks\" summary=\"Contents\"\n| '''Jump to:''' " .. concat(args[1], "\n|-\n| ") .. "\n|}"
end

return export