Module:Recipe

From Coral Island Wiki
Jump to navigation Jump to search

Documentation for this module may be created at Module:Recipe/doc

local p = {}
local lib = require('Module:Feature')
local Icon = require('Module:Icon')._main
local category = mw.html.create()

function p.main(frame)
	local args = require('Module:Arguments').getArgs(frame)

	return p._main(args)
end

function p._main(args)
	local items = args.items or args[1]
	assert(items, 'No ingredients given')
	local yield_item = args.product or mw.title.getCurrentTitle().rootText
	local yield_amount = args['yield'] or '1'
	local medium = args.medium or args[2] or 'Menu'
	local delim = args.delim or ';'
	local delim_amount = args.delim_amount or '*'
	local delim_quality = args.delim_quality or '/'
	local medium_time = args['time']
	
	--start out and figure out cols
	local out = mw.html.create('div'):addClass('ci-recipe-wrapper')
	
	local data = {
		items = items,
		delim = delim,
		amount = '^(.-)' .. delim_amount .. '(.*)$',
		quality = '^(.-)' .. delim_quality .. '(.*)$',
	}
	out:tag('div'):addClass('ci-recipe-ingredient'):node(p.parseIngredients(data))
	
	local out2 = out:tag('div'):addClass('ci-recipe-wrapper-second')
	
	local medium_div = out2:tag('div'):addClass('ci-recipe-medium' .. lib.ternary(medium_time~=nil, ' ci-recipe-timed', ''))
	if medium == 'Menu' then
		medium_div:node(Icon{
			name   = 'Crafting mainico',
			link = 'Crafting',
			notext = 1,
			size   = 35,
		})
		medium_div:tag('br')
		medium_div:wikitext('In-menu')
		medium_div:tag('br')
		medium_div:wikitext('crafting')
		category:wikitext('[[Category:Crafted with crafting menu]]')
	else
		medium_div:node(Icon{
			name   = medium,
			notext = 1,
			size   = 35,
		})
		medium_div:tag('br')
		medium_div:wikitext('[[', medium, ']]')
		category:wikitext('[[Category:Crafted with ', string.lower(medium), ']]')
		if lib.inArray({
			'blender',
			'ceramic bowl',
			'chef knife',
			'frying pan',
			'grill',
			'oven',
			'pot',
			'seasoning set',
			'skillet',
		}, string.lower(medium))
		then category:wikitext('[[Category:Cooked dishes|', string.lower(medium), ']]') end
	end
	if medium_time then
		medium_div:tag('br')
		medium_div:tag('div'):addClass('ci-recipe-yield-amount'):tag('small'):wikitext(medium_time)
	end
	
	out2
	:tag('div')
		:addClass('ci-recipe-yield')
		:tag('div')
			:addClass('ci-recipe-yield-img')
			:node(Icon{
				name   = yield_item,
				notext = 1,
				size   = 70,
			})
		:tag('div')
			:addClass('ci-recipe-yield-amount')
			:tag('small'):wikitext('×', yield_amount)
	
	category:wikitext('[[Category:Crafted items]]')
	out:node(require('Module:Namespace detect').main{main=category})
	
	return out
end

local function makeIcon(str, data)
	if not str then return mw.hmtl.create() end
	local pre, quality = string.match(str, data.quality)
	if not pre then
		pre = str
	end
	local name, amount = string.match(pre, data.amount)
	if not name then
		name = pre
		amount = '1'
	end
	category:wikitext('[[Category:Uses ', string.lower(name), ']]')
	return Icon
	{
		name    = name,
		amount  = amount,
		size    = '20',
		quality = quality
	}
end

function p.parseIngredients(data)
	local list = mw.html.create()
	for item in lib.gsplit(data.items, ';') do
		if lib.isNotEmpty(item) then
			if item:find('//') then
				for n, sub_item in ipairs(lib.split(item, '//')) do
					if n>1 then list:tag('i'):wikitext(' or ') end
					list:node(makeIcon(sub_item, data))
				end
			else
				list:node(makeIcon(item, data))
			end
		end
	end
	return list
end

return p