ವಿಷಯಕ್ಕೆ ಹೋಗು

ಮಾಡ್ಯೂಲ್:Running header

ವಿಕಿಸೋರ್ಸ್ದಿಂದ

Implements Template:RunningHeader


--[=[
Implements [[Template:RunningHeader]]
]=]

require('strict')

local getArgs = require('Module:Arguments').getArgs

local p = {}

function p._running_header(args)
	-- holds tracking categories
	local tracking_cats = {}
	
	-- aliases for first 3 parameters
	if args.left or args.center or args.centre or args.right then
		-- this is fine but it's worth keeping track of
		table.insert(tracking_cats, '[[Category:' .. 'Running headers using explicit parameter names' .. ']]')
		
		-- check for duplicates (which are a problem)
		if (args[1] and args.left) or (args[2] and args.center) or (args[2] and args.centre) or (args.center and args.centre) or (args[3] and args.right) then
			table.insert(tracking_cats, '[[Category:' .. 'Pages using duplicate arguments in template calls' .. ']]')
		end
		
		-- use aliases
		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
	
	-- get number of cells (largest-numbered parameter)
	-- can't use #args because that doesn't work consistently on tables that aren't sequences
	-- table.maxn also seems not to work
	local cell_count = 0
	for k, v in pairs(args) do
		local i = tonumber(k)
		if i and i > cell_count then
			cell_count = i
		end
	end
	
	-- track headers which don't set the contents of every cell
	local undefined_entries = false
	
	-- track how many cells have content
	local content_entries = 0
	
	for i = 1, cell_count do
		if not args[i] then
			undefined_entries = true
		elseif args[i] and args[i] ~= '' then
			content_entries = content_entries + 1
		end
	end
	
	if undefined_entries then
		-- track headers with undefined entries (fine but worth keeping track of)
		table.insert(tracking_cats, '[[Category:' .. 'Running headers with undefined entries' .. ']]')
	end
	if content_entries == 1 then
		-- track headers with only one non-blank entry
		table.insert(tracking_cats, '[[Category:' .. 'Running headers with only one content entry' .. ']]')
	end
	
	if cell_count == 0 or content_entries == 0 then
		-- track headers with no entries (pointless)
		table.insert(tracking_cats, '[[Category:' .. 'Empty running headers' .. ']]')
	elseif cell_count == 1 then
		-- track 1-cell headers (fine but worth keeping track of)
		table.insert(tracking_cats, '[[Category:' .. 'Running headers with one entry' .. ']]')
	elseif cell_count == 2 then
		-- track 2-cell headers (fine but worth keeping track of)
		table.insert(tracking_cats, '[[Category:' .. 'Running headers with two entries' .. ']]')
	elseif cell_count > 4 then
		-- track headers with more than 4 cells (fine but worth keeping track of)
		table.insert(tracking_cats, '[[Category:' .. 'Running headers with more than four entries' .. ']]')
	end
	
	-- TEMPORARY FOR MIGRATION: enforce 3-cell minimum
	cell_count = math.max(cell_count, 3)
	
	-- assemble cells
	local cells = {}
	
	-- classes
	local base_class = 'wst-rh'
	local cell_class = base_class .. '-cell'
	local extra_cell_classes = {
		[3] = {'wst-rh-left', 'wst-rh-center', 'wst-rh-right'}
	}
	extra_cell_classes = extra_cell_classes[cell_count] or {}
	
	for i = 1, cell_count do
		cells[i] = mw.html.create('div')
			:addClass(cell_class)
			:wikitext(args[i] or '')
		
		if extra_cell_classes[i] then
			cells[i]:addClass(extra_cell_classes[i])
		end
		
		cells[i] = tostring(cells[i])
	end
	
	-- assemble header
	local header_class = table.concat({
		base_class,
		base_class .. '-' .. cell_count,
		args.class or (base_class .. '-default')
	}, ' ')
	
	local header_div = mw.html.create('div')
		:addClass(header_class)
		:wikitext(table.concat(cells))
	
	return tostring(header_div) .. table.concat(tracking_cats)
end

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

return p