Модуль:Military

Материал из Кадровый состав НКВД 1935-1939
Перейти к навигации Перейти к поиску

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

local p = {}

function p.loop( frame )
	local args = frame:getParent().args
	return p._loop( args )
end
function p.Loop( frame )
	local args = frame.args
	return p._loop( args )
end
function p._loop( args )
	local start, finish, text, comment = tonumber( args[2] ), tonumber( args[3] )
	if not finish then
		start = mw.text.trim( args[2] )
		text = args['текст']
		comment = args['подсказка'] 
	end
	
	local prefix, postfix = mw.text.trim( args[1] or '' ), mw.text.trim( args[4] or '' )
	if prefix ~= '' then
		prefix = prefix .. ' '
	end
	if postfix ~= '' then
		postfix = ' ' .. postfix
	end
	local prefixURL, postfixURL = mw.uri.encode( prefix ), mw.uri.encode( postfix )
	local mode = mw.text.trim( args['формат'] or 'номерМС' )
	local class = mw.text.trim( args['class'] or '' )
	if class == '' then
		class = 'military-main'
	end
	local attr = mw.text.trim( args['attr'] or '' )
	
	local tablo = mw.loadData( 'Module:CatNav/segment' )
	function processUnit( unit )
		if mode == 'номерМС' then
			local label = text or unit
			if comment then
				label = '<abbr title="' .. comment .. '">' .. label .. '</abbr>'
			end
			if unit == '' then
				return '<td class="military-number ' .. class .. '" ' .. attr ..'>' .. label .. '</td>'
			end
			return '<td class="military-number ' .. class .. '" ' .. attr ..'>[' 
				.. tostring( mw.uri.fullUrl( 'НКВД:Выборка', { class = 'segment', value = prefix .. unit .. postfix } ) )
				.. ' <span class=military-number>' .. label .. '</span>]</td>\n'
		elseif mode == 'числоМС' then
			if unit == '' then
				return '<td class="military-quantity ' .. class .. '" ' .. attr ..'>' .. ( text or '' ) .. '</td>'
			end
			local z = tablo[prefix .. unit .. postfix]
			if z then 
				mw.ext.luaglobal.add( 'CatClassValue', prefix .. unit .. postfix )
			end
			return '<td class="military-quantity ' .. class .. '" ' .. attr ..'>' -- '>['  
--				.. tostring( mw.uri.fullUrl( 'НКВД:Выборка', { class = 'segment', value = prefix .. unit .. postfix } ) )
				.. ' <span class="military-quantity>' .. ( z or '<span style="color:red">0</span>' ) .. '</span></td>\n' -- '</span>]</td>\n'
		end
	end	
	local out = ''
	
	if finish then
		for unit = start, finish do
			out = out .. processUnit( unit )
		end
	else
		out = processUnit( start )
	end
	
	return out
end
--[===[
prev | start | stop | post
могут использоваться при единственном элементе (если отсутствует stop)
* текст = показываемый текст
* подсказка = подсказка к тексту 
цвет = 1 (по умолчанию), 2, 3, 9
формат = стандарт (по умолчанию), заголовок (узкая полоска), раздел (стандартная высота, сдвиг вправо)
шрифт = стандарт (по умолчанию), мелко (80%), хх%
размер = число колонок
--]===]

local function parseList( list )
	local out = {}
	for x in mw.text.gsplit( list, ',', true ) do
		local start,finish = string.match(x,'^%s*(%d+)%s*%-%s*(%d+)%s*$')
		if start then
			start, finish = tonumber( start ), tonumber( finish )
			for u = start, finish do
				out[#out+1] = u
			end
		else
			x = mw.text.trim( x )
			if x ~= '' then
				out[#out+1] = x
			end
		end
	end
	return out
end

				
			
	


function p.Unit( frame )
	local args = frame.args
	return p._unit( args )
end

function p._unit( args )
	local modeClasses = {
		['стандарт'] = '',
		['заголовок'] = 'mil-header',
		['раздел'] = 'mil-chapter',
	}
	local modeDivCSS = {
		['стандарт'] = '',
		['заголовок'] = 'height: auto;',
		['раздел'] = '',
	}
		
	local tablo = mw.loadData( 'Module:CatNav/segment' )
	local prefix, units, postfix 
		= mw.text.trim( args[1] or '' ), parseList( args[2] or '' ), mw.text.trim( args[3] or '' )
	local text, comment
	if #units <= 1 then
		text = args['текст']
		comment = args['подсказка'] 
	end
	
	if prefix ~= '' then
		prefix = prefix .. ' '
	end
	if postfix ~= '' then
		postfix = ' ' .. postfix
	end
	
	local width = ( args['размер'] or 1 ) * 29 - 1
	
	local mode = mw.text.trim( args['формат'] or '' )
	local modeCSSadd = modeDivCSS[mode] or ''
	mode = modeClasses[mode] or 'mil-error'
	
	local color = tonumber( args['цвет'] or 1 ) or 1
	
	local fontSize = args['шрифт'] or '100'
	if mw.ustring.sub( fontSize, 1, 3 ) == 'мел' then
		fontSize = '80'
	elseif mw.ustring.sub( fontSize, -1, -1 ) == '%' then
		fontSize =  mw.ustring.sub( fontSize, 1, -2 )
	end
	
	local class = args.class or ''
		
	local function processUnit( unit )
		local label = text or unit
		if comment then
			label = '<abbr title="' .. comment .. '">' .. label .. '</abbr>'
		end
		if unit == '' then
			return '<div class="mil-div mil-c' .. color .. ' ' .. class
				.. '" style="width: ' .. width .. 'px; ' .. modeCSSadd
				.. (args.divcss or '') .. '"><div class="' .. mode 
				.. '" style="width: ' .. width .. 'px; font-size: ' .. fontSize .. '%; ">' 
				.. label .. '</div></div>'
		end
		local z = tablo[prefix .. unit .. postfix]
		if z then 
			mw.ext.luaglobal.add( 'CatClassValue', prefix .. unit .. postfix )
		end
		return '<div class="mil-div mil-c' .. color .. ' ' .. class .. '" style="width: ' .. width 
			.. 'px;"><div class="mil-number" style="font-size: ' .. fontSize .. '%; ">[' 
			.. tostring( mw.uri.fullUrl( 'НКВД:Выборка', { class = 'segment', value = prefix .. unit .. postfix } ) )
			.. ' ' .. label .. ']</div><div class="mil-quantity">' .. ( z or '<span style="color:red">0</span>' ) .. '</div></div>'
	end	
	
	local out = {}
	
	if #units > 0 then
		for _, unit in ipairs( units ) do
			out[#out+1] = processUnit( unit )
		end
		out = table.concat( out )
	else
		out = processUnit( '' )
	end
	
	return out
end

p['ВЧ'] = function ( frame )
	local args = frame:getParent().args
	local x = mw.text.trim( args[1] or '' )
	if x == 'начало' then
		return '<div class=mil-block><div style=display:none>'
	elseif x == 'конец' then
		return '</div></div>'
	else
		return '</div>' .. p._unit( args ) .. '<div style=display:none>'
	end
end

p['вч'] = function ( frame )
	local args = frame.args
	local x = mw.text.trim( args[1] or '' )
	if x == 'начало' then
		return '<div class=mil-block><div style=display:none>'
	elseif x == 'конец' then
		return '</div></div>'
	else
		return '</div>' .. p._unit( args ) .. '<div style=display:none>'
	end
end

return p