Module:Sandbox/Ninjamask/Recipe

local p = {} local lib = require('Module:Feature') local args local frame

function recipePart(item) if item == nil then return nil end recipe = {} s = frame:expandTemplate{ title = 'Item data '.. args[1], args = {'pst2', 'recipe'} }   if s == '' then return nil end parts = Split(s, ',') for i = 1, #parts do       table.insert(recipe, {parts[i], recipePart(parts[i])}) end return recipe end

function p.recipe (frameInput) frame = frameInput if frame.args == nil then args = lib.arguments(frame) else args = lib.arguments(frame.args) end return table.tostring(recipePart(args[1])) end

function Split(str, delim, maxNb) -- Eliminate bad cases... if string.find(str, delim) == nil then return { str } end if maxNb == nil or maxNb < 1 then maxNb = 0   -- No limit end local result = {} local pat = "(.-)" .. delim .. ""  local nb = 0 local lastPos for part, pos in string.gfind(str, pat) do     nb = nb + 1 result[nb] = part lastPos = pos if nb == maxNb then break end end -- Handle the last field if nb ~= maxNb then result[nb + 1] = string.sub(str, lastPos) end return result end

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

return p