Модуль:CargoGen
Материал из Указатель частей и соединений РККА 1941-1945
Для документации этого модуля может быть создана страница Модуль:CargoGen/doc
local p = {} function p.CargoGen ( frame ) local args = frame.args local counter = 0 -- local fields, datatype, default, descrition = {}, {}, {}, {} local arrays = { {}, {}, {}, {} } local maxl = 0 for _, field in ipairs( args ) do counter = counter + 1 if counter == 5 then counter = 1 end field = mw.text.trim( field ) if counter == 1 then if #field > maxl then maxl = #field end elseif counter == 2 then field = string.gsub( field, ':', '=' ) end table.insert( arrays[counter], mw.text.trim( field ) ) end if counter ~= 4 then error( 'Неверное число аргументов! Для каждого поля: имя | тип (с «:» вместо «=») | значение по умолчанию | описание' ) end local res = { '<noinclude><div class=cargo-declare>{{#cargo_declare: _table = ' .. args._table } for i = 1, #arrays[1] do table.insert( res, ' | ' .. string.sub ( arrays[1][i] .. string.rep( ' ', maxl ), 1, maxl ) .. ' = ' .. arrays[2][i] ) end table.insert( res, '}}</div>' ) table.insert( res, '<table class="wikitable sortable cargo-description"><tr><th>field</th><th>type</th><th>default</th><th>description</th></tr>' ) for i = 1, #arrays[1] do table.insert( res, '<tr><td>' .. arrays[1][i] .. '</td><td>' .. arrays[2][i] .. '</td><td>' .. arrays[3][i] .. '</td><td>' .. arrays[4][i] .. '</td></tr>' ) end table.insert( res, '</table></noinclude><includeonly>{{#cargo_store: _table = ' .. args._table ) for i = 1, #arrays[1] do table.insert( res, ' | ' .. string.sub ( arrays[1][i] .. string.rep( ' ', maxl ), 1, maxl ) .. ' = {{{' .. arrays[1][i] .. '|' .. arrays[3][i] .. '}}}' ) end table.insert( res, '}}</includeonly><noinclude><!-- ' ) table.insert( res, '{{subst:#invoke:CargoGen|CargoGen | _table = ' .. args._table ) for i = 1, #arrays[1] do table.insert( res, ' | ' .. arrays[1][i] .. ' | ' .. string.gsub( arrays[2][i], '=', ':' ) .. ' | ' .. arrays[3][i] .. ' | ' .. arrays[4][i] ) end table.insert( res, '}}' ) -- «--></noinclude>» опущены для удобства return table.concat( res, '\n' ) end return p