Модуль:Military

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

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

local p = {}

--[===[
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 checking = mw.title.getCurrentTitle().subpageText == 'вне перечня'
	
	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 guard = args['гв']
	if guard then
		guard = 'mil-cg '
	else
		guard = ''
	end
	
	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 == '0' then
			label = ''
		end
		if unit == '' or unit == '0' then
			if checking then
					return
			end
			return '<div class="mil-div mil-c' .. color .. ' ' .. guard .. 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
		if checking then
				return
		end
		return '<div class="mil-div mil-c' .. color .. ' ' .. guard .. class .. '" style="width: ' .. width 
			.. 'px; ' .. (args.divcss or '') .. '"><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

p['отд'] = function ( frame )
	local args = frame.args
	local x = mw.text.trim( args[1] or '' )
	local tablo = mw.loadData( 'Module:CatNav/segment' )
	local z = tablo[x]
	if z then
		mw.ext.luaglobal.add( 'CatClassValue', x )
	end
	if ( z or 0 ) == 0 then
		z = '—'
	end
	return '[' .. tostring( mw.uri.fullUrl( 'НКВД:Выборка', { class = 'segment', value = x } ) ) .. ' ' .. x .. '] (' .. z .. ')'
end

p['список'] = function ( frame )
	local args = frame.args
	local format = args['формат'] or '* %t (%n)\n'
	local tablo = mw.loadData( 'Module:CatNav/segment' )
	local out = {}
	for _, x in ipairs( args ) do
		local z = tablo[x]
		if z then
			mw.ext.luaglobal.add( 'CatClassValue', x )
		end
		if ( z or 0 ) == 0 then
			z = '—'
		end
		table.insert( out, string.gsub( format, '(%%.)'), { 
			['%%'] = '%', 
			['%t'] = '[' .. tostring( mw.uri.fullUrl( 'НКВД:Выборка', { class = 'segment', value = x } ) ) .. ' ' .. x .. ']',
			['%n'] = z, }
		)
	end
	return table.concat( out )
end

return p