Module:BethNaught/RHH

From Wikisource
Jump to navigation Jump to search

-- A module to automate the positioning of the page number in a RunningHeader
-- based on its parity, and for related purposes.
-- © 2018 BethNaught, CC BY-SA 3.0

local p = {}

function p.three(frame)
	-- load arguments from frame
	local center = frame.args['center']
	local center_odd, center_even
	if center == nil then
		center_odd = frame.args['center_odd']
		center_even = frame.args['center_even']
		if center_odd == nil then center_odd = '' end
		if center_even == nil then center_even = '' end
	else
		-- if center is provided, it overrides specific odd/even values
		center_odd, center_even = center, center
	end
	-- pagenum loaded as a string; if not specified, we get nil, so pagenum_num
	-- is nil, and below we trigger the pagenum_num == nil handling block.
	local pagenum = frame.args['pagenum']
	local pagenum_num = tonumber(pagenum)
	-- argument loading finished
	
	-- initialise output variables
	local out_left, out_center, out_right = '', '', ''
	
	-- populate output variables based on pagenumber
	if pagenum_num == nil then
		-- for now, return an empty RunningHeader in this case
	elseif pagenum_num % 2 == 0 then
		out_left = pagenum
		out_center = center_even
	elseif pagenum_num % 2 == 1 then
		out_right = pagenum
		out_center = center_odd
	end
	
	-- time to return our RunningHeader
	return '{{RunningHeader|' .. out_left .. '|' .. out_center .. '|' .. out_right .. '}}'
end

return p