Module:Recipes

local item_link = require('Module:Item').go local trim = mw.text.trim

-- credit: http://richard.warburton.it -- this version is with trim. local function explode(div,str) if (div=='') then return false end local pos,arr = 0,{} -- for each divider found for st,sp in function return string.find(str,div,pos,true) end do		table.insert(arr, trim(string.sub(str,pos,st-1))) -- Attach chars left of current divider pos = sp + 1 -- Jump past current divider end table.insert(arr, trim(string.sub(str,pos))) -- Attach chars right of last divider return arr end

local metals = { ['Copper/Tin'] = 1, ['Silver/Tungsten'] = 1, ['Gold/Platinum'] = 1, ['Iron/Lead'] = 1, ['Cobalt/Palladium'] = 1, ['Mythril/Orichalcum'] = 1, ['Adamantite/Titanium'] = 1, ['Tin/Copper'] = 2, ['Tungsten/Silver'] = 2, ['Platinum/Gold'] = 2, ['Lead/Iron'] = 2, ['Palladium/Cobalt'] = 2, ['Orichalcum/Mythril'] = 2, ['Titanium/Adamantite'] = 2, }

-- retuan a array of itemname, split xxx/yyy to item1=xxx, item2=yyy. If it's something like "Lead/Iron Bar", it will normalize as item1 = Iron Bar, item2 = Lead Bar. local function split(name) local count = select(2, name:gsub("/", "/", 2)) if count == 0 then -- only 1 item return { trim(name) } elseif count == 1 then -- 2 items local item1a, item1b, item2a, item2b = name:match("^%s*(%S+)%s*(.-)/%s*(%S+)%s*(.-)$") local x = metals[item1a..'/'..item2a] if tostring(item1b) == '' and x then item1b = item2b end if x == 2 then return {trim(item2a..' '..item2b), trim(item1a..' '..item1b)} else return {trim(item1a..' '..item1b), trim(item2a..' '..item2b)} end else -- 3 or more items return explode('/', name) end end

-- return 1 or 2 value(s), when input is name[note], return item, note. function itemname(str) local item, note = str:match("^(.-)(%b[])$") if item then return item, note else return str end end

-- @todo 这里最好把any的处理搞起来. -- normalize ingredient name input, Lead Bar=>¦Lead Bar¦, Iron/Lead Bar => ¦Iron Bar¦Lead Bar¦, Lead/Iron Bar => ¦Iron Bar¦Lead Bar¦ .... normalize = function(name) local result = '¦' for k, v in pairs(split(name)) do result = result .. itemname(v) .. '¦'	end return result end

local p = {}

-- for p.register = function(frame) local args = frame:getParent.args

-- store to cargo. local serialized = '' local ingredients = {} local ingredients_string = '' -- order AS IS. local ingredients_string_full = '' -- sorted. for k, v in pairs(args) do serialized = serialized .. '»' .. trim(k) .. '¦' .. trim(v) if(type(k) == 'number') then if k % 2 == 1 then -- 2n-1, nth item local ingstr = normalize(v) ingredients_string = ingredients_string .. '^' .. ingstr table.insert(ingredients, ingstr .. args[k+1]) end end end table.sort(ingredients) for _, v in pairs(ingredients) do ingredients_string_full = ingredients_string_full .. '^' .. v	end frame:callParserFunction('#cargo_store:_table=newRecipes',{		result = trim(args['result'] or ),		amount = trim(args['amount'] or ), 		version = string.lower(trim(args['version'] or )), 		station = trim(args['station'] or ), 		ingredients = string.sub(ingredients_string, 2),		ings = string.sub(ingredients_string_full, 2),		args = serialized,	}) end

return p