Module:Class/Person




local p = {}
local mSlot = require("Module:Slot")

--[[
moved from Class/Title
Create table with names for no more than two authors
@authors table|nil - table of author pages
@return table
]]--
p.buildAuthorNameTbl = function( authors )
	if authors == nil or #authors == 0 then
		return {}
	end

	local res = {}
	res[1] = {
		["page"] = authors[1],
		["NaamGew"] = mSlot.getValueFromTemplateParam( authors[1], "Person", "NaamGew" ) or "",
		["NaamBiblio"] = mSlot.getValueFromTemplateParam( authors[1], "Person", "NaamBiblio" ) or ""
	}

	if authors[2] ~= nil then
		res[2] = {
			["page"] = authors[2],
			["NaamGew"] = mSlot.getValueFromTemplateParam( authors[2], "Person", "NaamGew" ) or "",
			["NaamBiblio"] = mSlot.getValueFromTemplateParam( authors[2], "Person", "NaamBiblio" ) or ""
		}
	end

	return res
end

--[[
originally in Class/Title - similar to method below

Format name string for no more than two authors
@authorCount int - total number of authors
@authors table - one or two authors
@return string
]]--
p.formatAuthorNameString = function( authorCount, authors, defaultStr )
	local authorNameStr = defaultStr
	
	if ( authorCount > 0 ) and type(authors[1]["NaamGew"]) == nil then
		mw.log( "Author name could not be retrieved" )
	end

	if ( authorCount == 1 ) then
		authorNameStr = authors[1]["NaamGew"]
	elseif ( authorCount == 2 ) then
		authorNameStr =  authors[1]["NaamGew"] .. " & " .. authors[2]["NaamGew"]
	elseif ( authorCount > 2 ) then
		authorNameStr =  authors[1]["NaamGew"] .. " & " ..  authors[2]["NaamGew"] .. ", e.a."
	end
	return authorNameStr
end

--[[ 
Originally in Class/Book
Accepts a comma-separated list of pages representing persons
and returns up to two names (NaamGew = first name foll. by last name )
- delimiter = &
- if more than two authors are given, the string ends with  ", e.a."
]]--
p.getAuthorNames = function( authors )
	if authors == "" or authors == nil then return "" end
	local authorTbl = mw.text.split( authors, "," )
	--mw.log( "#authorTbl", #authorTbl )
	local nameTbl = {}
	for i,v in ipairs(authorTbl) do
		-- first two authors only
		if i == 1 or i == 2 then
			local naamGew = mSlot.getValueFromTemplateData( "ws-page-props", mw.text.trim(v), "Person", "NaamGew" )
			--mw.log( "naamGew: " .. naamGew )
			table.insert( nameTbl, naamGew )
		end
	end
	local str = table.concat( nameTbl, " & ")
	if #authorTbl > 2 then str = str .. ", e.a." end
	return str
end

p.getFirstAuthorNameBiblio = function( schrijverNrs )
	if schrijverNrs == "" or schrijverNrs == nil then return "" end
	local authorsTbl = mw.text.split( schrijverNrs, "," )
	local naamBiblio = mSlot.getValueFromTemplateData( "ws-page-props", mw.text.trim(authorsTbl[1]), "Person", "NaamBiblio" )
	local res = naamBiblio
	if naamBiblio == "" then
		res = mSlot.getValueFromTemplateData( "ws-page-props", mw.text.trim(authorsTbl[1]), "Person", "NaamGew" )
	end
	return res
end

return p