Pergi ke kandungan

Modul:Header/attribution

Daripada Wikisumber
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 = '[[Kategori:' .. 'Karya pengarang yang tidak diketahui' .. ']]'
	end
	return '[[Portal:Teks awanama|' .. text .. ']]' .. cat
end

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

local contrib_types = {
	{noun = 'author', prefix = 'dikarang oleh'},
	{noun = 'attributed to', prefix = 'dikaitkan dengan', special_cases = {}},
	{noun = 'adapter', prefix = 'diadaptasi oleh'},
	{noun = 'abridger', prefix = 'diringkaskan oleh'},
	{noun = 'illustrator', prefix = 'dihias oleh'},
	{noun = 'director', prefix = 'arahan'},
	{noun = 'lyricist', prefix = 'seni kata disediakan oleh'},
	{noun = 'librettist', prefix = 'libreto disediakan oleh'},
	{
		noun = 'book by',
		prefix = 'buku oleh',
		special_cases = {
			['?'] = 'pengarang yang tidak diketahui',
			['unknown'] = 'pengarang yang tidak diketahui',
			['not mentioned'] = 'pengarang yang tidak diketahui',
			['anon'] = anon,
			['anonymous'] = anon,
			['various'] = 'pelbagai pengarang'
		}
	},
	{noun = 'composer', prefix = 'digubah oleh'},
	{noun = 'arranger', prefix = 'disusun oleh'},
	{
		noun = 'translator',
		prefix = 'diterjemah oleh',
		special_cases = {
			['?'] = 'penterjemah yang tidak diketahui',
			['unknown'] = 'penterjemah yang tidak diketahui',
			['not mentioned'] = 'penterjemah yang tidak diketahui',
			['anon'] = anon,
			['anonymous'] = anon,
			['various'] = 'pelbagai penterjemah',
			['wikisource'] = '[[Wikisumber:Terjemahan|Wikisumber]]'
		}
	},
	{
		noun = 'editor',
		prefix = 'disunting oleh',
		special_cases = {
			['?'] = 'penyunting yang tidak diketahui',
			['unknown'] = 'penyunting yang tidak diketahui',
			['not mentioned'] = 'penyunting yang tidak diketahui',
			['anon'] = anon_nocat,
			['anonymous'] = anon_nocat,
			['various'] = 'pelbagai penyunting'
		}
	}
}

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 {
		['?'] = noun .. 'yang tidak diketahui ',
		['unknown'] = noun .. 'yang tidak diketahui ',
		['not mentioned'] = noun .. 'yang tidak diketahui ',
		['anon'] = anon,
		['anonymous'] = anon,
		['various'] = 'pelbagai ' .. noun
	}
	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 = 'pengarang ' -- legacy parameter
	elseif param_name == 'translator' and args.language then
		prefix = 'diterjemah daripada bahasa ' .. (args.language_name or 'yang tidak dapat dikenal pasti') .. ' oleh '
	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 [[Wikisumber:Terjemahan|Wikisumber]]'
		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 = '[[Pengarang:' .. 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, '[[Wikisumber:Terjemahan|Wikisumber]]')
	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