Pergi ke kandungan

Modul:Running header

Daripada Wikisumber
--[=[
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:' .. 'Tajuk berturut menggunakan nama parameter eksplisit' .. ']]')
		
		-- 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:' .. 'Laman menggunakan argumen pendua dalam panggilan templat' .. ']]')
		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:' .. 'Tajuk berturut dengan entri tidak ditakrif' .. ']]')
	end
	if content_entries == 1 then
		-- track headers with only one non-blank entry
		table.insert(tracking_cats, '[[Category:' .. 'Tajuk berturut dengan satu entri kandungan' .. ']]')
	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:' .. 'Tajuk berturut dengan satu entri' .. ']]')
	elseif cell_count == 2 then
		-- track 2-cell headers (fine but worth keeping track of)
		table.insert(tracking_cats, '[[Category:' .. 'Tajuk berturut dengan dua entri' .. ']]')
	elseif cell_count > 4 then
		-- track headers with more than 4 cells (fine but worth keeping track of)
		table.insert(tracking_cats, '[[Category:' .. 'Tajuk berturut dengan lebih daripada empat entri' .. ']]')
	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