Module:auto cat: Difference between revisions

ਕੋਈ ਸੋਧ ਸਾਰ ਨਹੀਂ
No edit summary
No edit summary
Line 1: Line 1:
local export = {}
local export = {}


local submodules = {"languages", "rhymes", "topics"}
-- %s = any string
local handlers = {}
-- %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"


local function format_categories(categories)
function get_lua_pattern_from_pattern(pattern)
local cats = ""
pattern = string.gsub(pattern, "%s", "(.+)")
for _, cat in ipairs(categories) do
pattern = string.gsub(pattern, "%l", "([%a-]+)")
cats = cats .. "[[" .. cat .. "]]"
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 cats
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
for _, m in ipairs(submodules) do
local categories = nil
local handler = require(m).handler
if handler then table.insert(handlers, handler) end
for pattern, categories in ipairs(pattern_matches) do
end
lua_pattern = get_lua_pattern_from_pattern(pattern)
for _, handler in ipairs(handlers) do
if string.find(category_name, lua_pattern) ~= nil then
r_cats, r_match = handler(title_obj)
categories = process_category(pattern, category_name)
if r_match then
break
return format_categories(r_cats)
end
end
end
end
error("{{auto cat}} could not recognise the format of this category name")
if categories == nil then
categories = {no_match}
end
return categories_to_string(categories)
end
end


return export
return export