Module:auto cat: Difference between revisions

From Laenkea
Jump to navigation Jump to search
No edit summary
No edit summary
Line 1: Line 1:
local export = {}
local export = {}


-- %s = any string
 
-- %l = any language name
-- %l = any language name
-- %c = any language code
-- %c = any language code
local pattern_matches = {
-- %s = any string
["%s by language"] = {"Fundamental"},
local patterns = mw.loadData("Module:auto cat/data")
["%l %s"] = {"%s by language", "%l language"},
}
local no_match = "Category pages not matched by auto cat"
local no_match = "Category pages not matched by auto cat"


function get_lua_pattern_from_pattern(pattern)
function hydrate_patterns()
pattern = string.gsub(pattern, "%s", "(.+)")
    local languages = mw.loadData("Module:languages/data")
pattern = string.gsub(pattern, "%l", "([%a-]+)")
    local out_patterns = {}
pattern = string.gsub(pattern, "%c", "([%l-]+)")
    for p_template, p_results in pairs(patterns) do
return "^" .. pattern .. "$"
        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(pattern, category_name)
function process_category(category_name, p_lua, p_categories)
-- TODO: make this work
    local out = ""
return {}
    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 categories_to_string(categories)
function export.auto_cat(category_name)
local out = ""
    local lua_patterns = hydrate_patterns()
for _, category in ipairs(categories) do
    for p_lua, p_categories in pairs(lua_patterns) do
out = out .. "[[Category:" .. category .. "]]"
        if mw.ustring.find(category_name, p_lua) ~= nil then
end
            return process_category(category_name, p_lua, p_categories)
return out
        end
    end
   
    return "[[Category:" .. no_match .. "]]"
end
end


function export.show(frame)
function export.show(frame)
local title_obj = mw.title.getCurrentTitle()
    local title_obj = mw.title.getCurrentTitle()
if title_obj.nsText ~= "Category" then
    if title_obj.nsText ~= "Category" then
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
    return export.auto_cat(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
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