Module:Category handler: Difference between revisions

Ganaram inukshuk (talk | contribs)
mNo edit summary
Ganaram inukshuk (talk | contribs)
No edit summary
Line 49: Line 49:
-- 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(title, ns_override)
-- Get current namespace, as lowercase
local ns_text
local curr_ns = mw.ustring.lower(mw.title.getCurrentTitle().nsText or '')
 
if type(title) == "table" and title.nsText then
-- Build table of suppressed namespaces; start with default list
ns_text = mw.ustring.lower(title.nsText or '')
elseif type(title) == "string" then
ns_text = mw.ustring.lower(title)
else
ns_text = mw.ustring.lower(mw.title.getCurrentTitle().nsText or '')
end
 
local namespaces = {}
local namespaces = {}
for k, v in pairs(DEFAULT_SUPPRESSED_NAMESPACES) do
for k, v in pairs(DEFAULT_SUPPRESSED_NAMESPACES) do
Line 59: Line 65:
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 66: Line 71:
end
end


-- Return; if no namespace found, default to false
return namespaces[ns_text] or false
return namespaces[curr_ns] or false
end
end


-- Helper function
-- Helper function
Line 74: Line 79:
-- 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, suffixes_override)
local function has_suppressed_suffix(suffixes_override)
local title_obj
local title = mw.title.getCurrentTitle()
 
local pagename = mw.ustring.lower(title.text)
if type(title) == "table" and title.text then
title_obj = title
elseif type(title) == "string" then
title_obj = mw.title.new(title)
else
title_obj = mw.title.getCurrentTitle()
end


-- Build table of suppressed suffixes, start with default suffixes
local pagename = mw.ustring.lower(title_obj.text or '')
local suffixes = {}
local suffixes = {}
-- Include default suppressed 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)
-- Add custom overrides
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 92: Line 105:
end
end


-- Find and match suffix
-- Check if the page name ends with any of the suffixes (as subpages)
for _, suffix in ipairs(suffixes) do
for _, suffix in ipairs(suffixes) do
suffix = mw.ustring.lower(mw.text.trim(suffix or '')) -- Also normalize
suffix = mw.text.trim(mw.ustring.lower(suffix or ''))
if suffix ~= '' then
if suffix ~= '' then
local pattern = '/' .. mw.ustring.gsub(suffix, '([%^%$%(%)%%%.%[%]%*%+%-%?])', '%%%1') .. '$'
local pattern = '/' .. mw.ustring.gsub(suffix, '([%^%$%(%)%%%.%[%]%*%+%-%?])', '%%%1') .. '$'
Line 105: Line 118:
return false
return false
end
end
]]--


-- "Main" function; can be called by other modules
-- "Main" function; can be called by other modules
Line 111: Line 123:
-- 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, is_debug)
function p._category_handler(cats, ns_override, suffixes_override, is_debug)
    cats = cats or {}
cats = cats or {}
    is_debug = yesno(is_debug, false)
is_debug = yesno(is_debug, false)
 
local frame = mw.getCurrentFrame()
local parent_frame = frame:getParent()
local parent_title = parent_frame and mw.title.new(parent_frame:getTitle()) or mw.title.getCurrentTitle()
 
local suppressed_ns = is_suppressed_namespace(parent_title, ns_override)
local suppressed_suffix = has_suppressed_suffix(parent_title, suffixes_override)
local is_content = parent_title.isContentPage


    local frame = mw.getCurrentFrame()
if is_debug or suppressed_ns or suppressed_suffix or not is_content then
    -- Get the page where the template/module is being used (parent frame)
-- Add a hidden HTML comment for debugging
    local parent_title = frame:getParent() and mw.title.new(frame:getParent():getTitle()) or mw.title.getCurrentTitle()
local reason = is_debug and "debug mode"
    local parent_ns = parent_title.nsText
or (suppressed_ns and "suppressed namespace")
or (suppressed_suffix and "suppressed suffix")
or (not is_content and "non-content page")
or "unknown"


    -- Don't categorize if:
return string.format("<!-- categorization suppressed: %s -->", reason)
    -- - Debug mode is on (force suppress)
end
    -- - Parent page is in suppressed namespace
    -- - Parent page is NOT a content page (e.g. subpages like /doc, /sandbox)
    if is_debug
        or is_suppressed_namespace(ns_override, parent_ns)
        or not parent_title.isContentPage
    then
        return ''
    end


    local result = ''
local result = ''
    for _, cat in ipairs(cats) do
for _, cat in ipairs(cats) do
        cat = mw.text.trim(cat or '')
cat = mw.text.trim(cat or '')
        if cat ~= '' then
if cat ~= '' then
            result = result .. string.format('[[Category:%s]]', cat)
result = result .. string.format('[[Category:%s]]', cat)
        end
end
    end
end


    return result
return result
end
end


Line 145: Line 160:
function p.category_handler(frame)
function p.category_handler(frame)
local args = getArgs(frame)
local args = getArgs(frame)
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 = yesno(args["debug"], false) -- Parse debug mode; setting this to TRUE disables all categories
local is_debug             = yesno(args["debug"], false)


-- Parse categories
-- Parse categories
Line 159: Line 174:
end
end


-- Parse excluded namespaces
-- Parse excluded namespaces (added/overrides default)
-- 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
Line 172: Line 184:


-- Parse excluded suffixes
-- Parse excluded suffixes
--[[
local suffixes = {}
local suffixes = {}
for suffix in mw.text.gsplit(suffixes_unparsed, "[;\n]") do
for suffix in mw.text.gsplit(suffixes_unparsed, "[;\n]") do
Line 180: Line 191:
end
end
end
end
]]--


return p._category_handler(cats, ns_override, is_debug)
return p._category_handler(cats, ns_override, suffixes, is_debug)
end
end


return p
return p