Implements


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

local getArgs = require('Module:Arguments').getArgs
local running_header = require('Module:RunningHeader/core')._running_header
local make_attribute_string = require('Module:Optional CSS attribute').make_attribute_string
local toArabic = require('Module:Roman').toArabic

local p = {}

-- Template:RunningHeader-centered

function p._running_header_centered(args)
	local center_width = tonumber(args.center_width) or tonumber(args['center-width'])
	if args[4] then
		args.n = 4
		if center_width then
			local center_width_style = 'width:' .. center_width .. '%'
			local outer_width_style = 'width:' .. (100 - 2 * center_width)/2 .. '%'
			args.style1 = outer_width_style
			args.style2 = center_width_style
			args.style3 = center_width_style
			args.style4 = outer_width_style
		end
	else
		args.n = 3
		if center_width then
			local center_width_style = 'width:' .. center_width .. '%'
			local outer_width_style = 'width:' .. (100 - center_width)/2 .. '%'
			args.style1 = outer_width_style
			args.style2 = center_width_style
			args.style3 = outer_width_style
		end
	end
	args.class = (args.class or '') .. ' centered'
	return running_header(args)
end

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

-- Template:Recto-verso

function p._rv(args)
	local n = tonumber(args[1]) or tonumber(toArabic({args[1]})) or 0
	if n % 2 == 1 then
		return args[2]
	else
		return args[3]
	end
end

function p.rv(frame)
	return p._rv(getArgs(frame))
end

-- Template:Recto-verso header

function p._rvh(args)
	local pagenum = args[1]
	local recto = args[2]
	local verso = args[3] or args[2]
	local pagenum_display = args[4] or args[1]
	
	return p._rv({
		pagenum,
		running_header({[2] = recto, [3] = pagenum_display}),
		running_header({[1] = pagenum_display, [2] = verso})
	})
end

function p.rvh(frame)
	return p._rvh(getArgs(frame))
end

-- Template:Recto-verso header3

function p._rvh3(args)
	local pagenum = args[1]
	local recto = args[2]
	local verso = args[3]
	
	return p._rv({
		pagenum,
		running_header({[1] = recto, [3] = verso}),
		running_header({[1] = verso, [3] = recto})
	})
end

function p.rvh3(frame)
	return p._rvh3(getArgs(frame))
end

-- Template:Rh/1rv

function p._rh_1rv(args)
	return p._rv({
		args[2] or args[1],
		running_header({[1] = args[1], ['n'] = 1, ['class'] = '__recto'}),
		running_header({[1] = args[1], ['n'] = 1, ['class'] = '__verso'})
	})
end

function p.rh_1rv(frame)
	return p._rh_1rv(getArgs(frame))
end

-- Template:Rh/lr

function p._rh_lr(args)
	return p._rv({
		args[1],
		running_header({[1] = args[2], [2] = args[1], ['n'] = 2, ['class'] = 'wst-recto'}),
		running_header({[1] = args[1], [2] = args[2], ['n'] = 2, ['class'] = 'wst-verso'})
	})
end

function p.rh_lr(frame)
	return p._rh_lr(getArgs(frame))
end

-- Template:Rvh-chap

function p._rvh_chap(args)
	return p._rv({
		args[1],
		running_header({args[2], args[3], args[1]}),
		running_header({args[1], args[3], args[2]})
	})
end

function p.rvh_chap(frame)
	return p._rvh_chap(getArgs(frame))
end

-- Template:Rvh-chap/2

function p._rvh_chap_2(args)
	return p._rv({
		args[1],
		running_header({args[2], args[3], args[1]}),
		running_header({args[1], args[3], args[4]})
	})
end

function p.rvh_chap_2(frame)
	return p._rvh_chap_2(getArgs(frame))
end

-- Template:Rvh/header

function p._rvh_header(args)
	return p._rv({
		args[1],
		running_header({[1] = args[2], ['n'] = 1}),
		running_header({[1] = args[3] or args[2], ['n'] = 1})
	})
end

function p.rvh_header(frame)
	return p._rvh_header(getArgs(frame))
end

return p