Перейти к содержанию

Модуль:NameConvert

Википедия — эркенаб энциклопедия сайталдасан материал

Для документации этого модуля может быть создана страница Модуль:NameConvert/doc

local p = {}
--[[
  Функция преобразования строки "И.О. Фамилия" в формат "Фамилия И.О."
  
  Параметры: первый неименованный парметр - исходная строка
  noprefix -- '0' или 'no' или 'false' отключает удаление префиксов фамилий
  nolink -- '0' или 'no' или 'false' отключает удаление внутренних ссылок
]]
function p.convert( frame )
	local source_str = frame.args[1] or '';
	local name = mw.ustring.gsub( source_str, '^%[%[(.-)%]%]', '%1'); -- исключаем внутренние ссылки
	local notstripped = name == source_str; 
	local leftpart = nil;
	if not notstripped then
		local parts = mw.text.split( name, '|', true ); -- разделяем строку
		if #parts > 1 then
		  leftpart = parts[1];
		  name = parts[2];
		end
	end
	local result = mw.ustring.gsub( name, '^([^,]+%.) ?([^%.]*)', '%2 %1' ); -- инициалы в конце
	local prefix = {'[Фф]он', '[Дд]е', '[Дд]и', 'ван', '[Vv]on', '[Dd]e', '[Dd]i'}
	local noprefix = frame.args['noprefix'] or true;
	if type( noprefix ) == 'string' and (noprefix == 'false' or noprefix == 'no' or noprefix == '0') then
		noprefix = false
	end
	if noprefix	then
		for i, pref in ipairs(prefix) do
			result = mw.ustring.gsub(result, '^' .. pref .. ' ', ''); -- Убираем префиксы фамилий в начале
		end
	end
	local nolink = frame.args['nolink'] or true;
	if type( nolink ) == 'string' and (nolink == 'false' or nolink == 'no' or nolink == '0') then
		nolink = false
	end
	if nolink or notstripped then
		return result;
	elseif leftpart ~= nil then
		return '[[' .. leftpart .. '|'.. result .. ']]';
	else
		return '[[' .. result .. ']]';
	end
end

return p