local export = {}
-- Used by {{categorize}}
function export.template_categorize(frame)
local NAMESPACE = mw.title.getCurrentTitle().nsText
local format = frame.args["format"]
local args = frame:getParent().args
local langcode = args[1]; if langcode == "" then langcode = nil end
local sort_key = args["sort"]; if sort_key == "" then sort_key = nil end
local categories = {}
if not langcode then
if NAMESPACE == "Template" then return "" end
error("Language code has not been specified. Please pass parameter 1 to the template.")
end
local lang = require("Module:languages").getByCode(langcode)
if not lang then
if NAMESPACE == "Template" then return "" end
error("The language code \"" .. langcode .. "\" is not valid.")
end
local prefix = ""
local i = 1
local individual_sort_keys = {}
local has_individual_sort_keys = false
if format == "pos" then
prefix = lang:getCanonicalName()
while args[i + 1] do
local cat = args[i + 1]
local individual_sort_key = args["sort" .. i]
if cat ~= "" then
-- 如果cat里有'的'就把prefix插到最后一个'的'字的后面,否则把prefix插到cat的前面
rindex_de = mw.ustring.find(cat, '的[^的]*$')
if rindex_de and not no_prefix_fix then
table.insert(categories, mw.ustring.sub(cat, 1, rindex_de) .. prefix .. mw.ustring.sub(cat, rindex_de + 1))
else
table.insert(categories, prefix .. cat)
end
end
if individual_sort_key then
individual_sort_keys[#categories] = individual_sort_key
has_individual_sort_keys = true
end
i = i + 1
cat = args[i]
end
elseif format == "topic" then
prefix = lang:getCanonicalName() .. " "
while args[i + 1] do
local cat = args[i + 1]
local individual_sort_key = args["sort" .. i]
if cat ~= "" then
table.insert(categories, prefix .. cat)
end
if individual_sort_key then
individual_sort_keys[#categories] = individual_sort_key
has_individual_sort_keys = true
end
i = i + 1
cat = args[i]
end
else
while args[i + 1] do
local cat = args[i + 1]
local individual_sort_key = args["sort" .. i]
if cat ~= "" then
table.insert(categories, cat)
if individual_sort_key then
individual_sort_keys[#categories] = individual_sort_key
has_individual_sort_keys = true
end
end
i = i + 1
end
end
if has_individual_sort_keys then
local categories_with_sort_keys = {}
for i, category in ipairs(categories) do
table.insert(categories_with_sort_keys, { category = category, sort_key = individual_sort_keys[i] })
end
return require("Module:utilities/format_categories_with_sort_keys")(categories_with_sort_keys, lang, sort_key)
else
return require("Module:utilities/format_categories")(categories, lang, sort_key)
end
end
return export