Module:Sandbox/DarthKitty/Normalize Page Names

--

-- Takes the name of a page, makes it case-insensitive, and splits it into a -- prefix and root. If no prefix is given, the canonical namespace name is used -- instead.

local p = {}

-- Load dependencies local checkType = require("libraryUtil").checkType

-- Reduce table lookups local namespaces = mw.site.namespaces local uLower = mw.ustring.lower local lang = mw.getContentLanguage

-- Generic function to normalize the name of a page.

local function normalize(name, nsNumber, ...) checkType("normalize", 1, name, "string") checkType("normalize", 2, namespaceNumber, "number")

-- Generate a set of valid prefixes for a given namespace, `nsNumber`: -- `ns.canonicalName` is the canonical prefix -- `ns.name` is the prefix in your language -- `...` is an optional list of extra prefixes local ns = namespaces[nsNumber] local canonicalPrefix = ns.canonicalName local validPrefixes = {}

for i, v in ipairs{canonicalPrefix, ns.name, ...} do       if i > 2 then checkType("normalize", i, v, "string") end

validPrefixes[uLower(v) .. ":"] = true end

-- Split `name` into two parts: -- `prefix` - everything up to and including the first colon -- `root` - everything after `prefix` local prefix, root = name:match("^([^:]+:)(.+)$") prefix = uLower(prefix or "")

-- Use the default prefix if `name` has none if not validPrefixs[prefix] then prefix = canonicalPrefix root = name end

return lang:ucfirst(prefix), lang:ucfirst(root) end

-- Normalization for "Module:" pages.

function p.module(name) return normalize(name, 828, "Dev") end

return p -- -- (Add categories here.)