Module:Infobox recipe

--[=[ -- Implements Template:Infobox recipe --]=]

local p = {}

local yesno = require('Module:Yesno') local params = require('Module:Paramtest') local commas = require('Module:Addcommas') local geprice = require('Module:Exchange')._price local skillpic = require('Module:Skill_clickpic')._main local editbutton = require('Module:Edit button')

function p.main(frame) local args = frame:getParent.args --	Function to handle all the materials listed	Creates a table to pass to _main consisting of	* Item name, as a wikilink	* Item image, as a wikilink, includes link to page	* Quantity	* Formatted price for display	* Clean price for calculations	If the parameter mat local function mat_list local ret_list = {} for i=1,10,1 do			local mat = args['mat'..i]			if mat and params.has_content(mat) then local name = mat -- 2 separate prices -- 1 for display -- 1 for calculation local price,price_raw local qty = params.default_to(args['mat'..i..'qty'],'1') local img = params.default_to(args['mat'..i..'img'],					name..'.png') :gsub('File:','') local price_v = params.default_to(args['mat'..i..'price'],'/') -- gemw = use exchange price if price_v:lower == 'gemw' then price_raw = geprice(name) price = commas._add(price_raw) -- parseable as number = use that elseif tonumber(commas._strip(price_v),10) then price_raw = tonumber(commas._strip(price_v),10) price = commas._add(price_raw) -- Some untradeables have a distinct market price -- because of their materials -- The template will help here -- Look for template call elseif price_v:find('') then price_raw = frame:preprocess(price_v) price = commas._add(price_raw) -- everything else -- use N/A for display and 0 for calculations else price_raw = 0 price = 'N/A' end

table.insert(ret_list, {					name = ..name..,					price = price,					price_raw = price_raw,					quantity = qty,					image = ''				} ) end end return ret_list end --	Function to get a list of additional requirements, should there be any	Will take non-blank parameters and simply paste them as is into a table	Which will then be passed into the _main function	-- local function get_other_reqs local req_list = {} for i=1,5,1 do			local req = args['misc'..i]			if req and req:find('%S') then table.insert(req_list,req) end end return req_list end -- Fetching and formatting parameters starting here local item_name,item_image, skill,level,experience = params.defaults{ {args.name,mw.title.getCurrentTitle.text}, {args.image,nil}, {args.skill,nil}, {args.level,nil}, {args.skillXP,nil} }

local skill2,experience2, item_alch,item_sell, quest = params.defaults{ {args.secSkill,nil}, {args.secXP,nil}, {args.alchValue,nil}, {args.storeValue,nil}, {args.quest,nil} }	-- Add to image, if it's defined if item_image then item_image = '' -- anti-redlink elseif mw.title.getCurrentTitle.text == 'Infobox recipe/preload' or mw.title.getCurrentTitle.text == 'PreloadPage/Orphanage' then item_image = '' else item_image = '' end -- Allow amount to be removed if it's 1 local item_amount = tonumber(args.amount,10) or 1

-- Tables to pass; call above functions local materials = mat_list local misc_reqs = get_other_reqs

-- Look for gemw in the parameter, otherwise nil local item_ge_v = string.lower(args.geValue or '') local item_gep if item_ge_v == 'gemw' then item_gep = geprice(item_name) elseif item_ge_v == 'no' then item_gep = 'no' else item_gep = nil end

-- If alchemy can be parsed as a number, do so	-- If "no", leave it	-- Otherwise alch is nil if item_alch then item_alchv = tonumber(commas._strip(item_alch),10) if item_alchv then item_alch = item_alchv elseif item_alch:lower == 'no' then item_alch = 'no' else item_alch = nil end end

-- If sell to store can be parsed as a number, do so	-- If "no", leave it	-- Otherwise sell to store is nil if item_sell then item_sellv = tonumber(commas._strip(item_sell),10) if item_sellv then item_sell = item_sellv elseif item_sell:lower == 'no' then item_sell = 'no' else item_sell = nil end end

-- Only accept "yes" or "no" -- Anything else defaults to no -- Should this default to unknown instead? local members = yesno(args.members) and 'Yes' or 'No'

-- Pass parameters to _main return p._main{ item_name, item_image, item_amount, materials, misc_reqs, skill, level, experience, skill2, experience2, item_gep, item_alch, item_sell, members, quest } end

-- -- Main -- function p._main(...) -- Unpack parameters local item_name,item_image,item_amount, materials,misc_reqs, skill,level,experience, skill2,experience2,item_gep, item_alch,item_sell, members,quest = unpack(...)

local ret = mw.html.create('table') :addClass('wikitable') :css({ width = '50%',				['max-width'] = '600px' }) -- Caption header local caption = item_image..' '..item_name if item_amount > 1 then caption = caption..' ×'..item_amount end ret:tag('caption'):wikitext(caption):done

-- Creation of row 1 -- Columns 1 and 5 local row1 = ret:tag('tr') :css('text-align','center')

local row1a = row1:tag('th') :css('width','20%')

-- Row 1 column 1 if skill and not (skill:lower == 'no') then row1a:wikitext(skillpic(skill)) else row1a:wikitext('\'\'?\'\' '..editbutton) end

-- Add columns 2, 3, 4 row1:tag('th') :attr('title','Sell to store value') :css('width','20%') :wikitext('') :done :tag('th') :attr('title','Grand Exchange median price') :css('width','20%') :wikitext('') :done :tag('th') :attr('title','High alchemy') :css('width','20%') :wikitext('') :done

local row1e = row1:tag('th') :css('width','20%') -- Row 1 column 5 if skill2 and not (skill2:lower == 'no') then row1e:wikitext(skillpic(skill2)) else row1e:wikitext('-') end

-- Creation of row 2 local row2 = ret:tag('tr') :css('text-align','center') local row2a = row2:tag('td') local row2b = row2:tag('td') local row2c = row2:tag('td') local row2d = row2:tag('td') local row2e = row2:tag('td')

-- Row 1 column 1 if experience then row2a:wikitext(commas._add(experience)..' XP') else row2a:wikitext('\'\'?\'\' '..editbutton) end

-- Row 1 column 2 if item_sell then if item_sell == 'no' then row2b:attr('title','Cannot be sold to general stores') :wikitext('-') elseif item_amount > 1 then row2b:attr('title',commas._add(item_sell)..' coins each') :wikitext(commas._add(item_sell * item_amount)) else row2b:wikitext(commas._add(item_sell)) end else row2b:wikitext('\'\'?\'\' '..editbutton) end

-- Row 1 column 3 if item_gep then if item_gep == 'no' then row2c:attr('title','Cannot be sold on the Grand Exchange') :wikitext('-') elseif item_amount > 1 then row2c:attr('title',commas._add(item_gep)..' coins each') :wikitext(commas._add(item_gep * item_amount)) else row2c:wikitext(commas._add(item_gep)) end else row2c:wikitext('\'\'?\'\' '..editbutton) end

-- Row 1 column 4 if item_alch then if item_alch == 'no' then row2d:attr('title','Cannot be alchemised') :wikitext('-') elseif item_amount > 1 then row2d:attr('title',commas._add(item_alch)..' coins each') :wikitext(commas._add(item_alch * item_amount)) else row2d:wikitext(commas._add(item_alch)) end else row2d:wikitext('\'\'?\'\' '..editbutton) end

-- Row 1 column 5 if experience2 then row2e:wikitext(commas._add(experience2)..' XP') else row2e:wikitext('-') end

-- Add "Requirements" header ret:tag('tr') :tag('th') :attr('colspan','5') :wikitext('Requirements') :done :done -- Skill requirement 1 if skill then ret:tag('tr') :tag('td') :attr('colspan','4') :wikitext(skillpic(skill)..' '..					params.ucflc(skill)..' level') :done :tag('td') :css('text-align','center') :wikitext(level) :done :done end

-- Quest requirement if quest then ret:tag('tr') :tag('td') :attr('colspan','5') :wikitext(skillpic('quest')..' '..quest) :done :done end

-- Miscellaneous requirements for i, v in ipairs(misc_reqs) do		ret:tag('tr') :tag('td') :attr('colspan','5') :wikitext(v) :done :done end

-- Members requirement ret:tag('tr') :tag('td') :attr('colspan','4') :wikitext(' Members only?') :done :tag('td') :css('text-align','center') :wikitext(members) :done :done

-- Materials header ret:tag('tr') :tag('th') :attr('colspan','5') :wikitext('Materials') :done :done :tag('tr') :tag('th') :attr('colspan','2') :wikitext('Item') :done :tag('th') :wikitext('Quantity') :done :tag('th') :wikitext('Price') :done :tag('th') :wikitext('Total') :done :done

-- Materials local total_price = 0 local valued_items = 0 -- Iterates through the tables in the table of materials for i, v in ipairs(materials) do		-- Fetch all the variables local mat_name = v.name local mat_price = v.price local mat_raw = v.price_raw local mat_ttl local mat_qty = v.quantity local mat_img = v.image -- If no price is given for item, delete total -- Otherwise quantity * price each if mat_raw == 0 then mat_ttl = '-' else mat_ttl = mat_qty * mat_raw end -- If item total is a number -- Add it to the overall total -- Indicate this by incrementing valued item count -- Done with total calculations, add commas to total for formatting if type(mat_ttl) == 'number' then total_price = total_price + mat_ttl mat_ttl = commas._add(mat_ttl) if mat_raw > 0 then valued_items = valued_items + 1 end end ret:tag('tr') :tag('td') :attr('colspan','2') :css('border-right','none') :wikitext(mat_img..' '..mat_name) :done :tag('td') :css({ ['border-left'] = 'none',					['border-right'] = 'none',					['text-align'] = 'center' }) :wikitext(mat_qty) :done :tag('td') :css({ ['border-left'] = 'none',					['text-align'] = 'center' }) :wikitext(mat_price) :done :tag('td') :css('text-align','center') :wikitext(mat_ttl) :done :done end

-- If no materials found -- Indicate nothing listed, add an edit button for the page if #materials == 0 then ret:tag('tr') :tag('td') :attr('colspan','5') :css({ ['font-style'] = 'italic',						['text-align'] = 'center' }) :wikitext('Materials unlisted '..editbutton) :done :done end

-- Total -- Hide if less than 2 items have a value if valued_items > 1 then ret:tag('tr') :tag('th') :attr('colspan','4') :css('text-align','right') :wikitext('Total price') :done :tag('td') :css('text-align','center') :wikitext(commas._add(total_price)) :done :done end

return ret end

return p