Module:RecipeUsage

local p = {}

local recipesData = mw.loadData('Module:CraftingRecipes')

function table.val_to_str ( v ) if "string" == type( v ) then v = string.gsub( v, "\n", "\\n" ) if string.match( string.gsub(v,"[^'\"]",""), '^"+$' ) then return "'" .. v .. "'"   end return '"' .. string.gsub(v,'"', '\\"' ) .. '"' else return "table" == type( v ) and table.tostring( v ) or     tostring( v ) end end

function table.key_to_str ( k ) if "string" == type( k ) and string.match( k, "^[_%a][_%a%d]*$" ) then return k else return "[" .. table.val_to_str( k ) .. "]" end end

function table.tostring( tbl ) local result, done = {}, {} for k, v in ipairs( tbl ) do   table.insert( result, table.val_to_str( v ) ) done[ k ] = true end for k, v in pairs( tbl ) do   if not done[ k ] then table.insert( result,       table.key_to_str( k ) .. "=" .. table.val_to_str( v ) ) end end return "{" .. table.concat( result, "," ) .. "}" end

function createTable(recipes) local table = "" for i=1,#recipes do   local tableArgs = {} tableArgs.energy = recipes[i].energy tableArgs.time = recipes[i].time tableArgs.product_count = recipes[i].product[1] tableArgs.product_name = recipes[i].product[2] local t = 1 for k,ingredient in pairs(recipes[i].ingredients) do       tableArgs[t]   = ingredient[1] tableArgs[t+1] = ingredient[2] t = t + 2 end if i == #recipes then tableArgs.foot = 1 end table = table .. mw.getCurrentFrame:expandTemplate{title='CraftingTable', args=tableArgs} end return table end

p.crafting = function(f) return table.tostring(f:getParent:getTitle) -- local pageName = f:getParent:getTitle -- local recipes = {} -- for k,recipe in pairs(recipesData) do --    if recipe.product[2] == pageName then --     recipes[1] = recipe --     return createTable(recipes) --   end -- end -- return "No crafting recipe was found in Module:CraftingRecipes for " .. pageName end

p.usage = function(f) local pageName = f:getParent:getTitle local recipes = {} for k,recipe in pairs(recipesData) do   for i_k, ingredient in pairs(recipe.ingredients) do       if ingredient[2] == pageName then recipes[#recipes + 1] = recipe end end end return createTable(recipes) end

return p