Sari la conținut

Modul:Running header

Documentația acestui modul poate fi creată la Modul:Running header/doc

--[=[
Implementează [[Format:AntetPagină]]
]=]

require('strict')

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

local p = {}

function p._running_header(args)
	-- holds tracking categories
	local tracking_cats = {}
	
	-- alternate names 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, '[[Categorie:' .. 'Antete de pagină cu nume de parametri expliciți' .. ']]')
		-- 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, '[[Categorie:' .. 'Pagini care conțin formate cu argumente duplicate' .. ']]')
		end
		-- use alternate names
		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
	
	-- tracking category for headers which don't set the contents of every cell
	-- fine but worth keeping track of
	local undefined_entries = false
	for i = 1, cell_count do
		if not args[i] then
			undefined_entries = true
		end
	end
	if undefined_entries then
		table.insert(tracking_cats, '[[Categorie:' .. 'Antete de pagină cu parametri lăsați necompletați' .. ']]')
	end
	
	if cell_count == 0 then
		-- track headers with no entries (pointless)
		table.insert(tracking_cats, '[[Categorie:' .. 'Antete de pagină fără parametri' .. ']]')
	elseif cell_count == 1 then
		-- track 1-cell headers (fine but worth keeping track of)
		table.insert(tracking_cats, '[[Categorie:' .. 'Antete de pagină cu un singur parametru' .. ']]')
	elseif cell_count == 2 then
		-- track 2-cell headers (fine but worth keeping track of)
		table.insert(tracking_cats, '[[Categorie:' .. 'Antete de pagină cu doi parametri' .. ']]')
	elseif cell_count > 4 then
		-- track headers with more than 4 cells (fine but worth keeping track of)
		table.insert(tracking_cats, '[[Categorie:' .. 'Antete de pagină cu peste patru parametri' .. ']]')
	end
	
	-- TEMPORARY FOR MIGRATION: enforce 3-cell minimum
	cell_count = math.max(cell_count, 3)
	
	-- assemble cells
	local cells = {}
	local base_class = 'wst-rh'
	local base_class2 = 'wst-running-header' -- TEMPORARY FOR MIGRATION
	local cell_classes = {
		[3] = {'left', 'center', 'right'}
	}
	for i = 1, cell_count do
		-- set cell class
		-- local cell_class = base_class .. '-cell'
		local cell_class = base_class .. '-cell' .. ' ' .. base_class2 .. '-cell' -- TEMPORARY FOR MIGRATION
		if cell_classes[cell_count] and cell_classes[cell_count][i] then
			cell_class = cell_class .. ' ' .. base_class .. '-' .. cell_classes[cell_count][i]
		end
		-- set cell
		cells[i] = tostring(mw.html.create('div'):addClass(cell_class):wikitext(args[i] or ''))
	end
	
	-- header classes
	local header_class = table.concat({
		base_class,
		base_class .. '-' .. cell_count,
		base_class2, -- TEMPORARY FOR MIGRATION
		base_class2 .. '-' .. cell_count, -- TEMPORARY FOR MIGRATION
		-- args.class or (base_class .. '-default')
		args.class or (base_class .. '-default' .. ' ' .. base_class2 .. '-default') -- TEMPORARY FOR MIGRATION
	}, ' ')
	
	-- assemble header
	return tostring(mw.html.create('div'):addClass(header_class):wikitext(table.concat(cells))) .. 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