Module:auto cat: Difference between revisions

Jump to navigation Jump to search
Undoing it all I can't be bothered
No edit summary
Tag: Reverted
(Undoing it all I can't be bothered)
Tags: Manual revert Reverted
Line 1: Line 1:
local export = {}
local export = {}
local d_languages = mw.loadData("Module:languages/data")
local d_languages = mw.loadData("Module:languages/data")
local d = mw.loadData("Module:auto cat/data")
local d_cats, d_pos = d.cats, d.pos


-- Alternative to mw.ustring.gsub that is neater to call in code
-- Alternative to mw.ustring.gsub that is neater to call in code
-- and can handle multiple replacements at once.
-- and can handle multiple replacements at once.
local function replace(str, gsubs)
local function replace(str, patt, rpl, patt2, rpl2, patt3, rpl3)
     for from, to in pairs(gsubs) do
     local res = tostring(mw.ustring.gsub(str, patt, rpl))
    str = tostring(mw.ustring.gsub(str, from, to))
    if patt2 then
        res = tostring(mw.ustring.gsub(res, patt2, rpl2))
     end
     end
     return str
    if patt3 then
        res = tostring(mw.ustring.gsub(res, patt3, rpl3))
    end
     return res
end
end


Line 24: Line 26:
local function to_pattern(pattern, categories)
local function to_pattern(pattern, categories)
     return {"^" .. pattern .. "$", categories}
     return {"^" .. pattern .. "$", categories}
end
-- provides initial capitalisation
local function capitalize(word)
if mw.ustring.len(word) > 1 then
return mw.ustring.upper(mw.ustring.sub(word, 1, 1)) .. mw.ustring.sub(word, 2)
elseif mw.ustring.len(word) == 1 then
return mw.ustring.upper(word)
end
error("Empty input for capitalisation")
end
local function pluralize(word)
local ending = mw.ustring.sub(word, -1)
if ending == "h" and mw.ustring.sub(word, -2, -1) == "s" then ending = "sh" end
if ending == "s" or ending == "z" or ending == "x" or ending == "sh" then
return word .. "es"
end
return word .. "s"
end
end


-- Expands a pattern containing %l (and optionally %u), adding versions
-- Expands a pattern containing %l (and optionally %u), adding versions
-- of the pattern for each possible language
-- of the pattern for each possible language
local function hydrate_pattern_l(out_patterns, p_lua, p_categories, also_u, also_p)
local function hydrate_pattern_l(out_patterns, p_lua, p_categories, also_u)
     for _, l_data in pairs(d_languages) do
     for _, l_data in pairs(d_languages) do
    if also_p then
        if also_u then
    for _, pos in ipairs(d_pos) do
            for _, l2_data in pairs(d_languages) do
    if also_u then
                if l2_data.code ~= l_data.code then
            for _, l2_data in pairs(d_languages) do
                    local p_out_cats = {}
                if l2_data.code ~= l_data.code then
                    for _, p_cat in ipairs(p_categories) do
                    local p_out_cats = {}
                        table.insert(p_out_cats, replace(p_cat, "%%l", l_data.name, "%%u", l2_data.name))
                    for _, p_cat in ipairs(p_categories) do
                    end
                    table.insert(p_out_cats, capitalize(replace(p_cat, {["%%l"] = l_data.name, ["%%u"] = l2_data.name, ["%%ps"] = pluralize(pos), ["%%p"] = pos})))
                    table.insert(out_patterns, to_pattern(replace(p_lua, "%%l", sanitize(l_data.name), "%%u", sanitize(l2_data.name)), p_out_cats))
                    end
                end
                    table.insert(out_patterns, to_pattern(capitalize(replace(p_lua, {["%%l"] = sanitize(l_data.name), ["%%u"] = sanitize(l2_data.name), ["%%ps"] = pluralize(pos), ["%%p"] = pos})), p_out_cats))
            end
                end
        else
            end
            local p_out_cats = {}
        else
            for _, p_cat in ipairs(p_categories) do
            local p_out_cats = {}
                table.insert(p_out_cats, replace(p_cat, "%%l", l_data.name))
            for _, p_cat in ipairs(p_categories) do
            end
            table.insert(p_out_cats, capitalize(replace(p_cat, {["%%l"] = l_data.name, ["%%ps"] = pluralize(pos), ["%%p"] = pos})))
            table.insert(out_patterns, to_pattern(replace(p_lua, "%%l", sanitize(l_data.name)), p_out_cats))
            end
            table.insert(out_patterns, to_pattern(capitalize(replace(p_lua, {["%%l"] = sanitize(l_data.name), ["%%ps"] = pluralize(pos), ["%%p"] = pos})), p_out_cats))
        end
    end
    else
        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
     end
     end
Line 95: Line 56:
         local p_out_cats = {}
         local p_out_cats = {}
         for _, p_cat in ipairs(p_categories) do
         for _, p_cat in ipairs(p_categories) do
             table.insert(p_out_cats, replace(p_cat, {["%%l"] = l_data.name, ["%%c"] = l_code}))
             table.insert(p_out_cats, replace(p_cat, "%%l", l_data.name, "%%c", l_code))
         end
         end
         table.insert(out_patterns, to_pattern(replace(p_lua, {["%%c"] = l_code}), p_out_cats))
         table.insert(out_patterns, to_pattern(replace(p_lua, "%%c", l_code), p_out_cats))
     end
     end
end
local function hydrate_pattern_p(out_patterns, p_lua, p_categories)
    for _, pos in ipairs(d_pos) do
    local p_out_cats = {}
        for _, p_cat in ipairs(p_categories) do
            table.insert(p_out_cats, capitalize(replace(p_cat, {["%%ps"] = pluralize(pos), ["%%p"] = pos})))
        end
        table.insert(out_patterns, to_pattern(capitalize(replace(p_lua, {["%%ps"] = pluralize(pos), ["%%p"] = pos})), p_out_cats))
end
end
end


local function hydrate_patterns()
local function hydrate_patterns()
     local out_patterns = {}
     local out_patterns = {}
     for _, p_data in ipairs(d_cats) do
     for _, p_data in ipairs(mw.loadData("Module:auto cat/data")) do
         local p_template, p_categories = p_data[1], p_data[2]
         local p_template, p_categories = p_data[1], p_data[2]
         local p_lua = replace(sanitize(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
             hydrate_pattern_l(out_patterns, p_lua, p_categories, mw.ustring.find(p_template, "%%u") ~= nil, mw.ustring.find(p_template, "%%p") ~= nil)
             hydrate_pattern_l(out_patterns, p_lua, p_categories, mw.ustring.find(p_template, "%%u") ~= nil)
         elseif mw.ustring.find(p_template, "%%c") ~= nil then
         elseif mw.ustring.find(p_template, "%%c") ~= nil then
hydrate_pattern_c(out_patterns, p_lua, p_categories)
            hydrate_pattern_c(out_patterns, p_lua, p_categories)
elseif mw.ustring.find(p_template, "%%p") ~= nil then
            hydrate_pattern_p(out_patterns, p_lua, p_categories)
         else
         else
             table.insert(out_patterns, to_pattern(p_lua, p_categories))
             table.insert(out_patterns, to_pattern(p_lua, p_categories))
Line 134: Line 83:
     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 replace(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

Navigation menu