Pojdi na vsebino

Modul:Peskovnik/Smihael

Iz Wikipedije, proste enciklopedije
local p = {}

-- Main function: extracts TemplateData from a specified template,
-- generates label–parameter pairs, and then builds an argument table for the infobox.
-- If an optional "groups" parameter (a JSON string) is provided in frame.args,
-- its structure is used to group parameters under headers.
-- Finally, the table is passed to the Infobox module by calling its "infobox" function.
function p.makeInfoboxFromTableData(frame)
    local templateName = frame.args[1]
    if type(templateName) == "table" then
        templateName = templateName.name or tostring(templateName)
    end

    local title = mw.title.new("Template:" .. templateName)
    if not title then
        return "Template not found."
    end

    local content = title:getContent() or ""
    if content == "" then
        return "Template content is empty."
    end

    local jsonData = content:match("<templatedata>(.-)</templatedata>")
    if not jsonData then
        return "No TemplateData block found."
    end

    local ok, data = pcall(mw.text.jsonDecode, jsonData)
    if not ok then
        return "Error decoding TemplateData JSON."
    end

    -- Generate an array of { label, param } pairs from data.params.
    local pairsArray = p.generateLabelParamPairs(data)

    -- Check for an optional "groups" parameter.
    local infoboxArgs = nil
    if frame.args.groups and frame.args.groups ~= "" then
        local ok2, groupsData = pcall(mw.text.jsonDecode, frame.args.groups)
        if ok2 and type(groupsData) == "table" then
            infoboxArgs = p.generateInfoboxArgsFromGroups(pairsArray, groupsData)
        else
            infoboxArgs = p.generateInfoboxArgs(pairsArray)
        end
    else
        infoboxArgs = p.generateInfoboxArgs(pairsArray)
    end

    -- Call the Infobox module's "infobox" function with the generated arguments.
    local infoboxModule = require("Module:Infobox")
    if infoboxModule.infobox then
    	mw.log("Infobox args: " .. mw.text.jsonEncode(infoboxArgs))
        return infoboxModule.infobox(infoboxArgs)
    else
        return "Infobox module does not expose an appropriate function."
    end
end

-- Generates an array of label–parameter pairs.
-- For each key in data.params, it creates a pair table with:
--   label: the parameter's label (or the key if missing)
--   param: the parameter name.
function p.generateLabelParamPairs(data)
    local result = {}
    if data.params then
        for paramName, paramData in pairs(data.params) do
            local label = paramData.label or paramName
            table.insert(result, { label = label, param = paramName })
        end
    else
        return {}
    end
    return result
end

-- Default method: generate infobox arguments sequentially (without grouping).
-- For each pair, creates:
--   "labelX" = pair.label
--   "dataX"  = {{{pair.param|}}}
function p.generateInfoboxArgs(pairsArray)
    local args = {}
    for i, pair in ipairs(pairsArray) do
        args["label" .. i] = pair.label
        args["data" .. i]  = string.format("{{{%s|}}}", pair.param)
    end
    return args
end

-- Extended method: generate infobox arguments using groups.
-- groupsData is expected to be an array of group objects, each with:
--   header: string,
--   rows: an array of parameter names.
-- For each group, we output:
--
--   headerN = (group header)
--   then for each parameter in group.rows, add:
--       labelM = (the label corresponding to that parameter)
--       dataM  = {{{parameter|}}}
--
-- After processing groups, any pairs not assigned to a group are appended.
function p.generateInfoboxArgsFromGroups(pairsArray, groupsData)
    local args = {}
    local counter = 1
    local used = {}

    -- Build a lookup table from parameter name to its pair.
    local pairLookup = {}
    for _, pair in ipairs(pairsArray) do
        pairLookup[pair.param] = pair
    end

    -- Process each group.
    for _, group in ipairs(groupsData) do
        if group.header and group.rows and type(group.rows) == "table" then
            args["header" .. counter] = group.header
            counter = counter + 1
            for _, paramName in ipairs(group.rows) do
                local pair = pairLookup[paramName]
                if pair then
                    args["label" .. counter] = pair.label
                    args["data" .. counter]  = string.format("{{{%s|}}}", pair.param)
                    used[pair.param] = true
                    counter = counter + 1
                end
            end
        end
    end

    -- Append any pairs not included in any group.
    for _, pair in ipairs(pairsArray) do
        if not used[pair.param] then
            args["label" .. counter] = pair.label
            args["data" .. counter]  = string.format("{{{%s|}}}", pair.param)
            counter = counter + 1
        end
    end

    return args
end

return p