local export = {}
local function hydrate_patterns()
local languages = mw.loadData("Module:languages/data")
local out_patterns = {}
for _, p_data in ipairs(mw.loadData("Module:auto cat/data")) do
local p_template, p_results = p_data[1], p_data[2]
local p_lua = mw.ustring.gsub(tostring(mw.ustring.gsub(p_template, "%-", "%-")), "%%s", "(..-)")
if mw.ustring.find(p_template, "%%l") ~= nil then
for _, l_data in pairs(languages) do
local p_out_results = {}
for _, p_result in ipairs(p_results) do table.insert(p_out_results, tostring(mw.ustring.gsub(p_result, "%%l", l_data.name))) end
table.insert(out_patterns, {"^"..mw.ustring.gsub(p_lua, "%%l", tostring(mw.ustring.gsub(l_data.name, "%-", "%-"))).."$", p_out_results})
end
elseif mw.ustring.find(p_template, "%%c") ~= nil then
for l_code, l_data in ipairs(l_codes) do
local p_out_results = {}
for _, p_result in ipairs(p_results) do
table.insert(p_out_results, tostring(mw.ustring.gsub(tostring(mw.ustring.gsub(p_result, "%%c", l_code)), "%%l", l_data.name)))
end
table.insert(out_patterns, {"^"..mw.ustring.gsub(p_lua, "%%c", l_code).."$", p_out_results})
end
else
table.insert(out_patterns, {"^"..p_lua.."$", p_results})
end
end
return out_patterns
end
local function process_category(category_name, p_lua, p_categories)
local out = ""
local is_s, _, s_match = mw.ustring.find(category_name, p_lua)
for _, p_category in ipairs(p_categories) do
out = out .. "[[Category:" .. (is_s ~= nil and tostring(mw.ustring.gsub(p_category, "%%s", s_match)) or p_category) .. "]]"
end
return out
end
function export.auto_cat(category_name)
local lua_patterns = hydrate_patterns()
for _, p_data in ipairs(lua_patterns) do
local p_lua, p_categories = p_data[1], p_data[2]
if mw.ustring.find(category_name, p_lua) ~= nil then
return process_category(category_name, p_lua, p_categories)
end
end
return "[[Category:Category pages not matched by auto cat]]"
end
function export.show(frame)
local title_obj = mw.title.getCurrentTitle()
if title_obj.nsText ~= "Category" then
error("{{auto cat}} can only be used on pages in the Category namespace")
end
return export.auto_cat(title_obj.text)
end
return export