Модуль:Фамили, ЦӀе, Ден цӀе

ХӀокху модулах лаьцна хааман Модуль:Фамили, ЦӀе, Ден цӀе/doc агӀо кхолла мега

local get_args = require('Модуль:Arguments').getArgs
local mHatnote = require('Модуль:Hatnote')
local current_title_obj = mw.title.getCurrentTitle()

local p = {}
local hatnote_args = {
	hide_disambig = true,
	dot = true,
	empty_list_message = 'Не найдено ни одной страницы разрешения неоднозначности',
	preview_error = true
}
local templates = {
	'[фФ]амилихойн исписка',
	'[сС]писок т[ёе]зок',
	'[сС]писок полных т[ёе]зок',
	'[сС]писок однофамильцев-т[её]зок',
	'[сС]писок т[её]зок-однофамильцев'
}
local disambig_box_names = {
	'[чЧ]олхалла',
	'[мМ]ногозначность',
	'[dD]isambig'
}
local disambig_box_params = {
	'[фФ]амилии',
	'[оО]днофамильцы',
	'[тТ][ёе]зки',
	'[оО]днофамильцы-т[ёе]зки',
	'[пП]олные т[ёе]зки'
}
local tracking_categories = {
	bad_link = 'Википеди:Страницы с шаблоном ФИО со ссылкой на общие неоднозначности',
	no_links = 'Википеди:ФЦӀД кепаца хьажоргаш йоцу агӀонаш',
	disambig_not_found = 'Википеди:Страницы с модулем ФИО со ссылкой на другую статью'
}

function is_surname_disambig(title)
	local title_obj = mw.title.new(title)
	
	if not title_obj.exists or mw.title.equals(title_obj, current_title_obj) then
		return false 
	end
	
	if title_obj.isRedirect then
		title_obj = title_obj.redirectTarget
		if not title_obj.exists or mw.title.equals(title_obj, current_title_obj) then
			return false
		end
	end

	local page_content = title_obj:getContent():gsub('<[^>]*>', '')
	for i, template in ipairs(templates) do
		if mw.ustring.match(page_content, '{{' .. template) then
			return true
		end
	end
	for i, name in ipairs(disambig_box_names) do
		for j, param in ipairs(disambig_box_params) do
			if mw.ustring.match(page_content, string.format('{{%s[^}]*%s', name, param)) then
				return true
			end
		end
	end
    return false
end
    
function get_items(_title)
	local title = mHatnote.remove_precision{_title}
	local surname = title:match('^[^,]+')
	local surname_with_name = title:match('^[^,]+,%s[^%s]+')
	local full_name = title:match('^[^,]+,%s[^%s]+%s[^%s]+')

	return {surname, surname_with_name, full_name}
end

function find_disambig(title, categories, check_nil, nocat)
	local disambig_page
	local without = mHatnote.remove_precision{title}
	local with = without .. ' (маьӀнаш)'
	
	if is_surname_disambig(title) then
		disambig_page = title
	elseif without ~= title and is_surname_disambig(without) then
		disambig_page = without
	elseif with ~= title and is_surname_disambig(with) then
		disambig_page = with
		
	elseif title ~= with and mHatnote.is_disambig{title} then
		categories:add('bad_link', nocat)
		disambig_page = title
	elseif mw.title.new(with).exists then
		categories:add('bad_link', nocat)
		disambig_page = with
	elseif without ~= title and mHatnote.is_disambig{without} then
		categories:add('bad_link', nocat)
		disambig_page = without
	end
	
	if not disambig_page and check_nil then
		categories:add('disambig_not_found', nocat)
	end
	
	return disambig_page, without
end

p['Фамили, ЦӀе, Ден цӀе'] = function (frame)
	local args = get_args(frame)
	local title = args.title or args[1] or current_title_obj.text
	local categories = mHatnote.define_categories(tracking_categories)
	local nocat = args.nocat
	
	hatnote_args.prefix = 'Википедехь иштта фамили йолуш нахах лаьцна йаззамаш бу, хьж.'
	hatnote_args.list_sep = '; '
	hatnote_args.natural_join = false
	hatnote_args.nocat = nocat

	local lNum = 0
	for i, v in ipairs(get_items(title)) do
		link, label = find_disambig(v, categories, false, nocat)
		if link and link ~= title and link ~= current_title_obj.text then
			lNum = lNum + 1
			hatnote_args[lNum], hatnote_args['l' .. lNum] = link, label
		end
	end
	
	return mHatnote.main(hatnote_args, tracking_categories) .. categories
end

function template_by_type(frame, disambig_type, num)
	return function (frame)
		local args = get_args(frame)
		local categories = mHatnote.define_categories(tracking_categories)
		local nocat = args.nocat
		local prefixes = mw.loadData('Модуль:Кхин маьӀнаш/префиксаш')
		local prefix = prefixes[disambig_type]
		if type(prefix) == 'table' then
			if args[2] then
				hatnote_args.prefix = prefix.plural
			else
				hatnote_args.prefix = prefix.singular
			end
		else
			hatnote_args.prefix = prefix
		end
		hatnote_args.nocat = nocat
		
		if not args[1] then
			local link = get_items(current_title_obj.text)[num]
			local found_disambig, auto_label = find_disambig(link, categories, true, nocat)
			
			hatnote_args[1] = found_disambig or link
			hatnote_args['l1'] = args['l1'] or auto_label
		else
			local i = 1
			while args[i] do
				local link, label = mHatnote.parse_link{args[i]}
				if link then
					local found_disambig, auto_label = find_disambig(link, categories, true, nocat)
					
					hatnote_args[i] = found_disambig or link
					hatnote_args['l' .. i] = label or args['l' .. i] or auto_label
				else
					hatnote_args[i] = args[i]
				end
				
				i = i + 1
			end
		end
		
		return mHatnote.main(hatnote_args, tracking_categories) .. categories
	end
end

p['однофамильцы'] = template_by_type(frame, 'фамили', 1)
p['однофамильцы-тёзки'] = template_by_type(frame, 'цӀе а, фамили а', 2)
p['полные тёзки'] = template_by_type(frame, 'Фамили, ЦӀе, Ден цӀе', 3)

return p