Core logic for Module:RunningHeader and {{RunningHeader}}.


--[=[
Implements running header templates
]=]
require('strict')

local getArgs = require('Module:Arguments').getArgs
local make_attribute_string = require('Module:Optional CSS attribute').make_attribute_string

local p = {}

-- Core logic

local function running_header_core(args)
	-- how many cells the header has
	local n = tonumber(args.n)
	if not n and args[4] then
		n = 4
	elseif not n then
		n = 3
	end
	
	-- classes
	local base_class = 'wst-running-header'
	local class = args.class or (base_class .. '-default')
	local class_attr = make_attribute_string('class', {['class'] = table.concat({base_class, base_class .. '-' .. n, class}, ' ')})
	local cell_class_attr = make_attribute_string('class', {['class'] = base_class .. '-cell'})
	
	-- manual style for the whole header
	local style_attr = make_attribute_string('style', {['style'] = args.style})
	local manual_style = style_attr ~= ''
	
	-- alternate names for the cell content if there are 3 cells
	if n == 3 then
		args[1] = args[1] or args.left
		args[2] = args[2] or args.center or args.centre
		args[3] = args[3] or args.right
	end
	
	-- assemble the header
	local div_open = '<div ' .. class_attr .. ' ' .. style_attr .. '>'
	
	local cells = {}
	for k = 1, n, 1 do
		-- manual style for the cell
		local cell_style_attr = make_attribute_string('style', {['style'] = args['style' .. k]})
		manual_style = manual_style or cell_style_attr ~= ''
		
		cells[k] = '<div ' .. cell_class_attr .. ' ' .. cell_style_attr .. '>' .. (args[k] or '') .. '</div>'
	end
	
	local style_cat = ''
	if manual_style then
		style_cat = '[[Category:Running headers applying manual styles]]'
	end
	
	return div_open .. table.concat(cells) .. '</div>' .. style_cat
end

-- Template:RunningHeader

function p._running_header(args)
	return running_header_core(args)
end

function p.running_header(frame)
	local args = getArgs(frame, {trim = true, removeBlanks = false})
	return p._running_header(args)
end

return p