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 = {} | ||
local | -- %s = any string | ||
local | -- %l = any language name | ||
-- %c = any language code | |||
local pattern_matches = { | |||
["%s by language"] = {"Fundamental"}, | |||
["%l %s"] = {"%s by language", "%l language"}, | |||
} | |||
local no_match = "Category pages not matched by auto cat" | |||
function get_lua_pattern_from_pattern(pattern) | |||
local | pattern = string.gsub(pattern, "%s", "(.+)") | ||
for _, | pattern = string.gsub(pattern, "%l", "([%a-]+)") | ||
pattern = string.gsub(pattern, "%c", "([%l-]+)") | |||
return "^" .. pattern .. "$" | |||
end | |||
function process_category(pattern, category_name) | |||
-- TODO: make this work | |||
return {} | |||
end | |||
function categories_to_string(categories) | |||
local out = "" | |||
for _, category in ipairs(categories) do | |||
out = out .. "[[Category:" .. category .. "]]" | |||
end | end | ||
return | return out | ||
end | end | ||
Line 17: | Line 35: | ||
error("{{auto cat}} can only be used on pages in the Category: namespace") | error("{{auto cat}} can only be used on pages in the Category: namespace") | ||
end | end | ||
local category_name = title_obj.text | |||
local categories = nil | |||
for pattern, categories in ipairs(pattern_matches) do | |||
lua_pattern = get_lua_pattern_from_pattern(pattern) | |||
for | if string.find(category_name, lua_pattern) ~= nil then | ||
categories = process_category(pattern, category_name) | |||
if | break | ||
end | end | ||
end | end | ||
if categories == nil then | |||
categories = {no_match} | |||
end | |||
return categories_to_string(categories) | |||
end | end | ||
return export | return export |
Revision as of 13:46, 5 February 2024
local export = {}
-- %s = any string
-- %l = any language name
-- %c = any language code
local pattern_matches = {
["%s by language"] = {"Fundamental"},
["%l %s"] = {"%s by language", "%l language"},
}
local no_match = "Category pages not matched by auto cat"
function get_lua_pattern_from_pattern(pattern)
pattern = string.gsub(pattern, "%s", "(.+)")
pattern = string.gsub(pattern, "%l", "([%a-]+)")
pattern = string.gsub(pattern, "%c", "([%l-]+)")
return "^" .. pattern .. "$"
end
function process_category(pattern, category_name)
-- TODO: make this work
return {}
end
function categories_to_string(categories)
local out = ""
for _, category in ipairs(categories) do
out = out .. "[[Category:" .. category .. "]]"
end
return out
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
local category_name = title_obj.text
local categories = nil
for pattern, categories in ipairs(pattern_matches) do
lua_pattern = get_lua_pattern_from_pattern(pattern)
if string.find(category_name, lua_pattern) ~= nil then
categories = process_category(pattern, category_name)
break
end
end
if categories == nil then
categories = {no_match}
end
return categories_to_string(categories)
end
return export