Модуль:Military: различия между версиями

Материал из Кадровый состав НКВД 1935-1939
Перейти к навигации Перейти к поиску
Нет описания правки
Нет описания правки
Строка 1: Строка 1:
local p = {}
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
prev | start | stop | post
Строка 230: Строка 161:
return '</div>' .. p._unit( args ) .. '<div style=display:none>'
return '</div>' .. p._unit( args ) .. '<div style=display:none>'
end
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 x .. ' (' .. z .. ')'
end
end


return p
return p

Версия от 12:35, 22 июня 2018

Для документации этого модуля может быть создана страница Модуль: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 x .. ' (' .. z .. ')'
end

return p