Module:auto cat: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
No edit summary |
||
Line 1: | Line 1: | ||
local export = {} | local export = {} | ||
-- %l = any language name | -- %l = any language name | ||
-- %c = any language code | -- %c = any language code | ||
-- %s = any string | |||
local patterns = mw.loadData("Module:auto cat/data") | |||
local no_match = "Category pages not matched by auto cat" | local no_match = "Category pages not matched by auto cat" | ||
function | function hydrate_patterns() | ||
local languages = mw.loadData("Module:languages/data") | |||
local out_patterns = {} | |||
for p_template, p_results in pairs(patterns) do | |||
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 | |||
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 | |||
out_patterns["^"..mw.ustring.gsub(p_lua, "%%c", l_code).."$"] = p_out_results | |||
end | |||
else | |||
out_patterns["^"..p_lua.."$"] = p_results | |||
end | |||
end | |||
return out_patterns | |||
end | end | ||
function process_category( | function process_category(category_name, p_lua, p_categories) | ||
local out = "" | |||
local is_s, _, _, s_match = mw.ustring.find(p_lua, "%%s") ~= nil | |||
for _, p_category in ipairs(p_categories) do | |||
out = out .. "[[Category:" .. (is_s and tostring(mw.ustring.gsub(this_cat, "%%s", s_match)) or p_category) .. "]]" | |||
end | |||
return out | |||
end | end | ||
function | function export.auto_cat(category_name) | ||
local lua_patterns = hydrate_patterns() | |||
for p_lua, p_categories in pairs(lua_patterns) do | |||
if mw.ustring.find(category_name, p_lua) ~= nil then | |||
return process_category(category_name, p_lua, p_categories) | |||
end | |||
end | |||
return "[[Category:" .. no_match .. "]]" | |||
end | end | ||
function export.show(frame) | 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 | end | ||
return export | return export |
Revision as of 14:26, 26 February 2024
local export = {}
-- %l = any language name
-- %c = any language code
-- %s = any string
local patterns = mw.loadData("Module:auto cat/data")
local no_match = "Category pages not matched by auto cat"
function hydrate_patterns()
local languages = mw.loadData("Module:languages/data")
local out_patterns = {}
for p_template, p_results in pairs(patterns) do
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
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
out_patterns["^"..mw.ustring.gsub(p_lua, "%%c", l_code).."$"] = p_out_results
end
else
out_patterns["^"..p_lua.."$"] = p_results
end
end
return out_patterns
end
function process_category(category_name, p_lua, p_categories)
local out = ""
local is_s, _, _, s_match = mw.ustring.find(p_lua, "%%s") ~= nil
for _, p_category in ipairs(p_categories) do
out = out .. "[[Category:" .. (is_s and tostring(mw.ustring.gsub(this_cat, "%%s", s_match)) or p_category) .. "]]"
end
return out
end
function export.auto_cat(category_name)
local lua_patterns = hydrate_patterns()
for p_lua, p_categories in pairs(lua_patterns) do
if mw.ustring.find(category_name, p_lua) ~= nil then
return process_category(category_name, p_lua, p_categories)
end
end
return "[[Category:" .. no_match .. "]]"
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