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 d_languages = mw.loadData("Module:languages/data")
-- Alternative to mw.ustring.gsub that is neater to call in code
-- and can handle multiple replacements at once.
local function replace(str, patt, rpl, patt2, rpl2, patt3, rpl3)
    local res = tostring(mw.ustring.gsub(str, patt, rpl))
    if patt2 then
        res = tostring(mw.ustring.gsub(res, patt2, rpl2))
    end
    if patt3 then
        res = tostring(mw.ustring.gsub(res, patt3, rpl3))
    end
    return res
end
-- Sanitizes a string containing hyphens so that the hyphens
-- do not count as Lua pattern special characters
local function sanitize(str)
    return tostring(mw.ustring.gsub(str, "%-", "%-"))
end
-- Takes a Lua pattern and a table of categories and returns
-- the auto-cat-style table format required, containing the
-- given data
local function to_pattern(pattern, categories)
    return {"^" .. pattern .. "$", categories}
end
-- Expands a pattern containing %l (and optionally %u), adding versions
-- of the pattern for each possible language
local function hydrate_pattern_l(out_patterns, p_lua, p_categories, also_u)
    for _, l_data in pairs(d_languages) do
        if also_u then
            for _, l2_data in pairs(d_languages) do
                if l2_data.code ~= l_data.code then
                    local p_out_cats = {}
                    for _, p_cat in ipairs(p_categories) do
                        table.insert(p_out_cats, replace(p_cat, "%%l", l_data.name, "%%u", l2_data.name))
                    end
                    table.insert(out_patterns, to_pattern(replace(p_lua, "%%l", sanitize(l_data.name), "%%u", sanitize(l2_data.name)), p_out_cats))
                end
            end
        else
            local p_out_cats = {}
            for _, p_cat in ipairs(p_categories) do
                table.insert(p_out_cats, replace(p_cat, "%%l", l_data.name))
            end
            table.insert(out_patterns, to_pattern(replace(p_lua, "%%l", sanitize(l_data.name)), p_out_cats))
        end
    end
end
local function hydrate_pattern_c(out_patterns, p_lua, p_categories)
    for l_code, l_data in pairs(d_languages) do
        local p_out_cats = {}
        for _, p_cat in ipairs(p_categories) do
            table.insert(p_out_cats, replace(p_cat, "%%l", l_data.name, "%%c", l_code))
        end
        table.insert(out_patterns, to_pattern(replace(p_lua, "%%c", l_code), p_out_cats))
    end
end


local function hydrate_patterns()
local function hydrate_patterns()
    local languages = mw.loadData("Module:languages/data")
     local out_patterns = {}
     local out_patterns = {}
     for _, p_data in ipairs(mw.loadData("Module:auto cat/data")) do
     for _, p_data in ipairs(mw.loadData("Module:auto cat/data")) do
         local p_template, p_results = p_data[1], p_data[2]
         local p_template, p_categories = p_data[1], p_data[2]
         local p_lua = mw.ustring.gsub(tostring(mw.ustring.gsub(p_template, "%-", "%-")), "%%s", "(..-)")
         local p_lua = replace(sanitize(p_template), "%%s", "(..-)")
         if mw.ustring.find(p_template, "%%l") ~= nil then
         if mw.ustring.find(p_template, "%%l") ~= nil then
             for _, l_data in pairs(languages) do
             hydrate_pattern_l(out_patterns, p_lua, p_categories, mw.ustring.find(p_template, "%%u") ~= nil)
                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
                table.insert(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
         elseif mw.ustring.find(p_template, "%%c") ~= nil then
             for l_code, l_data in ipairs(l_codes) do
             hydrate_pattern_c(out_patterns, p_lua, p_categories)
                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
                table.insert(out_patterns, {"^"..mw.ustring.gsub(p_lua, "%%c", l_code).."$", p_out_results})
            end
         else
         else
             table.insert(out_patterns, {"^"..p_lua.."$", p_results})
             table.insert(out_patterns, to_pattern(p_lua, p_categories))
         end
         end
     end
     end
Line 30: Line 80:
local function process_category(category_name, p_lua, p_categories)
local function process_category(category_name, p_lua, p_categories)
     local out = ""
     local out = ""
     local is_s = mw.ustring.find(category_name, "%(%.%.%-%)") ~= nil
     local is_s = mw.ustring.find(p_lua, "%(%.%.%-%)") ~= nil
     local _, _, s_match = mw.ustring.find(category_name, p_lua)
     local _, _, s_match = mw.ustring.find(category_name, p_lua)
     for _, p_category in ipairs(p_categories) do
     for _, p_category in ipairs(p_categories) do
         out = out .. "[[Category:" .. (is_s and tostring(mw.ustring.gsub(p_category, "%%s", s_match)) or p_category) .. "]]"
         out = out .. "[[Category:" .. (is_s and replace(p_category, "%%s", s_match) or p_category) .. "]]"
     end
     end
     return out
     return out
Line 46: Line 96:
         end
         end
     end
     end
   
 
     return "[[Category:Category pages not matched by auto cat]]"
     return "[[Category:Category pages not matched by auto cat]]"
end
end

Navigation menu