Module:utilities/templates: Difference between revisions
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 | -- 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. | function export.categorize(frame) | ||
local args = frame:getParent(). | local args = process_params(frame:getParent().args, { | ||
[1] = {required = true, type = "language", default = "und", sublist = true}, | |||
[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}, | |||
force = {type = "boolean"}, | |||
}) | }) | ||
local | local langs = args[1] | ||
if not | if not langs[1] then | ||
return "" | return "" | ||
end | end | ||
local raw_cats = args[2] | |||
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 | end | ||
n = n + 1 | |||
cats[n] = cat | |||
end | end | ||
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 | end | ||
return concat(parts) | |||
end | end | ||
return export | return export | ||
Latest revision as of 14:44, 7 May 2026
- This module lacks a documentation subpage. Please create it.
- Useful links: root page • root page’s subpages • links • transclusions • testcases • sandbox
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