Module:Categorization

From nUSA Wiki Archive
Jump to navigation Jump to search

Documentation for this module may be created at Module:Categorization/doc

--<nowiki>
local utils = require("Module:Utils")

local p = {}

--% This function is meant to be called with the names of the namespaces that pages can be categorized in.
--% It returns a function to be used to categorize pages, which accepts any number of category names.
function p.categorize_in_namespaces(...)
    local namespaces = {...}
    return function(...)
        local result = ''
        if mw.title.getCurrentTitle():inNamespaces(unpack(namespaces)) then
            for _, category in next, {...} do
                result = result .. "[[Category:" .. category .. "]]"
            end
        end
        return result
    end
end

--% This function can be called with any number of category names.
--% If the current page is in a content namespace, it returns a string with links to each of the categories. Otherwise, it returns an empty string.
--% The function is especially meant for templates that can be used on non-article pages, for example in the User namespace, and where such pages should not be added to article categories.
function p.categorize_if_content_page(...)
    local result = ""
    if mw.title.getCurrentTitle().isContentPage then
        for _, category in next, {...} do
            result = result .. "[[Category:" .. category .. "]]"
        end
    end
    return result
end

--% This function is the same as the above but usable by {{#invoke:}}.
--# TODO: Make it so that we can use one function instead
function p._template_categorize_if_content_page(args)
    local categories = utils.trim_positional_arguments(args)
    local result = ""
    if mw.title.getCurrentTitle().isContentPage then
        for _, category in next, categories do
            result = result .. "[[Category:" .. category .. "]]"
        end
    end
    return result
end

p.template_categorize_if_content_page = utils.make_wrapper_function(p._template_categorize_if_content_page)

return p