Module:auto cat: Difference between revisions

ਕੋਈ ਸੋਧ ਸਾਰ ਨਹੀਂ
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