Modul:Header/attribution

Dokuméntasi antuk modul puniki prasida kakardi ring Modul:Header/attribution/doc

require('strict')

local p = {}

local yesno = require('Module:Yesno')
local tableTools = require('Module:TableTools')
local contrib_types = require('Module:Header/attribution data')

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

	-- Temporary check for illustrator in subpages.
	-- TODO: remove once
	--       [[Special:PermanentLink/14324127#Replace illustrator header parameter with section illustrator in subpages of works]]
	--       has been addressed.
	local cat = ''
	if param_name == 'illustrator' and mw.title.getCurrentTitle().isSubpage then
		cat = '[[' .. 'Category:' .. 'Subpages using illustrator parameter' .. ']]'
	end

	local s = construct_attribution_span({span_param_name = span_param_name, prefix = prefix, atext = atext})

	return s .. cat
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