Module:Infobox Location

-- -- Module for Template:Infobox Location

local p = {}

local onmain = require('Module:Mainonly').on_main local yesno = require('Module:Yesno') local paramtest = require('Module:Paramtest') local infobox = require('Module:Infobox') local cleanimg = require('Module:Clean image').clean local commas = require('Module:Addcommas')._add

-- Main function called with invokes function p.main(frame) local args = frame:getParent.args local ret = infobox.new(args)

ret:defineParams{ { name = 'image', func = { name = imgarg, params = { 'image' }, flag = { 'd' } } }, { name = 'name', func = 'name' }, { name = 'level', func = 'has_content' }, { name = 'travel_cost', func = { name = numberarg, params = { 'travel_cost' } } }, { name = 'discovered', func = { name = yesnoparam, params = { 'discovered' } } }, { name = 'ghost', func = { name = yesnoparam, params = { 'ghost' } } }, { name = 'age', func = { name = ageparam, params = { 'age' } } }, { name = 'from', func = 'has_content' }, { name = 'item', func = 'has_content' }, { name = 'itemquantity', func = { name = itemquantityparam, params = { 'itemquantity' } } }, { name = 'itemdisp', func = { name = itemdisp, params = { 'item', 'itemquantity' }, flag = { 'd', 'd' } } }, { name = 'id', func = { name = iddisp, params = { 'id' }, flag = { 'p' } } }, }

ret:create ret:cleanParams

ret:defineLinks({ links = {{ 'Template:%s/doc', 'Infobox' },		{ 'Template_talk:%s', 'Talk page' }}, colspan = '2' })

ret:defineName('Infobox Location') ret:addButtonsCaption

-- PARAMETER: image ret:addRow{ { tag = 'argd', content = 'image', class='infobox-image', colspan = '2' } }	-- PARAMETER: name ret:addRow{ { tag = 'argh', content = 'name', class='infobox-header', colspan = '2' } }	-- PARAMETER: level ret:addRow{ { tag = 'th', content = 'Level' }, { tag = 'argd', content = 'level' } }	-- PARAMETER: travel_cost if not ret:paramDefined('discovered') or not yesno(ret:param('discovered')) then ret:addRow{ { tag = 'th', content = 'Travel cost' }, { tag = 'argd', content = 'travel_cost' } }	end if ret:paramDefined('discovered') and yesno(ret:param('discovered')) then ret:addRow{ { tag = 'th', content = 'Requirements', class = 'infobox-subheader', colspan = '2' } }		if ret:paramDefined('from') then ret:addRow{ { tag = 'th', content = 'Accessible from' }, { tag = 'argd', content = 'from' } }		end ret:addRow{ { tag = 'th', content = 'Ghost Ship' }, { tag = 'argd', content = 'ghost' } }		if ret:paramDefined('age') then ret:addRow{ { tag = 'th', content = 'Age' }, { tag = 'argd', content = 'age' } }		end if ret:paramDefined('item') then ret:addRow{ { tag = 'th', content = 'Item' }, { tag = 'argd', content = 'itemdisp' } }		end end ret:addRow{ { tag = 'th', content = 'Advanced data', class = 'infobox-subheader', colspan = '2' }, meta = {addClass = 'advanced-data'} }

-- PARAMETER: id	:addRow{ { tag = 'th', content = 'ID' }, { tag = 'argd', content = 'id' }, meta = {addClass = 'advanced-data'} }	ret:finish if onmain then local a1 = ret:param('all') local a2 = ret:categoryData ret:wikitext(addcategories(a1, a2)) end return ret:tostring end

function imgarg(arg) if infobox.isDefined(arg) then return cleanimg{ file = arg, width = 300, height = 300 } end return nil end

function numberarg(c) if type(c) ~= 'number' then c = tonumber(c,10) or 0 end

if c == -1 then --c == 0 or		return '-' -- all other numbers elseif c > -1 then return commas(c) -- not a number = nil -- shouldn't be used, but it's a fallback else return nil end end

function yesnoparam(p) if infobox.isDefined(p) then local b = yesno(p) if b then return 'Yes' elseif not b then return 'No' else return nil end end return nil end

function ageparam(c) if type(c) ~= 'number' then c = tonumber(c,10) or 0 end

if c == 0 or c == -1 then return '-' -- all other numbers elseif c > 0 then return commas(c)..' days' -- not a number = nil -- shouldn't be used, but it's a fallback else return nil end end

function itemquantityparam(p) if infobox.isDefined(p) then return p	end return 1 end

function itemdisp(item, quantity) if not infobox.isDefined(item) then return nil end quantity = tonumber(quantity, 10) if quantity > 1 then return quantity..' '..item else return item end end

function iddisp(id) if infobox.isDefined(id) then if id:lower ~= 'no' then local r = string.gsub(id, ', *', ', ') return r		elseif id:lower == 'no' then return 'None' end end return nil end

function addcategories(args, catargs) local ret = { 'Locations' } local cat_map = { -- Added if the parameter has content defined = { },		-- Added if the parameter has no content notdefined = { id = 'Needs ID', },	}	-- Run and add mapped categories for n, v in pairs(cat_map.defined) do		if catargs[n] and catargs[n].one_defined then table.insert(ret,v) end end for n, v in pairs(cat_map.notdefined) do		if catargs[n] and catargs[n].all_defined == false then table.insert(ret,v) end end

-- combine table and format category wikicode for i, v in ipairs(ret) do		if (v ~= '') then ret[i] = string.format('', v)		end end

return table.concat(ret, '') end

return p