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

มอดูล:utilities/templates

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

local export = {}

local debug_track_module = "Module:debug/track"
local parameters_module = "Module:parameters"
local utilities_module = "Module:utilities"
local utilities_format_categories_with_sort_keys_module = "Module:utilities/format_categories_with_sort_keys"

local require = require

local function format_categories(...)
	format_categories = require(utilities_module).format_categories
	return format_categories(...)
end

local function format_categories_with_sort_keys(...)
	format_categories_with_sort_keys = require(utilities_format_categories_with_sort_keys_module)
	return format_categories_with_sort_keys(...)
end

local function process_params(...)
	process_params = require(parameters_module).process
	return process_params(...)
end

local function track(...)
	track = require(debug_track_module)
	return track(...)
end

-- Used by {{catfix}}.
function export.catfix(frame)
	local args = process_params(frame:getParent().args, {
		[1] = {type = "language", required = true},
		[2] = {alias_of = "sc"},
		["sc"] = {type = "script"},
	})
	return require("Module:utilities").catfix(args[1], args.sc)
end

-- Used by {{categorize}}, {{catlangname}} and {{topics}}.
function export.categorize(frame)
	local args = process_params(frame:getParent().args, {
		[1] = {required = true, type = "language", default = "und"},
		[2] = {required = true, list = true, allow_holes = true},
		["sort"] = {list = true, separate_no_index = true, allow_holes = true},
	})
	
	local lang = args[1]
	if not lang then
		return ""
	end
	
	local full_langcode = lang:getFullCode()
	if lang:getCode() ~= full_langcode then
		track("Module:utilities/templates/categorize called with variant langcode")
	end
	
	local raw_cats, sort_keys, format = args[2], args.sort, frame.args["format"]
	local default_sort = sort_keys.default
	local prefix = format == "pos" and lang:getFullName() .. " " or format == "topic" and full_langcode .. ":" or ""
	
	-- Put the categories in an array. If any have an individual sortkey, they
	-- will need to be tables with the category and sort key for
	-- [[Module:utilities/format_categories_with_sort_keys]]; otherwise, add
	-- them as strings.
	local cats, n, with_sort_keys = {}, 0, false
	for i = 1, raw_cats.maxindex do
		local cat = raw_cats[i]
		if cat ~= nil then
			cat = prefix .. cat
			local sort_key = sort_keys[i]
			if with_sort_keys then
				cat = {category = cat, sort_key = sort_key}
			-- If a sort key exists, reformat all previously-processed
			-- categories into the table format.
			elseif sort_key ~= nil then
				with_sort_keys = true
				for j = 1, n do
					cats[j] = {category = cats[j]}
				end
				cat = {category = cat, sort_key = sort_key}
			end
			n = n + 1
			cats[n] = cat
		end
	end

	if with_sort_keys then
		return format_categories_with_sort_keys(cats, lang, default_sort)
	end
	return format_categories(cats, lang, default_sort)
end

return export