Модуль:Телефон

Материал из Справочника наблюдателя
Перейти к:навигация, поиск

(i)      Описание модуля[]

Этот модуль является телом шаблона {{Т}} .

Symbol redirect vote.svg   Подробнее см. отдельную статью Шаблон:Т/doc

local p = {}

local knowCode = {'(9%d%d)(%d+)', '(495)(%d+)', '(499)(%d+)'}

function redPhone (num, err)
	return '<span style="color:red" title="' .. err ..  '">' .. num .. '</span>'
end


function phone (number, defaultCode, typh, strictFormat)

	local numberIn = mw.text.trim (number)
	local numberOut = mw.ustring.gsub (numberIn, '[%-− ()+]', '')
	local prefixIn = ''
	if #numberOut == 11 then
		prefixIn = mw.ustring.sub (numberOut, 1, 1)
		if prefixIn == '7' or prefixIn == '8' then
			numberOut = mw.ustring.sub (numberOut, 2)
		else
			return redPhone (numberIn, 'В номере больше 10 цифр')
		end
	end
	if #numberOut == 10 then
		if mw.ustring.sub (numberOut, 1, #defaultCode) == code then
			code = defaultCode
		else
			for i, val in ipairs (knowCode) do
				code = mw.ustring.match ( numberOut, '^' .. val .. '$' )
				if code then
					break
				end
			end
			if not code then
				code = mw.ustring.match ( numberIn, '^%+?[ ]*' .. prefixIn .. '[ ]*%(([%-−%d ]+)%)' )
				if code then
					code = mw.ustring.gsub (code, '[%-− ]', '')
				end
			end
			if not code then
				code = mw.ustring.match ( numberIn, '^%+?[ ]*' .. prefixIn .. '[%- ]*(%d+)[%- ]' )
			end
			if not code then
				return redPhone (numberIn, 'Не определен код города')
			end	
		end
	elseif #numberOut + #defaultCode == 10 then
		code, numberOut = defaultCode, defaultCode .. numberOut
	elseif #numberOut == 7 then
		code, numberOut = '495', '495' .. numberOut
	end
	if not code then
		return redPhone (numberIn, 'Не определен код ABC/DEF')
	else
		local res
		if #code == 3 then
			res = mw.ustring.sub (numberOut,4,6) .. ' ' .. mw.ustring.sub (numberOut,7,8) .. ' ' .. mw.ustring.sub (numberOut,9,10)
		elseif #code == 4 then
			res = mw.ustring.sub (numberOut,5,6) .. ' ' .. mw.ustring.sub (numberOut,7,8) .. ' ' .. mw.ustring.sub (numberOut,9,10)
		elseif #code == 5 then
			res = mw.ustring.sub (numberOut,6,6) .. ' ' .. mw.ustring.sub (numberOut,7,8) .. ' ' .. mw.ustring.sub (numberOut,9,10)
		elseif #code == 6 then
			res = mw.ustring.sub (numberOut,7,8) .. ' ' .. mw.ustring.sub (numberOut,9,10)
			code = mw.ustring.sub (code,1,3) .. ' ' .. mw.ustring.sub (code,4,6) 
		end
		if strict then
			return ' (' .. code .. ') ' .. res
		else
			return '<span style="white-space:nowrap" class=plainlinks>[' .. typh .. ':+7' .. numberOut .. ' (' .. code .. ') ' .. res .. ']</span>'
		end
	end
end

function p.Phones (frame)
	local args = frame:getParent().args
	local defaultCode = args.code
	if not defaultCode then
		defaultCode = frame:callParserFunction( '#var', 'PhCode' ) or ''
	end

	local icon = args.icon
	local typh = 'tel'
	if icon then
		local x = mw.ustring.lower (mw.ustring.sub (icon, 1, 1))
		if x == 't' or x == 'p' or x == 'т' then
			icon = '[[Файл:Телефон.svg|18x16px|text-bottom|link=|Телефон]]&nbsp;'
		elseif x == 'f' or x == 'ф' then
			icon = '[[Файл:Icon 8017.svg|18x16px|text-bottom|link=|Факс]]&nbsp;'
		elseif x == 's' or x == 'с' then
			icon = '[[Файл:Sms.png|18x16px|text-bottom|link=|Номер для смс]]&nbsp;'
			typh = 'sms'
		end
	else
		icon = ''
	end

--	do 
--		return mw.dumpObject( args )
--	end
	
	local xx = mw.text.split (args[1] or '', ',')
	for i,v in ipairs (xx) do
		xx [i] = phone (v, defaultCode, typh, false)
	end
	local wh = args[2]
	if wh then
		wh = mw.text.trim (wh)
		if mw.ustring.sub (wh,1,1) ~= '(' then
			wh = '(' .. wh .. ')'
		end
		wh = "&nbsp;&nbsp;''" .. wh .. "''"
	else
		wh = ''
	end
	
	return icon .. table.concat (xx, '&nbsp; ') .. wh
end

	

return p