Module:Category handler: Difference between revisions
No edit summary |
rollback to previously working code |
||
| Line 1: | Line 1: | ||
-- This module follows [[User:Ganaram inukshuk/Provisional style guide for Lua]] | -- This module follows [[User:Ganaram inukshuk/Provisional style guide for Lua]] | ||
local yesno = require("Module:Yesno") | local yesno = require("Module:Yesno") | ||
| Line 7: | Line 6: | ||
-- Basic category handler, based on Wikipedia's category handler. It categorizes | -- Basic category handler, based on Wikipedia's category handler. It categorizes | ||
-- pages, given a table of categories as input, and suppresses categorization if | -- pages, given a table of categories as input, and suppresses categorization if | ||
-- the page is in the table of excluded namespaces. | -- the page is in the table of excluded namespaces, or table of excluded | ||
-- suffixes (subpages). | |||
-- The most common categorizing templates that have/require complex rules are: | -- The most common categorizing templates that have/require complex rules are: | ||
-- - Infoboxes; these usually shouldn't categorize if they're outside the main | -- - Infoboxes; these usually shouldn't categorize if they're outside the main | ||
| Line 47: | Line 47: | ||
-- 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( | local function is_suppressed_namespace(ns_override) | ||
-- Get current namespace, as lowercase | |||
local curr_ns = mw.ustring.lower(mw.title.getCurrentTitle().nsText or '') | |||
-- 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 | ||
| Line 63: | Line 57: | ||
end | end | ||
-- Then extend/override list using ns_override, if available | |||
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 | ||
| Line 69: | Line 64: | ||
end | end | ||
return namespaces[ | -- Return; if no namespace found, default to false | ||
return namespaces[curr_ns] or false | |||
end | end | ||
| Line 76: | Line 72: | ||
-- Accepts an opiontal table, containing additional suffixes to suppress | -- Accepts an opiontal table, containing additional suffixes to suppress | ||
-- Suffix and custom suffixes are lowercased to guarantee matching | -- Suffix and custom suffixes are lowercased to guarantee matching | ||
local function has_suppressed_suffix(title | local function has_suppressed_suffix(suffixes_override) | ||
local | local title = mw.title.getCurrentTitle() | ||
local pagename = mw.ustring.lower(title.text) | |||
-- Build table of suppressed suffixes, start with default suffixes | |||
local suffixes = {} | local suffixes = {} | ||
for _, suffix in ipairs(DEFAULT_SUPPRESSED_SUFFIXES) do | for _, suffix in ipairs(DEFAULT_SUPPRESSED_SUFFIXES) do | ||
table.insert(suffixes, suffix) | table.insert(suffixes, suffix) | ||
end | end | ||
-- | -- Then append additional suffixes, if available (append, not replace) | ||
if type(suffixes_override) == "table" then | if type(suffixes_override) == "table" then | ||
for _, suffix in ipairs(suffixes_override) do | for _, suffix in ipairs(suffixes_override) do | ||
| Line 102: | Line 89: | ||
end | end | ||
-- | -- Find and match suffix | ||
for _, suffix in ipairs(suffixes) do | for _, suffix in ipairs(suffixes) do | ||
suffix = mw. | suffix = mw.ustring.lower(mw.text.trim(suffix or '')) -- Also normalize | ||
if suffix ~= '' then | if suffix ~= '' then | ||
local pattern = '/' .. mw.ustring.gsub(suffix, '([%^%$%(%)%%%.%[%]%*%+%-%?])', '%%%1') .. '$' | local pattern = '/' .. mw.ustring.gsub(suffix, '([%^%$%(%)%%%.%[%]%*%+%-%?])', '%%%1') .. '$' | ||
| Line 114: | Line 101: | ||
return false | return false | ||
end | end | ||
| Line 129: | Line 107: | ||
-- 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, | function p._category_handler(cats, ns_override, suffixes, is_debug) | ||
local cats = cats or {} | |||
local is_debug = yesno(is_debug, false) | |||
-- Test values; should be commented out for normal use | |||
--local cats = cats or {"Abstract MOS patterns", "7-tone scales"} | |||
--local ns_override = ns_override or { ["module"] = true } | |||
-- If in a suppressed namespace/prefix, or in debug mode (suppresses ALL | |||
-- categorization) don't bother | |||
if is_suppressed_namespace(ns_override) | |||
or has_suppressed_suffix(suffixes) | |||
or is_debug then | |||
return '' | |||
end | |||
-- Otherwise, categorize | |||
local result = '' | |||
for _, cat in ipairs(cats) do | |||
cat = mw.text.trim(cat or '') | |||
if cat ~= '' then | |||
result = result .. string.format('[[Category:%s]]', cat) | |||
end | |||
end | |||
return result | |||
end | |||
-- Wrapper for templates calling via #invoke | -- Wrapper for templates calling via #invoke | ||
function p.category_handler(frame) | function p.category_handler(frame) | ||
local args = | local args = frame:getParent().args | ||
local cats_unparsed = args["categories"] or "" | local cats_unparsed = args["categories" ] or "" | ||
local excluded_ns_unparsed = args["excluded_ns"] or "" | local excluded_ns_unparsed = args["excluded_ns"] or "" | ||
local suffixes_unparsed = args["suffixes"] or "" | local suffixes_unparsed = args["suffixes" ] or "" | ||
local is_debug | local is_debug = yesno(args["debug"], false) -- Parse debug mode; setting this to TRUE disables all categories | ||
-- Parse categories | -- Parse categories | ||
| Line 190: | Line 153: | ||
end | end | ||
-- Parse excluded namespaces | -- Parse excluded namespaces | ||
-- These are added in addition to the default list | |||
-- This currently can't force-allow suppressed namespaces as template input, | |||
-- only disallow additional namespaces | |||
local ns_override = {} | local ns_override = {} | ||
for ns in mw.text.gsplit(excluded_ns_unparsed, "[;\n]") do | for ns in mw.text.gsplit(excluded_ns_unparsed, "[;\n]") do | ||