Module:Category handler: Difference between revisions
m comments |
add namespace alias handling, so main-namespace can be specified |
||
| Line 26: | Line 26: | ||
-- Adjust as needed! | -- Adjust as needed! | ||
local DEFAULT_SUPPRESSED_NAMESPACES = { | local DEFAULT_SUPPRESSED_NAMESPACES = { | ||
["main"] = false, | |||
["talk"] = true, | ["talk"] = true, | ||
["user"] = true, | ["user"] = true, | ||
| Line 49: | Line 50: | ||
"sandbox", | "sandbox", | ||
} | } | ||
-- List of namespace aliases | |||
-- For "main", this is so editors can type in "main" since the main namespace's | |||
-- actual name is "". For "xw"-related namespaces, these are shorthands. | |||
local NAMESPACE_ALIASES = { | |||
[""] = "main", -- "" is treated as "main" | |||
["xw"] = "xenharmonic wiki", -- Shorthand | |||
["xw talk"] = "xenharmonic wiki talk" -- Shorthand | |||
} | |||
-- Helper function | |||
-- Converts namespace aliases to their actual names | |||
-- Must be placed before is_suppressed_namespace() | |||
local function normalize_ns(ns) | |||
ns = mw.ustring.lower(mw.text.trim(ns or "")) -- Convert to lowercase and and trim extra spaces | |||
if ns == "" then ns = "main" end -- Empty-string is assumed to be "main" | |||
if NAMESPACE_ALIASES[ns] then | |||
return NAMESPACE_ALIASES[ns] | |||
end | |||
return ns | |||
end | |||
-- Helper function: check if current namespace is excluded | -- Helper function: check if current namespace is excluded | ||
-- Accepts an optional table, containing or overriding other namespaces' rules | -- Accepts an optional table, containing or overriding other namespaces' rules | ||
local function is_suppressed_namespace(ns_override) | local function is_suppressed_namespace(ns_override) | ||
-- Get current namespace, as lowercase | -- Get current namespace, or aliased namespace, as lowercase | ||
local curr_ns = | local curr_ns = normalize_ns(mw.title.getCurrentTitle().nsText) | ||
-- Build table of suppressed namespaces; start with default list | -- Build table of suppressed namespaces; start with default list | ||
local namespaces = {} | local namespaces = {} | ||
for k, v in pairs(DEFAULT_SUPPRESSED_NAMESPACES) do | for k, v in pairs(DEFAULT_SUPPRESSED_NAMESPACES) do | ||
namespaces[k] = v | namespaces[normalize_ns(k)] = v | ||
end | end | ||
| Line 65: | Line 87: | ||
if type(ns_override) == "table" then | if type(ns_override) == "table" then | ||
for k, v in pairs(ns_override) do | for k, v in pairs(ns_override) do | ||
namespaces[k] = v | namespaces[normalize_ns(k)] = v | ||
end | end | ||
end | end | ||