Module:Sandbox/User:The Gaffer/Modules/Products table

From Brighter Shores Wiki
Jump to navigation Jump to search

Documentation for this module may be created at Module:Sandbox/User:The Gaffer/Modules/Products table/doc

local p = {}

function p._main(frame)
    local args = frame:getParent().args
    local mw = require('mw')
    
    local products = getProducts(args)
    local recipeNames = extractRecipeNames(products)
    mw.logObject(recipeNames)
end

local function extractRecipeNames(data)
    local recipeNames = {}

    for _, recipeInfo in ipairs(data) do
        local recipeLink = recipeInfo["Recipe"]
        if recipeLink then
            local displayName = recipeLink:match("%[%[.-|(.+)%]%]")
            if displayName then
                table.insert(recipeNames, displayName)
            end
        end
    end
    return recipeNames
end

local function getProducts(args)
	local item = mw.smw.ask('[[:+]][[Uses item::' .. args.item .. '|?Uses item | mainlabel=Recipe]]') or 0
    return item
end

return p