Module:Infobox

From Absit Omen Lexicon

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

local p = {}

function p.addSection(values, labels, specialRows, args, debugDisplay)
	section = mw.html.create()
	section
		:tag('tr')
			:tag('th')
				:attr('colspan', '2')
				:wikitext(labels.header)
			:done()
		:done()

	empty = true
	for _, value in ipairs(values) do
		row = p.addRow(args[value], labels[value], specialRows[value], debugDisplay)
		section:node(row)
		if row then
			empty = false
		end
  	end

  	if empty == true and not(debugDisplay) then
  		return nil
  	end

	return section
end

function p.addRow(value, label, specialRow, debugDisplay)
	if value == nil and not(debugDisplay) then
		return false
	end

	row = mw.html.create('tr')

	labelCell = mw.html.create('td')
	labelCell:addClass('ib-label')
	valueCell = mw.html.create('td')
	valueCell:addClass('ib-value')	

	if specialRow and specialRow ~= 'colspan' then
		valueCell
			:tag(specialRow)
			:wikitext(value)
	else
		valueCell:wikitext(value)

	end


	if specialRow then
		labelCell:attr('colspan', '2')
		valueCell:attr('colspan', '2')
	end

	if label then
		labelCell:wikitext(label)

		row
			:node(labelCell)
		if specialRow then
			row:done()
				:tag('tr')
		end
	end

	row:node(valueCell)

	return row

end


function p.addHeader(image, title)
	header = mw.html.create('tr')
	header
		:tag('td')
			:attr('colspan', '2')
			:addClass('ib-image')
			:wikitext('[[File:' .. image .. '|250px]]')
			:done()
		:done()
		:tag('tr')
			:tag('th')
				:attr('colspan', '2')
				:wikitext(title)

		return header
end

function p.infoboxtable(type)
	type = type or 'generic'

	root = mw.html.create('table')
	
	return root
end

function p.infoboxdiv(type)
	type = type or 'generic'

	root = mw.html.create('div')

	root
		:addClass('ib')
		:addClass('ib-' .. type)
		:addClass('plainlinks')

	return root
end

-- usage str = str:gsub("(%a)([%w_']*)", tchelper)
function p.tchelper(first, rest)
   return first:upper()..rest:lower()
end

function p.cargoStore(args, cargoFields, cargoTable)

	local cargoArgs = {_table = cargoTable}
	local cargo = ''

	for _, field in ipairs(cargoFields) do
		local default = field.default and (args[field.default] and args[field.default] or field.default) or ''
		local value = args[field.arg] and args[field.arg] or default
		table.insert(cargoArgs, ('%s=%s'):format(field.field, value))
	end

	mw.getCurrentFrame():callParserFunction{ 
		name = '#cargo_store',
		args = cargoArgs
	}

	return
	

end

return p