Module:Category handler: Difference between revisions
m todo |
ArrowHead294 (talk | contribs) m Wikitext debugger option |
||
| (4 intermediate revisions by one other user not shown) | |||
| Line 12: | Line 12: | ||
-- - Infoboxes; these usually shouldn't categorize if they're outside the main | -- - Infoboxes; these usually shouldn't categorize if they're outside the main | ||
-- namespace. | -- namespace. | ||
-- - Certain mboxes; some mboxes categorize | -- - Certain mboxes; some mboxes categorize pages, but if that mbox categorizes | ||
-- categorize their own | -- templates/modules and are placed on that page's /doc subpage, it should | ||
-- categorize the page on which the documentation is transcluded, not the /doc | |||
-- page itself. | |||
-- - Categorizing templates used on their own /doc pages as examples; a special | |||
-- case of the previous case; passing in debug=1 disables all categorization. | |||
-- Default list of namespaces in which to suppress categorization. Most | -- Default list of namespaces in which to suppress categorization. Most | ||
| Line 22: | 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 45: | 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 61: | 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 | ||
| Line 108: | Line 134: | ||
-- Disallows categories if it's in a suppressed namespace or the page has a | -- Disallows categories if it's in a suppressed namespace or the page has a | ||
-- suppressed suffix (subpage) | -- suppressed suffix (subpage) | ||
function p._category_handler(cats, ns_override, suffixes | function p._category_handler(cats, is_debug, ns_override, suffixes) | ||
local cats = cats or {} | local cats = cats or {} | ||
local is_debug = yesno(is_debug, false) | local is_debug = yesno(is_debug, false) | ||
| Line 139: | Line 165: | ||
function p.category_handler(frame) | function p.category_handler(frame) | ||
local args = getArgs(frame) | local args = getArgs(frame) | ||
local excluded_ns_unparsed = args["excluded_ns"] or "" -- Additional namespaces to exclude (EG, a template that should not be used within main namespace, which is allowed by default) | local excluded_ns_unparsed = args["excluded_ns"] or "" -- Additional namespaces to exclude (EG, a template that should not be used within main namespace, which is allowed by default) | ||
local allowed_ns_unparsed = args["allowed_ns" ] or "" -- Namespaces to allow; overrides default list (EG, a template that should be used within user namespace, which is disallowed by default) | local allowed_ns_unparsed = args["allowed_ns" ] or "" -- Namespaces to allow; overrides default list (EG, a template that should be used within user namespace, which is disallowed by default) | ||
local suffixes_unparsed = args["suffixes" ] or "" -- Additional page suffixes in which to disallow categories | local suffixes_unparsed = args["suffixes" ] or "" -- Additional page suffixes in which to disallow categories | ||
local is_debug = yesno(args["debug"], false) | local is_debug = yesno(args["debug"], false) -- Parse debug mode; setting this to TRUE disables all categories | ||
local wtext = yesno(args["wtext"]) -- Used to show the underlying Wikitext | |||
-- Parse categories | -- Parse categories | ||
| Line 166: | Line 193: | ||
end | end | ||
-- | -- Parse allowed namespaces | ||
-- This gets added to the ns_override array, with values set to FALSE | -- This gets added to the ns_override array, with values set to FALSE | ||
-- EG, Template:TODO | -- EG, Template:TODO is used in several namespaces that are normally | ||
-- | -- suppressed: user, talk, help, and maybe xw talk | ||
for ns in mw.text.gsplit(allowed_ns_unparsed, "[;\n]") do | |||
ns = mw.text.trim(ns) | |||
if ns ~= "" then | |||
ns_override[ns] = false | |||
end | |||
end | |||
-- Parse excluded suffixes | -- Parse excluded suffixes | ||
| Line 180: | Line 213: | ||
end | end | ||
local result = p._category_handler(cats, is_debug, ns_override, suffixes) | |||
if wtext then | |||
result = "<syntaxhighlight lang=\"wikitext\">" .. result .. "</syntaxhighlight>" | |||
end | |||
return frame:preprocess(result) | |||
end | end | ||
return p | return p | ||