Документацію для цього модуля можна створити у Модуль:Author/документація

local Error = require('Модуль:Error');
local Sister = require('Модуль:Plain sister');
local getArgs = require('Модуль:Arguments').getArgs
local Wikidata = require('Модуль:Wikidata')

local this = {};

local migrationNeeded = false;
local birthNeeded = false;

local capitalize = function(str)
    if not str then
        return str
    end
    return mw.ustring.upper(mw.ustring.sub(str, 1, 1))..mw.ustring.sub(str, 2)
end

this.capitalize = capitalize

local getLocalValue = function(args, argNames, defaultName)
    for key, argName in pairs(argNames) do
        local value = args[argName]
        if value then
            return value
        end
    end
    return defaultName
end

-- process empty arguments as not specified
local function filterEmpty(text, defaultValue)
    if text and mw.ustring.len(text) > 0 then
        return text;
    end
    return defaultValue;
end

-- handle boolean argument (yes/no)
local function toBoolean(value, defaultValue)
    if defaultValue then
        -- default value is true
        value = mw.ustring.lower(filterEmpty(value, 'yes'));
        return value == 'yes' or value == 'так';
    else
        -- default value is false
        value = mw.ustring.lower(filterEmpty(value, 'no'));
        return not (value == 'no' or value == 'ні');
    end
end

local getValue = function(args, localArgNames, wikidataValue)
    local localValue = getLocalValue(args, localArgNames, nil)
    if localValue then
        migrationNeeded = true
    else
        localValue = wikidataValue
    end
    return localValue
end

local function parseDate(text) 
    local BC = mw.ustring.match(text, '^%s*%-') or mw.ustring.match(text, 'до н%.%s?е%.') or string.match(text, 'BCE$');
    local date = mw.ustring.match(text, '%d+');
    if not date then 
        return nil;
    end
    
    if BC then
        return date, ' до н. е.'
    else
        return date, '';
    end;
end

function this.main(frame)
    -- parse arguments
    local args = getArgs(frame);
    
    local firstname = getValue(args, {"ім'я", 'firstname'}, Wikidata.getItemLabel('P735')) or '';
    local lastname = getValue(args, {'прізвище', 'lastname'}, Wikidata.getItemLabel('P734')) or '';
    local fullname = firstname..(firstname and (firstname ~= '') and ' ' or '')..lastname;
    
    if fullname == '' then
    	fullname = mw.wikibase.label(mw.wikibase.getEntityIdForCurrentPage());
   	end
   	
   	fullname = getLocalValue(args, {"перевизначити_ім'я", 'override_name'}, fullname);
    
    -- get the initial
    local initial = getLocalValue(args, {'ініціал', 'last_initial'}, nil);
    if not initial then 
        if mw.ustring.len(lastname)>0 then
            initial = mw.ustring.sub(lastname, 1, 1)
        elseif mw.ustring.len(firstname)>0 then
            initial = mw.ustring.sub(firstname, 1, 1)
        end
    end
    if initial then
        initial = capitalize(initial);
    end;

    -- get dates
    local rawBirth = getValue(args, {'рік_народження', 'birthyear'}, Wikidata.getDateValue({'P569', 'ye'})) or '?';
    local rawDeath = getValue(args, {'рік_смерті', 'deathyear'}, Wikidata.getDateValue({'P570', 'ye'})) or '';
    local birth, birthSuffix = parseDate(rawBirth);
    local death, deathSuffix = parseDate(rawDeath);
    
    if not birth then
    	birthNeeded = true;
    end
    
    -- other params
    local description = capitalize(getValue(args, {'опис', 'description'}, Wikidata.getDescription()) or '');
    local pseudonym = getValue(args, {'псевдонім', 'pseudonym'}, Wikidata.getRawValues('P742')) or '';
    local defaultsort = getLocalValue(args, {'defaultsort'}, nil);
    local image = getValue(args, {'зображення', 'image'}, Wikidata.getRawValue('P18'));
    local signature = getValue(args, {'підпис', 'signature'}, Wikidata.getRawValue('P109'));
    local image_caption = getLocalValue(args, {'підпис_зображення', 'image_caption'}, nil) or fullname;
    
    if toBoolean(args['без_зображення'], false) or toBoolean(args['no_image'], false) then
    	image = nil;
    	signature = nil;
    end
    
    -- do the main job
    
    -- header
    local result = {'{| class="authortemplate"\n|-\n| class="gen_header_backlink" |\n| class="gen_header_title" |'.."'''"};
    result[#result + 1] = fullname or ' ';
    result[#result + 1] = "'''<br />";
    if birth or death then
	    result[#result + 1] = (birth and birth or '')..(birthSuffix and birthSuffix or '');
	    result[#result + 1] = '—';
	    result[#result + 1] = (death and death or '')..(deathSuffix and deathSuffix or '');
	else
		result[#result + 1] = '&nbsp;';
	end
    result[#result + 1] = '\n| class="gen_header_forelink" |\n|}\n'
    
    -- notes
    result[#result + 1] = '{| class="author_notes"\n|-\n|';
    result[#result + 1] = Sister.main(frame);
    result[#result + 1] = '\n'
    result[#result + 1] = description;
    result[#result + 1] = '<br />'
    result[#result + 1] = pseudonym or '';
    result[#result + 1] = '\n|}'
    
    -- image
    if image then
        result[#result + 1] = '<div class="infobox" style="border:1px solid gray; float:right; padding:3px; margin-left:1em;">';
        result[#result + 1] = '[[Файл:';
        result[#result + 1] = image;
--        result[#result + 1] = '|thumb|';
--        result[#result + 1] = frame:expandTemplate{ title = 'Center', args = {image_caption} };
        result[#result + 1] = '|безрамки|центр';
        result[#result + 1] = ']]';
	    if signature then
	        result[#result + 1] = '[[Файл:';
    	    result[#result + 1] = signature;
        	result[#result + 1] = '|безрамки|центр';
	        result[#result + 1] = ']]';
	    end
        result[#result + 1] = '</div>';
    end
    
    -- DEFAULTSORT and categories
    result[#result + 1] = '{{DEFAULTSORT:';
    if defaultsort then
        result[#result + 1] = defaultsort;
    else
        if lastname and lastname ~= '' then
            result[#result + 1] = lastname;
            result[#result + 1] = ', ';
        end
        result[#result + 1] = firstname;
    end
    result[#result + 1] = '}}';
    result[#result + 1] = '[[Категорія:Автори]]';
    result[#result + 1] = '[[Категорія:Автори';
    if initial then
        result[#result + 1] = '-';
        result[#result + 1] = initial;
    else
        result[#result + 1] = ' без ініціалів';
    end
    result[#result + 1] = ']]';
    if birth then
        result[#result + 1] = '[[Категорія:Народжені у ';
        result[#result + 1] = birth;
        result[#result + 1] = '-ому'
        if birthSuffix then
        	result[#result + 1] = birthSuffix;
    	end
        result[#result + 1] = ']]';
    end
    if death then
        result[#result + 1] = '[[Категорія:Померлі у ';
        result[#result + 1] = death;
        result[#result + 1] = '-ому'
        if deathSuffix then
        	result[#result + 1] = deathSuffix;
    	end
        result[#result + 1] = ']]';
    end
    
    if migrationNeeded or birthNeeded then
        result[#result + 1] = '[[Категорія:Дані до перенесення до Вікіданих]]';
    end
    
    if not Wikidata.getId() then
        result[#result + 1] = "[[Категорія:Сторінки авторів без прив'язки до Вікіданих]]";
    end
    
	--
	-- Does Author present in calendar?
	--
    local calendar = getLocalValue(args, {'календар'}, ' ');
	if calendar ~= 'ні' then

		local objTitle, objCal, cTxt, nPos, lNotFound
		local aMonth, iMnth, cMnth
		aMonth = {'січень','лютий','березень','квітень','травень','червень','липень','серпень','вересень','жовтень','листопад','грудень'}
	
		objTitle = mw.title.getCurrentTitle()
		name_page = objTitle.prefixedText
		lNotFound = true
		for iMnth=1, 12 do
			cMnth = aMonth[iMnth]
        	str =  'Календар/' .. cMnth
			objCal = mw.title.makeTitle('Вікіджерела', str)
			cTxt = objCal:getContent()
	
			nPos = mw.ustring.find(cTxt, name_page, 1, true)
			if nPos ~= nil then
				lNotFound = false
				result[#result + 1] = '[[Категорія:Календар на ' .. cMnth .. ']]'
			end
		end
		if lNotFound then
			result[#result + 1] = '[[Категорія:Відсутність автора в календарі]]'
		end
	end
--   
    return frame:preprocess(table.concat(result));
end

--
-- Return a category for an Index page
--
function this.cat4index(frame)
	local name = frame.args[1]
	local type = tonumber(frame.args[2])
	local str, pos1, pos2, shift, lfirst
    local cat = ''
    local cat_name, obj_title
	
   	if type == 1 then
   		ctype = 'автор'
   	elseif type == 2 then
   		ctype = 'перекладач'
   	elseif type == 3 then
   		ctype = 'редактор'
   	else
   		ctype = 'автор'
   	end
	
	lfirst = true
	shift = 1
	
	repeat	
		pos1 = mw.ustring.find( name, "Автор:", shift)
		pos2 = mw.ustring.find( name, "|", shift+1)

		if pos1 == nil or pos2 == nil then
			if lfirst then
				str = mw.ustring.gsub(name, '|', '')
				str = mw.ustring.gsub(str, '%]', '')
				str = mw.ustring.gsub(str, '%[', '')
			else
				break
			end
		else
			str = mw.ustring.sub(name, pos1+6, pos2-1)

			lfirst = false
			shift = pos2
		end

		obj_title = mw.title.makeTitle('Автор', str)
		if obj_title == nil or obj_title.id == 0 then
			cat_name = ''
		else
			cat_name = '[[Категорія:Індекси видань, ' .. ctype .. ' яких – ' .. str .. ']]'
		end
		
		cat = cat .. cat_name 
	until pos1 == nil or pos2 == nil

	return frame:preprocess(cat)
end
--
return this;