Module:Category handler: Difference between revisions

Ganaram inukshuk (talk | contribs)
overhaul logic for suffixes?
Ganaram inukshuk (talk | contribs)
No edit summary
Line 110: Line 110:
-- suppressed suffix (subpage)
-- suppressed suffix (subpage)
function p._category_handler(cats, ns_override, suffixes, is_debug)
function p._category_handler(cats, ns_override, suffixes, is_debug)
local cats = cats or {}
    local cats = cats or {}
local is_debug = yesno(is_debug, false)
    local is_debug = yesno(is_debug, false)
local title = mw.title.getCurrentTitle()


-- Don't bother if if:
    local frame = mw.getCurrentFrame()
-- - In debug mode
    -- Get the page where the template/module is being used (parent frame)
-- - In a suppressed namespace
    local parent_title = frame:getParent() and mw.title.new(frame:getParent():getTitle()) or mw.title.getCurrentTitle()
-- - On a subpage like /doc or /sandbox (but not if transcluded)
    local parent_ns = parent_title.nsText
-- - Not a "content" page (IE, not a main/module/template page)
if is_debug
or is_suppressed_namespace(ns_override)
or not title.isContentPage
then
return ''
end


-- Categorize
    -- Don't categorize if:
local result = ''
    -- - Debug mode is on (force suppress)
for _, cat in ipairs(cats) do
    -- - Parent page is in suppressed namespace
cat = mw.text.trim(cat or '')
    -- - Parent page is NOT a content page (e.g. subpages like /doc, /sandbox)
if cat ~= '' then
    if is_debug
result = result .. string.format('[[Category:%s]]', cat)
        or is_suppressed_namespace(ns_override, parent_ns)
end
        or not parent_title.isContentPage
end
    then
        return ''
    end


return result
    -- 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
end