Module:utilities/templates: Difference between revisions

From Linguifex
Jump to navigation Jump to search
No edit summary
No edit summary
 
Line 1: Line 1:
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 concat = table.concat
local insert = table.insert
local insert = table.insert
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


local export = {}
-- 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}}
-- Used by {{categorize}}, {{catlangname}} and {{topics}}.
function export.template_categorize(frame)
function export.categorize(frame)
local args = frame:getParent().args
local args = process_params(frame:getParent().args, {
local format = frame.args["format"]
[1] = {required = true, type = "language", default = "und", sublist = true},
args = require("Module:parameters").process(args, {
[1] = {required = true, type = "language", default = "und"},
[2] = {required = true, list = true, allow_holes = true},
[2] = {required = true, list = true, allow_holes = true},
["sort"] = {list = true, separate_no_index = true, allow_holes = true},
sort = {list = true, separate_no_index = true, allow_holes = true},
force = {type = "boolean"},
})
})
local lang = args[1]
local langs = args[1]
if not lang then
if not langs[1] then
return ""
return ""
end
end
local raw_cats = args[2]
 
local sort_keys = args.sort
local parts = {}
local default_sort = sort_keys.default
for _, lang in ipairs(langs) do
local cats = {}
local full_langcode = lang:getFullCode()
local prefix = format == "pos" and lang:getCanonicalName() .. " " or
if lang:getCode() ~= full_langcode then
format == "topic" and lang:getCode() .. ":" or ""
track("Module:utilities/templates/categorize called with variant langcode")
end
local cats_with_sort_keys = {}
for i = 1, raw_cats.maxindex do
local raw_cats, sort_keys, format = args[2], args.sort, frame.args["format"]
local cat = raw_cats[i]
local default_sort = sort_keys.default
if cat then
local prefix = format == "pos" and lang:getFullName() .. " " or format == "topic" and full_langcode .. ":" or ""
cat = prefix .. cat
insert(cats, cat)
-- Put the categories in an array. If any have an individual sortkey, they
local sort_key = sort_keys[i]
-- will need to be tables with the category and sort key for
if #cats_with_sort_keys > 0 then
-- [[Module:utilities/format_categories_with_sort_keys]]; otherwise, add
insert(cats_with_sort_keys, {
-- them as strings.
category = cat,
local cats, n, with_sort_keys = {}, 0, false
sort_key = sort_key
for i = 1, raw_cats.maxindex do
})
local cat = raw_cats[i]
elseif sort_key then
if cat ~= nil then
for j = 1, #cats - 1 do
cat = prefix .. cat
insert(cats_with_sort_keys, {category = cats[j]})
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
end
insert(cats_with_sort_keys, {
n = n + 1
category = cat,
cats[n] = cat
sort_key = sort_key
})
end
end
end
end
end
if #cats_with_sort_keys > 0 then
if with_sort_keys then
return require("Module:utilities/format_categories_with_sort_keys")(cats_with_sort_keys, lang, default_sort)
insert(parts, format_categories_with_sort_keys(cats, lang, default_sort, nil, args.force))
else
else
return require("Module:utilities").format_categories(cats, lang, default_sort)
insert(parts, format_categories(cats, lang, default_sort, nil, args.force))
end
end
end
return concat(parts)
end
end


return export
return export

Latest revision as of 14:44, 7 May 2026



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 concat = table.concat
local insert = table.insert
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", sublist = true},
		[2] = {required = true, list = true, allow_holes = true},
		sort = {list = true, separate_no_index = true, allow_holes = true},
		force = {type = "boolean"},
	})
	
	local langs = args[1]
	if not langs[1] then
		return ""
	end

	local parts = {}
	for _, lang in ipairs(langs) do
		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
			insert(parts, format_categories_with_sort_keys(cats, lang, default_sort, nil, args.force))
		else
			insert(parts, format_categories(cats, lang, default_sort, nil, args.force))
		end
	end

	return concat(parts)
end

return export