Модуль:Report

Материал из Указатель частей и соединений РККА 1941-1945
Перейти к:навигация, поиск

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

local report = {}
local ref = require( 'Module:Ref' )
local redata = mw.loadData( 'Module:Redata' )
local formation

local knownBrackets = {
	['Войско Польское'] = 'Войско Польское',
	['фр.'] = 'фр.',
	['венг.'] = 'венг.',
	['чех.'] = 'чех.',
	['югосл.'] = 'югосл.',
	['рум.'] = 'рум.',
	['болг.'] = 'болг.',
	['монг.'] = 'монг.',
	['I'] = 1,
	['II'] = 2,
	['III'] = 3,
	['IV'] = 4,
	['V'] = 5,
	['VI'] = 6,
}
report.knownBrackets = knownBrackets

local buffer = {}
local  bufferCommon, bufferCommonN, bufferCommonAdd, bufferCommonForm, bufferCommonStruct, bufferCommonHq, bufferCommonGene

report.space = ' '
report.pageMode = true


function report.category( str )
	local a, brackets = string.match( str, '^([^(]+) %((.+)%)$' )
	if not a then
		a = str
		brackets = ''
	else
		local bracketsA = mw.text.split( brackets, ') (', true )
		brackets = ''
		for i = 1, #bracketsA do
			if type( knownBrackets[bracketsA[i]] ) == 'string' then
				brackets = brackets .. ' (' .. bracketsA[i] .. ')'
			end
		end
	end
	a = ( string.match( a, '^(.+)( «.+»)$' ) ) or a


	local b = string.match( a, '[гГ]в%. (.+)$' )
	if b then
		if string.find( b, 'минометн' ) then
			a = 'гв. ' .. b
		else
			a = b
		end
	else
		b = string.match( a, '[оО]тд%. (.+)$' )
		if b then
			a = b
		else
			a = ( string.match( a, '^%d[^ ]* (.+)$' ) ) or a
		end
	end
	return a .. brackets

end

function report.fuller( str )
	if redata.Full[str] then
		return redata.Full[str]
	end
	local num, a = string.match( str, '^(%d[^ ]* )(.+)$' )
	if num then
		local gv, otd
		a, gv = string.gsub( a, 'гв. ', '' )
		if gv == 1 then
			gv = 'гв. '
		else
			gv = ''
		end
		a, otd = string.gsub( a, 'отд. ', '' )
		if otd == 1 then
			otd = 'отд. '
		else
			otd = ''
		end
		if redata.Full[a] then
			return num .. otd .. gv .. redata.Full[a]
		end
	end
	return str
end

function report.shorter( str, plural )
	-- отделяем хвосты
	local brackets, quotes, numb, name, otd, gv
	local a, b
	local abbr

	local function acheck( u, plural )
		local descr = redata.Descr[u]
		if not descr then
			return nil
		end
		if plural then
			return descr[2] or descr[1]
		end
		return descr[1]
	end

	a, brackets = string.match( str, '^(.+) %((.+)%)$' )
	local owngene
	if not a then
		a = str
		brackets = ''
	else
		local bracketsA = mw.text.split( brackets, ') (', true )
		brackets = ''
--		for i = 1, #bracketsA do
--			if knownBrackets[bracketsA[i]] then
--				brackets = brackets .. ' (' .. bracketsA[i] .. ')'
--			end
--		end
		for _, bra in ipairs( bracketsA ) do
			local known = knownBrackets[bra]
			if known then
				if type( known ) == 'string' then
					if report.gene then
						if known ~= report.gene then
							error( 'Межнациональная рознь: ' .. report.gene .. ' содержит ' .. known )
						else
							owngene = known
						end
					else
						owngene = known
						brackets = brackets .. ' (' .. known .. ')'
					end
				else
					brackets = brackets .. ' (' .. bra .. ')'
				end
			end
		end
	end
	if report.gene and not owngene then
		brackets = brackets .. ' (РККА)'
	else
		report.gene = owngene
	end

	b, quotes = string.match( a, '^(.+)( «.+»)$' )
	if b then
		a = b
	else
		quotes = ''
	end

	numb, b = string.match( a, '^(%d+%-?[абв]?) (.+)$' )
	if numb then
		numb = numb .. report.space
		a = b
	end

	-- ИИИ отд. гв. ТТТ: (отд. ТТТ), (ТТТ)
	-- ИИИ отд. ТТТ: (отд. ТТТ), (ТТТ)
	-- ИИИ гв. ТТТ: (ТТТ)
	-- ИИИ ТТТ: (ИИИ ТТТ), (ТТТ)


	name, otd, gv, b = string.match( a, '^(.*)(отд%. )(гв%. )(.+)$' )
	if name then
		abbr = acheck( otd .. b, plural )
		if abbr then
			return ( numb or '' ) .. name .. gv .. abbr .. quotes .. brackets
		end
		abbr = acheck( b, plural )
		if abbr then
			return ( numb or '' ) .. name .. otd .. gv .. abbr .. quotes .. brackets
		end
		return str
	end

	name, otd, b = string.match( a, '^(.*)(отд%. )(.+)$' )
	if name then
		abbr = acheck( otd .. b, plural )
		if abbr then
			return ( numb or '' ) .. name .. abbr .. quotes .. brackets
		end
		abbr = acheck( b, plural )
		if abbr then
			return ( numb or '' ) .. name .. otd .. abbr .. quotes .. brackets
		end
		return str
	end

	name, gv, b = string.match( a, '^(.*)(гв%. )(.+)$' )
	if name then
		abbr = acheck( b, plural )
		if abbr then
			return ( numb or '' ) .. name .. gv .. abbr .. quotes .. brackets
		end
		return str
	end

	abbr = acheck( a, plural )
	if abbr then
		return ( numb or '' ) .. abbr .. quotes .. brackets
	end

	name, b = string.match( a, '^([^ ]+ )(.+)$' )
	if name then
		abbr = acheck( b, plural )
		if abbr then
			return ( numb or '' ) .. name .. abbr .. quotes .. brackets
		end
	end

	return str
end

function report.outPart( showMode, link, unit, addendum, reference, inFormation, hq )
	-- showMode = false/nil не показывать, 1 кратко, 2 полностью; string — текст, показываемый вместо названия юнита (для буферизации)
	-- link = true — превращать в ссылку (игнор при mode=false/nil)
	local res = {}
	if link and showMode then
		table.insert( res, '[[' .. unit )
		if showMode == 1 then
			table.insert( res, '|' .. report.shorter( unit ) )
		elseif type( showMode ) == 'string' then
			table.insert( res, '|' .. showMode )
		end
		table.insert( res, ']]' )
	elseif showMode == 2 then
		table.insert( res, unit )
	elseif showMode == 1 then
		table.insert( res, report.shorter( unit ) )
	end
	if reference ~= '' then
		table.insert( res, ref.use( reference, report.pageMode ) )
	end
	if type( showMode ) ~= 'string' then
		if ( hq or '' ) ~= '' then
			table.insert( res, ' (<span title="Управление без войск">&empty;</span>)' )
		end
		if addendum ~= '' then
			table.insert( res, ' <i>(' .. addendum .. ')</i>' )
		end
		if inFormation ~= '' then
			formation = formation or mw.loadData( 'Module:Formation' )
			table.insert( res, ' — <i>на ' .. formation.FUP[tonumber( inFormation )] .. '</i>')
		end
	end
	return table.concat( res )
end

function report.outEl( tab, n, showMode, link, compact )
	local res = ''
	local unit = tab['id'..n]
	if unit ~= '' then
		if unit == 'Действующая армия' then
			return nil
		elseif unit == 'Военные округа и недействующие фронты' then
			if string.match( tab['id'..(n -1)], 'военный округ$' ) then
				return nil
			else
				return '<i>(недействующие войска)</i>'
			end
		elseif unit == 'Войска ПВО на территории военных округов и недействующих фронтов' then
			if string.match( tab['id'..(n -1)], 'фронт — войска ПВО$' ) then
				res = '<i>(недействующий фронт)</i> / '
			end
			unit = 'Войска ПВО территории страны'
		elseif unit == 'Войска ПВО, прикрывавшие объекты действующей армии' then
			if string.match( tab['id'..(n -1)], 'округ — войска ПВО$' ) then
				res = '<i>(Действующая армия)</i> / '
			end
			unit = 'Войска ПВО территории страны'
		end
		if compact then
			return report.toBuffer( tab, n )
		end
		res = res .. report.outPart( showMode, link, unit, tab['add'..n], tab['ref'..n], tab['form'..n], tab['hq'..n] )
		if tab['s_id'..n] ~= '' then
			res = res .. ' [<i></i>' .. report.outPart( showMode, true, tab['s_id'..n], tab['s_add'..n], tab['s_ref'..n], '', '' ) .. ']'
		end
		return res
	end
end

function report.toBuffer( tab, n )
	local unit = tab['id'..n]
	local x, u = string.match( unit, '^([^ ]+) (.+)$' )
	local res
	if not x then
		x = unit
		u = ''
	end
--	assert( x, unit )
	if bufferCommon and
	( u ~= bufferCommon or tab['add'..n] ~= bufferCommonAdd
		or tab['form'..n] ~= bufferCommonForm  or tab['s_id'..n] ~= bufferCommonStruct
		or bufferCommonGene ~= report.gene or tab['hq'..n] ~= bufferCommonHq ) then
		res = report.popBuffer()
	end
	if not bufferCommon then
--		bufferTab = tab
		bufferCommon = u
		bufferCommonAdd = tab['add'..n]
		bufferCommonForm = tab['form'..n]
		bufferCommonStruct = tab['s_id'..n]
		bufferCommonHq = tab['hq'..n]
		bufferCommonN = n
		bufferCommonGene = report.gene
	end
	table.insert( buffer, { n, x, tab } )
	return res
end

function report.popBuffer()
	if bufferCommon then
		local savedGene = report.gene
		report.gene = bufferCommonGene
		if #buffer == 1 then
			local n, utab = buffer[1][1], buffer[1][3]
			buffer = {}
			bufferCommon = nil
			local res = report.outEl( utab, n, 1, true )
			report.gene = savedGene
			return res
		end
		local res = {}
		for _, el in ipairs( buffer ) do
			local n, unumb, utab = el[1], el[2], el[3]
			table.insert( res, report.outPart( unumb, true, utab['id'..n], bufferCommonAdd, utab['ref'..n], bufferCommonForm, bufferCommonHq ) )
			-- в реальности add, form, hq тут игнорируются
		end
		res = { table.concat( res, ', ' ) .. report.space .. report.shorter( bufferCommon, true ) }
		if ( bufferCommonHq or '' ) ~= '' then
			table.insert( res, ' (<span title="Управление без войск">&empty;</span>)' )
		end
		if bufferCommonAdd ~= '' then
			table.insert( res, '<i>(' .. bufferCommonAdd .. ')</i>' )
		end
		if bufferCommonForm ~= '' then
			formation = formation or mw.loadData( 'Module:Formation' )
			table.insert( res, '— <i>на ' .. formation.FUP[tonumber( bufferCommonForm )] .. '</i>' )
		end
		local btab = buffer[1][3]
		if bufferCommonStruct ~= '' then
			table.insert( res, '[<i></i>' .. report.outPart( 1, true, bufferCommonStruct, btab['s_add'..bufferCommonN], btab['s_ref'..bufferCommonN], '' ) .. ']' )
		end
		buffer = {}
		bufferCommon = nil
		report.gene = savedGene
		return table.concat( res, ' ' )
	else
		return nil
	end
end


function report.printDate( str )
	if ( str or '' ) ~= '' then
		return '<span class=date-field>'
		.. string.sub( str, 9, 10 ) .. '.' .. string.sub( str, 6, 7 ) .. '.'
		.. string.sub( str, 1, 4 ) .. '</span>'
	end
	return '<span class=date-field>&nbsp;</span>'
end

function report.printDateLink( page, bookpage, text )
	local basepage, date, part
	if ( bookpage or '' ) == '' then
		bookpage = ''
	else
		bookpage = '#p' .. bookpage
	end
	if page then
		basepage, date, part = string.match( page, '^(.+ (%d%d%.%d%d%.%d%d%d%d))/(%d)$' )
		assert( basepage )
		date = '[[' .. basepage .. bookpage .. '|' .. ( text or date ) .. ']]'
	else
		date = '&nbsp;'
		part = ''
	end
	return '<span class="oobdate date-field" data-part="' .. part .. '">'
		.. date .. '</span>'
end

return report