Module:utilities/templates: Difference between revisions

From Linguifex
Jump to navigation Jump to search
(Created page with "local insert = table.insert local export = {} -- Used by {{categorize}} function export.template_categorize(frame) local args = frame:getParent().args local format = frame.args["format"] args = require("Module:parameters").process(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...")
 
No edit summary
 
(2 intermediate revisions by the same user not shown)
Line 22: Line 22:
local default_sort = sort_keys.default
local default_sort = sort_keys.default
local cats = {}
local cats = {}
local prefix = format == "pos" and lang:getFullName() .. " " or
local prefix = format == "pos" and lang:getCanonicalName() .. " " or
format == "topic" and lang:getFullCode() .. ":" or ""
format == "topic" and lang:getCode() .. ":" or ""
local cats_with_sort_keys = {}
local cats_with_sort_keys = {}

Latest revision as of 23:16, 7 August 2024

Documentation for this module may be created at Module:utilities/templates/doc

local insert = table.insert

local export = {}

-- Used by {{categorize}}
function export.template_categorize(frame)
	local args = frame:getParent().args
	local format = frame.args["format"]
	
	args = require("Module:parameters").process(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 raw_cats = args[2]
	local sort_keys = args.sort
	local default_sort = sort_keys.default
	local cats = {}
	local prefix = format == "pos" and lang:getCanonicalName() .. " " or
		format == "topic" and lang:getCode() .. ":" or ""
	
	local cats_with_sort_keys = {}
	for i = 1, raw_cats.maxindex do
		local cat = raw_cats[i]
		if cat then
			cat = prefix .. cat
			insert(cats, cat)
			local sort_key = sort_keys[i]
			if #cats_with_sort_keys > 0 then
				insert(cats_with_sort_keys, {
					category = cat,
					sort_key = sort_key
				})
			elseif sort_key then
				for j = 1, #cats - 1 do
					insert(cats_with_sort_keys, {category = cats[j]})
				end
				insert(cats_with_sort_keys, {
					category = cat,
					sort_key = sort_key
				})
			end
		end
	end
	
	if #cats_with_sort_keys > 0 then
		return require("Module:utilities/format_categories_with_sort_keys")(cats_with_sort_keys, lang, default_sort)
	else
		return require("Module:utilities").format_categories(cats, lang, default_sort)
	end
end

return export