Module:Cat

From XenReference
Revision as of 16:07, 23 December 2025 by Inthar (talk | contribs)

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

local getArgs = require('Module:Arguments').getArgs

local p = {}

local function tokenize(unparsed)
    local strings = {}
    local inside = false
    local string = ""
    for i = 1, #unparsed do
        if unparsed[i] == "\"" then
            if inside then
                table.insert(strings, string)
                string = ""
            end
            inside = not inside
        elseif inside then
            string = string .. unparsed[i]
        end
    end
    return strings
end

-- Turns arguments into a string "[[Category:Cat1]][[Category:Cat2]]..."
-- Expects double quotes around each category name
function p.toCategories(frame)
    local args = getArgs(frame)
    local unparsed = args[1]
    local cats = ""
    for _, quoted_cat in pairs(tokenize(unparsed)) do
        cats = cats .. "[[Category:" .. cat .. "]]"
    end
    return frame:preprocess(cats)
end

return p