Module:ItemRecipes

local lang = mw.getLanguage('en') local p = {} function p.main(frame) if frame == mw.getCurrentFrame then args = require('Module:ProcessArgs').merge(true) else frame = mw.getCurrentFrame end local item = args[1] or mw.title.getCurrentTitle.text local result = p.doQuery(item) if not result then return ' \'\'No crafting recipes found.\'\' ' end local formatted = p.formatResult(result) return p.makeTable(formatted) end

-- cargo function p.doQuery(item) local result = mw.ext.cargo.query('Recipes',		p.cargoFields,		{			where = p.cargoWhere(item),			orderBy = 'tool'		}	) if not next(result) then return nil else return result end end

function p.cargoFields local fields = { 'result','resultAmount','tool' }	for i = 1,12 do fields[#fields+1] = 'item' .. i fields[#fields+1] = 'amount' .. i	end return table.concat(fields,',') end

function p.cargoWhere(item) local where = {} for i = 1,12 do		where[#where+1] = string.format('item%s="%s"', i, item) end return table.concat(where,' OR ') end

-- process data for output function p.formatResult(result) local formatted = {} for k, row in ipairs(result) do		formatted[k] = { p.formatLinkAndImage(row.result, row.resultAmount), p.makeIngredientsTable(row), p.formatTool(row.tool) }	end return formatted end

function p.formatLinkAndImage(result, amount) return string.format(" %s (%s)",		result or ,		result or ,		result or ,		amount or 	) end

function p.makeIngredientsTable(row) local tbl = mw.html.create('table'):css({		margin='-1px',		width = 'calc(100% + 2px)',		['border-collapse'] = 'collapse'	}) for i = 1, 12 do if row['item' .. i] ~= '' then tbl:tag('tr'):tag('td'):wikitext(p.formatLinkAndImage(row['item' .. i], row['amount' .. i])) end end return tostring(tbl) end

function p.formatTool(tool) if lang:lc(tool) == 'by hand' then return 'By Hand' else return string.format(' %s',			tool or ,			tool or ,			tool or ''		) end end

-- make table for output function p.makeTable(formatted) local styles = { '',		{ padding = '0!important', }, ''	}	local tbl = mw.html.create('table'):addClass('wikitable') tbl:tag('tr') :tag('th'):wikitext('Result'):done :tag('th'):wikitext('Ingredients'):done :tag('th'):wikitext('Crafting Station'):done for _, row in ipairs(formatted) do		tr = tbl:tag('tr') for k, v in ipairs(row) do			tr:tag('td'):css(styles[k]):wikitext(v) end end return tbl end

return p