Модуль:Не переведено

Версия от 20:04, 12 декабря 2017; t>TenBaseT (Защитил Модуль:Не переведено: критический шаблон или модуль ([Редактирование=только администраторы] (бессрочно) [Переименование=только…)

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

local getArgs = require('Module:Arguments').getArgs
local p = {}

local function is_empty(param)
	return param == nil or param == ''
end

local function wikilink(lang, title, text)
	if is_empty(text) then
		text = title
	end
	if lang ~= 'ru' then
		title = string.format(':%s:%s', lang, title)
	end
	return string.format('[[%s|%s]]', title, text)
end

local function italic(s)
	return tostring(
		mw.html.create('span')
		:css('font-style', 'italic')
		:wikitext(s)
	)
end

function p.main(frame)
	local yesno = require('Module:Yesno')
	local args = getArgs(frame)
	local languages = mw.loadData('Module:Languages/data')
	local error = require('Module:Error').error

	local leave = yesno(args['l'])
	
	local categories = ''
	local nocat = yesno(args['nocat'])
	local allow_cat = mw.title.getCurrentTitle().namespace == 0 and not nocat
	local orphan_categories = {
		['error'] = '[[Категория:Википедия:Статьи с некорректно заполненным шаблоном Не переведено]]',
		['outdated'] = '[[Категория:Википедия:Статьи с неактуальным шаблоном Не переведено]]',
		['redirect'] = '[[Категория:Википедия:Запросы на замену перенаправлений переводами]]'
	}
	
	if is_empty(args[1]) and not args['есть']:match('^:[a-z-]+:') then
		if allow_cat then
			categories = categories .. orphan_categories['error']
		end
		return error{'не указано название статьи'} .. categories
	end

	if not is_empty(args['есть']) or args[1]:match('^:[a-z-]+:') then
		local temp
		if is_empty(args['есть']) then
			temp = args[1]
		else
			temp = args['есть']
		end
		args[1] = args['надо'] or args['нужно'] or args[2] or ''
		args[2] = args['текст'] or args[3]
		args[3] = mw.ustring.sub(temp, 2, mw.ustring.find(temp, ':', 2, true) - 1)
		args[4] = mw.ustring.sub(temp, mw.ustring.find(temp, ':', 2, true) + 1)
		if is_empty(args[1]) then
			args[1] = args[4]
		end
	end

	local title = args[1]
	local text = args[2]
	local exists = mw.title.new(title).exists and not leave
	local ru_link = wikilink('ru', title, text)
	if exists and not mw.isSubsting() then
		ru_link = tostring(
			mw.html.create('span')
			:css('background', '#FFFF00')
			:wikitext(ru_link)
		)
	end
	if leave and mw.title.new(title).isRedirect and allow_cat then
		categories = categories .. orphan_categories['redirect']
	end
	
	local post_text_list = {}
	local addition = args['a']
	local show_original = yesno(args['o']) or addition ~= nil
	local wikidata_link = ''
	local i, j = 4, 1
	while args[i] or args[i - 1] or i <= 4 do
		
		local lang = args[i - 1]
		local iw_title = args[i]
		local iw_text = args[i + 1]
		if is_empty(lang) then
			lang = 'en'
		end
		if is_empty(iw_title) then
			iw_title = title
		end
		if is_empty(iw_text) then
			iw_text = iw_title
		end
		
		local lang_text
		local lang_title
		local lang_link
		if lang == 'd' then
			iw_text = 'd'
		elseif languages[lang] == nil then
			if allow_cat then
				categories = categories .. orphan_categories['error']
			end
			return error{'некорректный ISO-код «' .. lang .. '»'} .. categories
		else
			lang_text = languages[lang][1]
			lang_title = languages[lang][2]
			lang_link = wikilink('ru', lang_title, lang_text)
		end

		local iw_link
		if show_original or lang == 'd' then
			iw_link = wikilink(lang, iw_title, iw_text)
		else
			iw_link = wikilink(lang, iw_title, lang_text)
		end
		
		if lang == 'd' then
			if not ( mw.isSubsting() or exists ) then
				wikidata_link = iw_link
			end
		elseif mw.isSubsting() or exists then	
			if show_original then
				post_text_list[j] = string.format('%s&nbsp;%s', lang_link, italic(iw_text))
			end
		elseif show_original then
			post_text_list[j] = string.format('%s&nbsp;%s', lang_link, italic(iw_link))
		else
			post_text_list[j] = iw_link
		end
		
		if show_original and lang ~= 'd' then
			i = i + 3
		else
			i = i + 2
		end
		if lang ~= 'd' then
			j = j + 1
		end
	end
		
	local post_text = ''
	if not is_empty(addition) then
		addition = '; ' .. addition
	else 
		addition = ''
	end
	if not is_empty(wikidata_link) and next(post_text_list) ~= nil then
		wikidata_link = wikidata_link .. '; '
	end
	if next(post_text_list) ~= nil or not is_empty(wikidata_link) then
		post_text = string.format(' (%s%s%s)', wikidata_link, table.concat(post_text_list, ', '), addition)
		if not show_original then
			post_text = tostring(
				mw.html.create('span')
				:addClass('noprint')
				:css('font-size', '85%')
				:wikitext(post_text)
			)
		end
	end
	
	local exist_message = ''
	if exists then
		if allow_cat then
			categories = categories .. orphan_categories['outdated']
		end
		exist_message = tostring(
			mw.html.create('sup')
			:addClass('noprint')
			:wikitext('[' .. italic('[[Шаблон:Не переведено#Действия после появления перевода|убрать шаблон]]') .. ']')
		)
	end
	
	local result
	result = ru_link .. post_text
	if not mw.isSubsting() then
		result = result .. exist_message .. categories
	end
	
	return result
end

return p