Module:CraftInline

From Ylvapedia

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

local p = {}

local function isBlank(value)
	return value == nil or not value or value == ''
end

function p.craftInline(f)

	local args = f:getParent().args
	local lang = f.args[1]
	local data = mw.loadData('Module:CraftingTable/i18n')
	local mode = args['mode'] or 'inline'
	local Recipe = {
		{
			CraftingStation = args['1-1'] or '',
			Ingredient = args['1-2'] or '',
			IngredientNum = args['1-3'] or '',
			IngredientExtra = args['1-4'] or ''
		},
		{
			CraftingStation = args['2-1'] or '',
			Ingredient = args['2-2'] or '',
			IngredientNum = args['2-3'] or '',
			IngredientExtra = args['2-4'] or ''
		},
		{
			CraftingStation = args['3-1'] or '',
			Ingredient = args['3-2'] or '',
			IngredientNum = args['3-3'] or '',
			IngredientExtra = args['3-4'] or ''
		}
	}

	local imgsize = args['size'] or '32px'
	if args['size'] == 'xxx-large' then imgsize = '32px'
	elseif args['size'] == 'xx-large' then imgsize = '32px'
	elseif args['size'] == 'x-large' then imgsize = '32px'
	elseif args['size'] == 'large' then imgsize = '32px'
	elseif args['size'] == 'medium' then imgsize = '32px'
	elseif args['size'] == 'small' then imgsize = '28px'
	elseif args['size'] == 'x-small' then imgsize = '24px'
	elseif args['size'] == 'xx-small' then imgsize = '20px'
	end

	local CSs = { '', '', '' }
	local Ings = { '', '', '' }

	for n = 1, 3 ,1 do

		if isBlank(Recipe[n].CraftingStation) then
		else
			local v = {} ; v = mw.text.split(Recipe[n].CraftingStation, "%s*,%s*")
			for i, item in ipairs(v) do

				if mode == 'inline' then
					if i > 1 then CSs[n] = CSs[n]..',' end
				elseif mode == 'table' or mode == 'inlinetd' then
					if i > 1 then CSs[n] = CSs[n]..'<br />' end
				elseif mode == 'tabletd' then
				end

				if isBlank(item) then
					CSs[n] = CSs[n]..data[lang]['BlankValue']
				else
					CSs[n] = CSs[n]..f:expandTemplate{title=data[lang]['TemplateName'],args={item,size=imgsize}}
				end
			end
		end

		if isBlank(Recipe[n].Ingredient) then
		else
			local v = {} ; v = mw.text.split(Recipe[n].Ingredient, "%s*,%s*")
			local x = {} ; x = mw.text.split(Recipe[n].IngredientNum, "%s*,%s*")
			local y = {} ; y = mw.text.split(Recipe[n].IngredientExtra, "%s*,%s*")
			for i, item in ipairs(v) do

				if mode == 'inline' or mode == 'inlinetd' then
					if i > 1 then
						Ings[n] = Ings[n]..'<span class="plus">&nbsp;&nbsp;&plus;&nbsp;</span>'
					end
				elseif mode == 'table' then
					if i > 1 and check == false then
						Ings[n] = Ings[n]..'<div class="plus or-separator">&plus;</div>'
					end
					check = false
				elseif mode == 'tabletd' then 
					if i > 1 then
						Ings[n] = Ings[n]..'<br />'
					else
						Ings[n] = Ings[n]..'</td><td>'
					end
				elseif mode == 'td' then

				else
				end

				if isBlank(item) then
					Ings[n] = Ings[n]..data[lang]['BlankValue']
				else
					if string.find(item,'/') then
						local z = {} ; z = mw.text.split(item,'%s*/%s*')
						local z2 = {} ; if x[i] then z2 = mw.text.split(x[i],'%s*/%s*') or 1 end
						local z3 = {} ; if y[i] then z3 = mw.text.split(y[i],'%s*/%s*') or '' end

						for i2, item2 in ipairs(z) do

							local itemname = item2
							local itemnum = z2[i2] or 1
							local itemvar
							local itemext = z3[i2] or ''

							if mode == 'inline' or mode == 'inlinetd' then
								if i2 > 1 then Ings[n] = Ings[n]..'<span class="or">'..data[lang]['Or']..'</span>' end
							else
								if i2 > 1 then Ings[n] = Ings[n]..'<div class="or-separator">'..data[lang]['Or']..'</div>' end
							end

							if string.find(item2,'@') then
								itemname, itemvar = item2:match("([^@]+)@([^@]+)")
							end

							if isBlank(itemnum) then
								if itemvar then
									Ings[n] = Ings[n]..f:expandTemplate{title=data[lang]['TemplateName'],args={itemname,v=itemvar,link='Elin:'..itemname..' '..itemvar,size=imgsize}}
								else
									Ings[n] = Ings[n]..f:expandTemplate{title=data[lang]['TemplateName'],args={itemname,size=imgsize}}
								end
							else
								if itemvar then
									Ings[n] = Ings[n]..f:expandTemplate{title=data[lang]['TemplateName'],args={itemname,itemnum,v=itemvar,link='Elin:'..itemname..' '..itemvar,size=imgsize}}
								else
									Ings[n] = Ings[n]..f:expandTemplate{title=data[lang]['TemplateName'],args={itemname,itemnum,size=imgsize}}
								end
							end

							if z3[i2] == data[lang]['TitleExtra'] then Ings[n] = Ings[n]..data[lang]['TextExtra']
							elseif z3[i2] ==  data[lang]['TitleGourmet'] then Ings[n] = Ings[n]..data[lang]['TextGourmet']
							end
						end

						if mode == 'inline' or mode == 'inlinetd' then
						else
							Ings[n] = Ings[n]..'<div class="separator">&nbsp;</div>'
						end
						check = true
					else
						if string.find(item,'@') then
							local v2 = {} ; v2 = mw.text.split(item,'%s*@%s*')
							Ings[n] = Ings[n]..f:expandTemplate{title=data[lang]['TemplateName'],args={v2[1],v=v2[2],size=imgsize}}
						else
							if x[i] == '' then
								Ings[n] = Ings[n]..f:expandTemplate{title=data[lang]['TemplateName'],args={item,size=imgsize}}
							else
								Ings[n] = Ings[n]..f:expandTemplate{title=data[lang]['TemplateName'],args={item,x[i],size=imgsize}}
							end
						end

						if y[i] == data[lang]['TitleExtra'] then
							Ings[n] = Ings[n]..data[lang]['TextExtra']
						elseif y[i] ==  data[lang]['TitleGourmet'] then
							Ings[n] = Ings[n]..data[lang]['TextGourmet']
						end
					end
				end
			end
		end
	end

	local html = mw.html.create('span'):addClass('ci')
	local div = ''
	local table = { '', '', '' }

		if not args['size'] or args['size'] == '' then
		else html:cssText('font-size:'..args['size']..';')
		end

	if mode == 'table' then
		div = html:tag('div'):addClass('CraftingTable'):cssText('float: left; display: ruby;')
	end

	for n = 1, 3 ,1 do

		if mode == 'inline' or mode == 'inlinetd' then
			if CSs[n] ~= '' then
				if n == 1 then
					html:wikitext(CSs[n])
				elseif n == 2 then
					if CSs[1] == CSs[2] then
					elseif CSs[2] == CSs[3] then
					else html:wikitext(CSs[n]) end
				elseif n == 3 then
					if CSs[1] == CSs[3] then
					elseif CSs[2] == CSs[3] then
					else html:wikitext(CSs[n]) end
				end
			end

			if Ings[n] ~= '' then
				if n > 1 and mode == 'inlinetd' then
					html:wikitext('<br />')
				end

				if CSs[n] ~= '' then
					html:wikitext(':'..Ings[n])
				else
					html:wikitext(Ings[n])
				end
			end

		elseif mode == 'table' and CSs[n] ~= '' then

			if n > 1 then
				table[n] = div:tag('table'):addClass('wikitable'):cssText('margin-left:1em;')
			else
				table[n] = div:tag('table'):addClass('wikitable')
			end
			table[n]:tag('tr'):tag('th'):wikitext(data[lang]['TitleCraftingStation']):tag('th'):wikitext(data[lang]['TitleIngredient'])
			table[n]:tag('tr'):tag('td'):wikitext(CSs[n]):tag('td'):cssText('text-align: center;'):wikitext(Ings[n])

		elseif mode == 'tabletd' then
		elseif mode == 'td' then
		else
		end

	end

	if mode == 'table' then html:wikitext(string.format('<div style="clear:both;"></div>')) end
	return tostring(html)
end

return p