memory alpha
Module documentation ()

This Lua module implements {{Sidebar}}.

Module source

local p = {}

local function getArgNums(args)
	local nums = {}
	local numPrev = 0

	for k, v in pairs(args) do
		local num = k:match('^[%w ]+([1-9]%d*)$')
		if num and tonumber(num) ~= numPrev then
			table.insert(nums, tonumber(num))
			numPrev = tonumber(num)
		end
	end

	table.sort(nums)
	return nums
end

local function templateStyles(frame, source)
	if source then
		return frame:extensionTag{
			name = 'templatestyles',
			args = {src = source}
		}
	else
		return ''
	end
end

function p.main(frame)
	local args = frame:getParent().args
	local content = ''
	local pi_theme = args.theme or 'wikia'
	local pi_layout = args.layout or 'default'
	local pi_class = args.class or ''

	if args.title then
		content = content .. '<title><default>' .. args.title .. '</default></title>'
	end

	for k, v in ipairs(getArgNums(args)) do
		if args['image ' .. k] then
			content = content .. '<image'
			if args['image name ' .. k] then
				content = content .. ' name="' .. args['image name ' .. k] .. '"'
			end
			content = content .. '><default>' .. args['image ' .. k] .. '</default>'
			if args['alt ' .. k] then
				content = content .. '<alt><default>' .. args['alt ' .. k] .. '</default></alt>'
			end
			if args['caption ' .. k] then
				content = content .. '<caption><default>' .. args['caption ' .. k] .. '</default></caption>'
			end
			content = content .. '</image>'
		elseif args['data value ' .. k] then
			content = content .. '<data><default>' .. args['data value ' .. k] .. '</default>'
			if args['data label ' .. k] then
				if pi_layout == 'stacked' then
					content = content .. '<label>' .. args['data label ' .. k] .. '</label>'
				else
					content = content .. '<label>' .. args['data label ' .. k] .. ':</label>'
				end
			end
			content = content .. '</data>'
		elseif args['header ' .. k] then
			content = content .. '<header>' .. args['header ' .. k] .. '</header>'
		elseif args['navigation ' .. k] then
			content = content .. '<navigation>' .. args['navigation ' .. k] .. '</navigation>'
		elseif args['group ' .. k] == 'here' then
			content = content .. '<group'
			if args['group layout ' .. k] then
				content = content .. ' layout="' .. args['group layout ' .. k] .. '"'
			end
			if args['group show ' .. k] then
				content = content .. ' show="' .. args['group show ' .. k] .. '"'
			end
			if args['group collapse ' .. k] then
				content = content .. ' collapse="' .. args['group collapse ' .. k] .. '"'
			end
			if args['group row-items ' .. k] then
				content = content .. ' row-items="' .. args['group row-items ' .. k] .. '"'
			end
			content = content .. '>'
		elseif args['group end ' .. k] == 'here' then
			content = content .. '</group>'
		elseif args['panel ' .. k] == 'here' then
			content = content .. '<panel>'
		elseif args['panel end ' .. k] == 'here' then
			content = content .. '</panel>'
		elseif args['section ' .. k] == 'here' then
			content = content .. '<section>'
			if args['section label ' .. k] then
				content = content .. '<label>' .. args['section label ' .. k] .. '</label>'
			end
		elseif args['section end ' .. k] == 'here' then
			content = content .. '</section>'
		end
	end

	local sidebar = frame:extensionTag{
		name = 'infobox',
		content = content,
		args = {
			theme = pi_theme,
			layout = pi_layout,
		}
	}

	local sidebarContainer = mw.html.create('div'):wikitext(
		templateStyles(frame, 'Module:Sidebar/styles.css'),
		templateStyles(frame, args.styling),
		sidebar
	):addClass('sidebar-container ' .. pi_class)

	return tostring(sidebarContainer)
end

return p