Documentation for this module may be created at Module:Header/attribution/doc

require('strict')

local p = {}

local yesno = require('Module:Yesno')
local tableTools = require('Module:TableTools')

local function anon(text)
	text = text or 'Anonymous'
	local cat = ''
	if mw.title.getCurrentTitle():inNamespaces(0, 114) then
		cat = '[[Category:' .. 'Works by unknown authors' .. ']]'
	end
	return '[[Portal:Anonymous texts|' .. text .. ']]' .. cat
end

local function anon_nocat(text)
	return '[[Portal:Anonymous texts|' .. (text or 'Anonymous') .. ']]'
end

local contrib_types = {
	{noun = 'author', prefix = 'by'},
	{noun = 'attributed to', prefix = 'attributed to', special_cases = {}},
	{noun = 'adapter', prefix = 'adapted by'},
	{noun = 'abridger', prefix = 'abridged by'},
	{noun = 'illustrator', prefix = 'illustrated by'},
	{noun = 'director', prefix = 'directed by'},
	{noun = 'lyricist', prefix = 'lyrics by'},
	{noun = 'librettist', prefix = 'libretto by'},
	{
		noun = 'book by',
		prefix = 'book by',
		special_cases = {
			['?'] = 'unknown author',
			['unknown'] = 'unknown author',
			['not mentioned'] = 'unknown author',
			['anon'] = anon,
			['anonymous'] = anon,
			['various'] = 'various authors'
		}
	},
	{noun = 'composer', prefix = 'composed by'},
	{noun = 'arranger', prefix = 'arranged by'},
	{
		noun = 'translator',
		prefix = 'translated by',
		special_cases = {
			['?'] = 'unknown translator',
			['unknown'] = 'unknown translator',
			['not mentioned'] = 'unknown translator',
			['anon'] = anon,
			['anonymous'] = anon,
			['various'] = 'various translators',
			['wikisource'] = '[[Wikisource:Translations|Wikisource]]'
		}
	},
	{
		noun = 'editor',
		prefix = 'edited by',
		special_cases = {
			['?'] = 'unknown editor',
			['unknown'] = 'unknown editor',
			['not mentioned'] = 'unknown editor',
			['anon'] = anon_nocat,
			['anonymous'] = anon_nocat,
			['various'] = 'various editors'
		}
	}
}

for i = 1, #contrib_types do
	local noun = contrib_types[i]['noun']
	contrib_types[i]['param_name'] = contrib_types[i]['param_name'] or string.gsub(noun, ' ', '-')
	contrib_types[i]['special_cases'] = contrib_types[i]['special_cases'] or {
		['?'] = 'unknown ' .. noun,
		['unknown'] = 'unknown ' .. noun,
		['not mentioned'] = 'unknown ' .. noun,
		['anon'] = anon,
		['anonymous'] = anon,
		['various'] = 'various ' .. noun .. 's'
	}
	contrib_types[i]['index'] = i
end

p.attr_data = {}
for k, v in pairs(contrib_types) do
	p.attr_data[v['param_name']] = v
end

local function construct_attribution_span(args)
	local aspan = mw.html.create('span')
		:addClass('vcard')
		:attr('id', 'header-' .. args.span_param_name .. '-text')
		:tag('span'):addClass('fn'):wikitext(args.atext)
	
	return tostring(mw.html.create('span')
		:addClass('contributor-text')
		:wikitext(args.prefix .. tostring(aspan))
	)
end

local function construct_attribution(args, data)
	local param_name = data['param_name']
	local span_param_name = param_name
	local prefix = data['prefix'] .. ' '
	
	if param_name == 'section-author' then
		span_param_name = 'contributor'
	end
	
	if param_name == 'author' and args['override-' .. param_name] then
		prefix = '' -- legacy parameter
	elseif param_name == 'translator' and args.language then
		prefix = 'translated from ' .. (args.language_name or 'unrecognized language') .. ' by '
	end
	
	-- override (legacy parameter)
	local atext = args['override-' .. param_name]
	if atext then
		if args.template_name == 'Translation header' and param_name == 'translator' then
			atext = atext .. ' and [[Wikisource:Translations|Wikisource]]'
		end
		return construct_attribution_span({span_param_name = span_param_name, prefix = prefix, atext = atext})
	end
	
	-- get author1, author2, ...
	local all_contributors = {}
	for k, v in pairs(args) do
		local n
		local info
		
		local param_name_pattern = string.gsub(param_name, '%-', '%%%-')
		local nText = string.match(k, '^' .. param_name_pattern .. '%d*$')
		local nDisplay = string.match(k, '^' .. param_name_pattern .. '%d*%-display$')
		local nNoLink = string.match(k, '^' .. param_name_pattern .. '%d*%-nolink$')
		
		if nText then
			n = string.gsub(nText, param_name_pattern .. '(%d*)$', '%1')
			n = tonumber(n) or 1
			info = 'text'
		elseif nDisplay then
			n = string.gsub(nDisplay, param_name_pattern .. '(%d*)%-display$', '%1')
			n = tonumber(n) or 1
			info = 'display'
		elseif nNoLink then
			n = string.gsub(nNoLink, param_name_pattern .. '(%d*)%-nolink$', '%1')
			n = tonumber(n) or 1
			info = 'nolink'
		end
		
		if n then
			all_contributors[n] = all_contributors[n] or {}
			all_contributors[n][info] = v
		end
	end
	
	local contributors = {}
	for n, contributor in pairs(all_contributors) do
		local text = contributor['text']
		local display = contributor['display']
		local nolink = yesno(contributor['nolink']) or false
		
		if nolink then
			contributors[n] = display or text
		elseif text then
			local special = false
			for k, v in pairs(data['special_cases']) do
				if string.lower(text) == k then
					special = true
					if args.template_name == 'Translation header' and param_name == 'translator' and k == 'wikisource' then
						display = nil
					elseif type(v) == 'function' then
						display = v(display)
					else
						display = v
					end
					break
				end
			end
			
			if not special then
				if not display then
					display = string.gsub(text, ' %(.*%)$', '')
				end
				display = '[[Author:' .. text .. '|' .. display .. ']]'
			end
			if display then
				contributors[n] = display
			end
		end
	end
	
	contributors = tableTools.compressSparseArray(contributors)
	if args.template_name == 'Translation header' and param_name == 'translator' then
		table.insert(contributors, '[[Wikisource:Translations|Wikisource]]')
	end
	
	if #contributors == 0 then
		return nil
	elseif #contributors == 1 then
		atext = contributors[1]
	else
		atext = table.concat(contributors, ', ', 1, #contributors - 1) .. ' and ' .. contributors[#contributors]
	end
	
	return construct_attribution_span({span_param_name = span_param_name, prefix = prefix, atext = atext})
end

function p.construct_attributions(args)
	local attributions = {}
	for i = 1, #contrib_types do
		local atext = construct_attribution(args, contrib_types[i])
		if atext then
			table.insert(attributions, atext)
		end
	end
	
	if #attributions == 0 then
		return ''
	end
	
	return table.concat(attributions, ', ')
end

-- section

function p.construct_section(args)
	local section_text = args['section']
	
	if not section_text or section_text == '' then
		return ''
	end
	
	local attributions = {}
	for i = 1, #contrib_types do
		local data = contrib_types[i]
		data['param_name'] = 'section-' .. data['param_name']
		
		local atext = construct_attribution(args, data)
		if atext then
			table.insert(attributions, atext)
		end
	end
	
	if #attributions > 0 then
		local sep = ' '
		if #attributions > 1 then
			sep = '<br/>'
		end
		section_text = section_text .. sep .. table.concat(attributions, ', ')
	end
	
	return tostring(mw.html.create('div')
		:addClass('header-section-text')
		:wikitext(section_text)
	)
end

return p