Модуль:Military: различия между версиями
Перейти к навигации
Перейти к поиску
StasR (обсуждение | вклад) Нет описания правки |
StasR (обсуждение | вклад) Нет описания правки |
||
(не показаны 22 промежуточные версии этого же участника) | |||
Строка 1: | Строка 1: | ||
local p = {} | local p = {} | ||
--[===[ | --[===[ | ||
prev | start | stop | post | prev | start | stop | post | ||
могут использоваться при единственном элементе (если отсутствует stop) | могут использоваться при единственном элементе (если отсутствует stop) | ||
* текст = показываемый текст | * текст = показываемый текст | ||
* подсказка = подсказка к тексту | * подсказка = подсказка к тексту | ||
цвет = 1 (по умолчанию), 2, 3, 9 | цвет = 1 (по умолчанию), 2, 3, 9 | ||
формат = стандарт (по умолчанию), заголовок (узкая полоска), раздел (стандартная высота, сдвиг вправо) | формат = стандарт (по умолчанию), заголовок (узкая полоска), раздел (стандартная высота, сдвиг вправо) | ||
Строка 80: | Строка 11: | ||
размер = число колонок | размер = число колонок | ||
--]===] | --]===] | ||
local function miliLink( unit ) | |||
-- return tostring( mw.uri.fullUrl( 'НКВД:Выборка', { class = 'segment', value = unit } ) ) | |||
return mw.site.server .. '/segment/' .. string.gsub( unit, ' ', '%%20' ) | |||
end | |||
local function parseList( list ) | local function parseList( list ) | ||
Строка 99: | Строка 35: | ||
return out | return out | ||
end | end | ||
function p.Unit( frame ) | function p.Unit( frame ) | ||
Строка 121: | Строка 52: | ||
['раздел'] = '', | ['раздел'] = '', | ||
} | } | ||
local tablo = mw.loadData( 'Module:CatNav/segment' ) | local tablo = mw.loadData( 'Module:CatNav/segment' ) | ||
local checking = mw.title.getCurrentTitle().subpageText == 'вне перечня' | local checking = mw.title.getCurrentTitle().subpageText == 'вне перечня' | ||
local prefix, units, postfix | local prefix, units, postfix | ||
= mw.text.trim( args[1] or '' ), parseList( args[2] or '' ), mw.text.trim( args[3] or '' ) | = mw.text.trim( args[1] or '' ), parseList( args[2] or '' ), mw.text.trim( args[3] or '' ) | ||
local text, comment | local text, comment | ||
if #units <= 1 then | if #units <= 1 then | ||
text = args['текст'] | text = args['текст'] | ||
comment = args['подсказка'] | comment = args['подсказка'] | ||
end | end | ||
if prefix ~= '' then | if prefix ~= '' then | ||
prefix = prefix .. ' ' | prefix = prefix .. ' ' | ||
Строка 139: | Строка 70: | ||
postfix = ' ' .. postfix | postfix = ' ' .. postfix | ||
end | end | ||
local width = ( args['размер'] or 1 ) * 29 - 1 | local width = ( args['размер'] or 1 ) * 29 - 1 | ||
local mode = mw.text.trim( args['формат'] or '' ) | local mode = mw.text.trim( args['формат'] or '' ) | ||
local modeCSSadd = modeDivCSS[mode] or '' | local modeCSSadd = modeDivCSS[mode] or '' | ||
mode = modeClasses[mode] or 'mil-error' | mode = modeClasses[mode] or 'mil-error' | ||
local color = tonumber( args['цвет'] or 1 ) or 1 | local color = tonumber( args['цвет'] or 1 ) or 1 | ||
local guard = args['гв'] | local guard = args['гв'] | ||
Строка 153: | Строка 84: | ||
guard = '' | guard = '' | ||
end | end | ||
local fontSize = args['шрифт'] or '100' | local fontSize = args['шрифт'] or '100' | ||
if mw.ustring.sub( fontSize, 1, 3 ) == 'мел' then | if mw.ustring.sub( fontSize, 1, 3 ) == 'мел' then | ||
Строка 160: | Строка 91: | ||
fontSize = mw.ustring.sub( fontSize, 1, -2 ) | fontSize = mw.ustring.sub( fontSize, 1, -2 ) | ||
end | end | ||
if fontSize == '100' then | |||
fontSize = '' | |||
else | |||
fontSize = 'font-size:' .. fontSize ..'%' | |||
end | |||
local class = args.class or '' | local class = args.class or '' | ||
local function processUnit( unit ) | local function processUnit( unit ) | ||
local label = text or unit | local label = text or unit | ||
Строка 168: | Строка 103: | ||
label = '<abbr title="' .. comment .. '">' .. label .. '</abbr>' | label = '<abbr title="' .. comment .. '">' .. label .. '</abbr>' | ||
end | end | ||
if unit == '' then | if unit == '0' then | ||
label = '' | |||
end | |||
if unit == '' or unit == '0' then | |||
if checking then | if checking then | ||
return | return '' | ||
end | end | ||
return '<div class="mil-div mil-c' .. color .. ' ' .. guard .. class | return '<div class="mil-div mil-c' .. color .. ' ' .. guard .. class | ||
.. '" style="width: ' .. width .. 'px; ' .. modeCSSadd | .. '" style="width: ' .. width .. 'px; ' .. modeCSSadd | ||
.. (args.divcss or '') .. '"><div class="' .. mode | .. (args.divcss or '') .. '"><div class="' .. mode | ||
.. '" style="width: ' .. width .. 'px; | .. '" style="width: ' .. width .. 'px;' .. fontSize .. '">' | ||
.. label .. '</div></div>' | .. label .. '</div></div>' | ||
end | end | ||
local z = tablo[prefix .. unit .. postfix] | local z = tablo[prefix .. unit .. postfix] | ||
if z then | if z then | ||
mw.ext.luaglobal.add( 'CatClassValue', prefix .. unit .. postfix ) | mw.ext.luaglobal.add( 'CatClassValue', prefix .. unit .. postfix ) | ||
end | end | ||
if checking then | if checking then | ||
return | return '' | ||
end | end | ||
return '<div class="mil-div mil-c' .. color .. ' ' .. guard .. class .. '" style="width: ' .. width | return '<div class="mil-div mil-c' .. color .. ' ' .. guard .. class .. '" style="width: ' .. width | ||
.. 'px; ' .. (args.divcss or '') .. '"><div class="mil-number" style=" | .. 'px; ' .. (args.divcss or '') .. '"><div class="mil-number" style="' .. fontSize .. '">[' | ||
.. | .. miliLink( prefix .. unit .. postfix ) | ||
.. ' ' .. label .. ']</div><div class="mil-quantity">' .. ( z or '<span style="color:red">0</span>' ) .. '</div></div>' | .. ' ' .. label .. ']</div><div class="mil-quantity">' .. ( z or '<span style="color:red">0</span>' ) .. '</div></div>' | ||
end | end | ||
local out = {} | local out = {} | ||
if #units > 0 then | if #units > 0 then | ||
for _, unit in ipairs( units ) do | for _, unit in ipairs( units ) do | ||
Строка 201: | Строка 139: | ||
out = processUnit( '' ) | out = processUnit( '' ) | ||
end | end | ||
return out | return out | ||
end | end | ||
Строка 228: | Строка 166: | ||
end | end | ||
end | end | ||
p['отд'] = function ( frame ) | |||
local checking = mw.title.getCurrentTitle().subpageText == 'вне перечня' | |||
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 checking then | |||
return '' | |||
end | |||
if ( z or 0 ) == 0 then | |||
z = '—' | |||
end | |||
return '[' .. miliLink( x ) .. ' ' .. x .. '] (' .. z .. ')' | |||
end | |||
p['_список'] = function ( args ) | |||
local checking = mw.title.getCurrentTitle().subpageText == 'вне перечня' | |||
local ptn = args['формат'] or '%t (%n)\n' | |||
local tablo = mw.loadData( 'Module:CatNav/segment' ) | |||
local out = {} | |||
for _, x in ipairs( args ) do | |||
x = mw.text.trim( x ) | |||
if x ~= '' then | |||
local shift, val = string.match( x, '^([%*: ]+)(.+)$' ) | |||
if not shift then | |||
shift = '* ' | |||
val = x | |||
end | |||
x, val = val, string.gsub( val, "'", '' ) -- чтобы легко вставлять курсив/жирный | |||
local z = tablo[val] | |||
if z then | |||
mw.ext.luaglobal.add( 'CatClassValue', val ) | |||
end | |||
if ( z or 0 ) == 0 then | |||
z = '—' | |||
end | |||
local el = shift .. string.gsub( ptn, '(%%.)', { | |||
['%%'] = '%', | |||
['%t'] = '[' .. miliLink( val ) .. ' ' .. x .. ']', | |||
['%n'] = z, | |||
['%l'] = '\n', | |||
} ) | |||
table.insert( out, el ) | |||
end | |||
end | |||
if checking then | |||
return '' | |||
end | |||
return table.concat( out ) | |||
end | |||
p['список'] = function ( frame ) | |||
return p['_список']( frame.args ) | |||
end | |||
p['Список'] = function ( frame ) | |||
return p['_список']( frame:getParent().args ) | |||
end | |||
p['Остаток'] = function ( frame ) | |||
local used = mw.ext.luaglobal.get( 'CatClassValue' ) | |||
local tablo = mw.loadData( 'Module:CatNav/segment' ) | |||
local syn = mw.loadData( 'Module:CatNav/segment/sub' ) | |||
local revert = {} | |||
for _, u in ipairs( used ) do | |||
revert[u] = true | |||
if syn[u] then | |||
for _, v in ipairs( syn[u] ) do | |||
revert[v] = true | |||
end | |||
end | |||
end | |||
local unused = {} | |||
for u, n in pairs( tablo ) do | |||
if not revert[u] then | |||
table.insert( unused, '[' .. miliLink( u ) .. ' ' .. u .. '] (' .. n .. ')' ) | |||
end | |||
end | |||
table.sort( unused ) | |||
return 'Отсутствуют в перечне (' .. #unused .. '/' .. #used .. '): ' .. table.concat( unused, ', ' ) | |||
end | |||
return p | return p |
Версия от 11:18, 27 марта 2019
Для документации этого модуля может быть создана страница Модуль:Military/doc
local p = {}
--[===[
prev | start | stop | post
могут использоваться при единственном элементе (если отсутствует stop)
* текст = показываемый текст
* подсказка = подсказка к тексту
цвет = 1 (по умолчанию), 2, 3, 9
формат = стандарт (по умолчанию), заголовок (узкая полоска), раздел (стандартная высота, сдвиг вправо)
шрифт = стандарт (по умолчанию), мелко (80%), хх%
размер = число колонок
--]===]
local function miliLink( unit )
-- return tostring( mw.uri.fullUrl( 'НКВД:Выборка', { class = 'segment', value = unit } ) )
return mw.site.server .. '/segment/' .. string.gsub( unit, ' ', '%%20' )
end
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
if fontSize == '100' then
fontSize = ''
else
fontSize = 'font-size:' .. fontSize ..'%'
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;' .. 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="' .. fontSize .. '">['
.. miliLink( 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 checking = mw.title.getCurrentTitle().subpageText == 'вне перечня'
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 checking then
return ''
end
if ( z or 0 ) == 0 then
z = '—'
end
return '[' .. miliLink( x ) .. ' ' .. x .. '] (' .. z .. ')'
end
p['_список'] = function ( args )
local checking = mw.title.getCurrentTitle().subpageText == 'вне перечня'
local ptn = args['формат'] or '%t (%n)\n'
local tablo = mw.loadData( 'Module:CatNav/segment' )
local out = {}
for _, x in ipairs( args ) do
x = mw.text.trim( x )
if x ~= '' then
local shift, val = string.match( x, '^([%*: ]+)(.+)$' )
if not shift then
shift = '* '
val = x
end
x, val = val, string.gsub( val, "'", '' ) -- чтобы легко вставлять курсив/жирный
local z = tablo[val]
if z then
mw.ext.luaglobal.add( 'CatClassValue', val )
end
if ( z or 0 ) == 0 then
z = '—'
end
local el = shift .. string.gsub( ptn, '(%%.)', {
['%%'] = '%',
['%t'] = '[' .. miliLink( val ) .. ' ' .. x .. ']',
['%n'] = z,
['%l'] = '\n',
} )
table.insert( out, el )
end
end
if checking then
return ''
end
return table.concat( out )
end
p['список'] = function ( frame )
return p['_список']( frame.args )
end
p['Список'] = function ( frame )
return p['_список']( frame:getParent().args )
end
p['Остаток'] = function ( frame )
local used = mw.ext.luaglobal.get( 'CatClassValue' )
local tablo = mw.loadData( 'Module:CatNav/segment' )
local syn = mw.loadData( 'Module:CatNav/segment/sub' )
local revert = {}
for _, u in ipairs( used ) do
revert[u] = true
if syn[u] then
for _, v in ipairs( syn[u] ) do
revert[v] = true
end
end
end
local unused = {}
for u, n in pairs( tablo ) do
if not revert[u] then
table.insert( unused, '[' .. miliLink( u ) .. ' ' .. u .. '] (' .. n .. ')' )
end
end
table.sort( unused )
return 'Отсутствуют в перечне (' .. #unused .. '/' .. #used .. '): ' .. table.concat( unused, ', ' )
end
return p