Модуль:TemplateDataDocМодуль для работы с TemplateData и автоматической генерации заготовок для копирования и примеров использования шаблонов на основе её. Для корректной сортировки параметров в TemplateData должен содержаться массив МетодыВнешние
Внутренние
require( 'strict' );
local docSubPage = mw.message.new( 'Templatedata-doc-subpage' ):plain();
local p = {};
local lastNumber = 0;
-- Enable/disable additional spacing for block-formatted templates
local formatBlockSpaces = true;
-- Params that should not be shown in code
local deprecatedParams = {
'nocat',
'from',
'nocoord',
'nocatcoord',
'Автооформление заголовка',
'автооформление заголовка',
'Ширина',
'ширина',
'Ширина изображения',
'ширина изображения',
'Ширина логотипа',
'ширина логотипа',
'Ширина автографа',
'ширина автографа',
};
local noDocNote = 'TemplateDataDoc: Запишите страницу для отображения заполненного шаблона.';
function p.processJson( json )
local status, data = pcall( mw.text.jsonDecode, json );
if status == false then
return nil;
end
if not data[ 'paramOrder' ] then
data[ 'paramOrder' ] = {};
for paramName, paramData in pairs( data[ 'params' ] ) do
table.insert( data[ 'paramOrder' ], paramName );
end
end
for _, param in ipairs( deprecatedParams ) do
if data[ 'params' ][ param ] ~= nil then
data[ 'params' ][ param ][ 'deprecated' ] = '-';
end
end
return data;
end
function p.getTemplateData( pageName )
local title = mw.title.makeTitle( 0, pageName );
if not title or not title.exists then
return false
end
local content = title:getContent()
if not content then
return false;
end;
local json = mw.ustring.match( content, '<[Tt]emplate[Dd]ata%s*>(.*)</[Tt]emplate[Dd]ata%s*>' );
if not json then
return nil;
end
return p.processJson( json )
end
function p.getValue( data, key )
if data[ key ] then
return data[ key ];
end
-- Numbered keys return as numbers
local nkey = tonumber( key );
if nkey ~= nil and data[ nkey ] then
return data[ nkey ];
end
return {};
end
-- See https://phabricator.wikimedia.org/diffusion/ETDA/browse/master/Specification.md?as=remarkup
-- We need a global format value for the 'block' and 'inline': [[phab:T205438]]
function p.convertFormatString( rawTemplateFormat )
local formatType = rawTemplateFormat or 'inline';
local templateFormat = formatType;
local isBlockFormatted = false;
if formatType == 'block' then
templateFormat = '{{_\n| _ = _\n}}';
isBlockFormatted = true;
elseif formatType == 'inline' then
templateFormat = '{{_|_=_}}';
end
return templateFormat, isBlockFormatted, formatType;
end
function p.getFormatParts( rawTemplateFormat, templateName )
local templateFormat, isBlockFormatted, formatType = p.convertFormatString( rawTemplateFormat );
local nameFormat = mw.ustring.match( templateFormat, '^[^|]+' );
local paramKeyFormat = mw.ustring.match( templateFormat, '%|[^=]+=' );
local paramValueFormat = mw.ustring.match( templateFormat, '=[^}]+' );
paramValueFormat = mw.ustring.sub( paramValueFormat, 2 );
local endFormat = mw.ustring.match( templateFormat, '%}%}.*$' );
local startFormat = mw.ustring.gsub( nameFormat, '_', templateName );
return isBlockFormatted, formatType, startFormat, endFormat, paramKeyFormat, paramValueFormat;
end
function p.formatKeyValue( key, parameterData, formatData )
if parameterData[ 'deprecated' ] then
return '';
end
local args = formatData.args;
local parameterName = key;
local nkey = tonumber( key );
-- Add additional spacing to string keys
if formatBlockSpaces and formatData.parameterLength and formatData.formatType ~= 'inline' and ( nkey == nil or lastNumber ~= nkey - 1 ) then
while mw.ustring.len( key ) < formatData.parameterLength do
key = key .. ' ';
end
end
-- Remove numbering for adjacent numbered keys
if nkey ~= nil and lastNumber == nkey - 1 then
key = '';
lastNumber = nkey;
end
local value = '';
if formatData.valueKey == 'example' and parameterData[ 'example' ] then
-- Example
value = parameterData[ 'example' ];
else
if formatData.valueKey == 'description' and parameterData[ 'description' ] then
-- Description
value = parameterData[ 'description' ];
if value ~= '' then
value = '<!-- ' .. value .. ' -->';
end
elseif parameterData[ 'autovalue' ] then
-- Autovalue
value = parameterData[ 'autovalue' ];
end
if args[ '$' .. parameterName ] and args[ '$' .. parameterName ] ~= '' then
-- Custom values from template call
value = args[ '$' .. parameterName ];
end
end
local formattedKey = mw.ustring.gsub( formatData.paramKeyFormat, '_+', key, 1 );
if key == '' then
formattedKey = mw.ustring.gsub( formattedKey, '=', '' );
end
return formattedKey .. mw.ustring.gsub( formatData.paramValueFormat, '_', value, 1 );
end
function p.generateBlankCode( templateData, templateName, args )
if templateData == false then
return '{{' .. templateName .. '}}';
end
local parameterLength = 0;
for i, parameterName in ipairs( templateData[ 'paramOrder' ] ) do
local parameterData = p.getValue( templateData[ 'params' ], parameterName );
if not parameterData[ 'deprecated' ] then
local length = mw.ustring.len( parameterName );
if length > parameterLength then
parameterLength = length;
end
end
end
local isBlockFormatted, formatType, startFormat, endFormat, paramKeyFormat, paramValueFormat = p.getFormatParts( templateData[ 'format' ], templateName );
local out = startFormat;
lastNumber = 0;
for i, parameterName in ipairs( templateData[ 'paramOrder' ] ) do
local parameterData = p.getValue( templateData[ 'params' ], parameterName );
if parameterData[ 'inherits' ] then
parameterData = p.getValue( templateData[ 'params' ], parameterData[ 'inherits' ] );
end
out = out .. p.formatKeyValue( parameterName, parameterData, {
args = args,
valueKey = ( args[ 'description' ] and 'description' or nil ),
formatType = formatType,
isBlockFormatted = isBlockFormatted,
parameterLength = parameterLength,
paramKeyFormat = paramKeyFormat,
paramValueFormat = paramValueFormat,
} );
end
return out .. endFormat;
end
function p.generateBlank( frame )
local frame = mw.getCurrentFrame();
local getArgs = require( 'Module:Arguments' ).getArgs;
local args = getArgs( frame );
local templateName = frame.args[ 1 ];
table.remove( args, 1 );
local docPage = 'Template:' .. templateName .. '/' .. docSubPage;
local templateData = p.getTemplateData( docPage );
local out = p.generateBlankCode( templateData, templateName, args );
local previewNote = ''
if templateData == false and frame:preprocess('{{REVISIONID}}') == '' then
previewNote = '<div class="warningbox">' .. noDocNote .. '</div>';
end
return previewNote .. frame:extensionTag{
name = 'syntaxhighlight',
args = { lang = 'html', copy = true },
content = out,
};
end
function p.generateExampleCode( templateData, templateName, args )
if templateData == false then
return '{{' .. templateName .. '}}';
end
local parameterLength = 0;
for i, parameterName in ipairs( templateData[ 'paramOrder' ] ) do
local parameterData = p.getValue( templateData[ 'params' ], parameterName );
if parameterData[ 'example' ] and not parameterData[ 'deprecated' ] then
local length = mw.ustring.len( parameterName );
if length > parameterLength then
parameterLength = length;
end
end
end
local isBlockFormatted, formatType, startFormat, endFormat, paramKeyFormat, paramValueFormat = p.getFormatParts( templateData[ 'format' ], templateName );
local out = startFormat;
lastNumber = 0;
for i, parameterName in ipairs( templateData[ 'paramOrder' ] ) do
local parameterData = p.getValue( templateData[ 'params' ], parameterName );
if parameterData[ 'inherits' ] then
parameterData = p.getValue( templateData[ 'params' ], parameterData[ 'inherits' ] );
end
if parameterData[ 'example' ] then
out = out .. p.formatKeyValue( parameterName, parameterData, {
args = args,
valueKey = 'example',
formatType = formatType,
isBlockFormatted = isBlockFormatted,
parameterLength = parameterLength,
paramKeyFormat = paramKeyFormat,
paramValueFormat = paramValueFormat,
} );
end
end
return out .. endFormat;
end
function p.generateExample( frame )
local frame = mw.getCurrentFrame();
local args = frame.args;
local templateName = frame.args[ 1 ];
local docPage = 'Template:' .. templateName .. '/' .. docSubPage;
local templateData = p.getTemplateData( docPage );
local out = p.generateExampleCode( templateData, templateName, args );
local previewNote = ''
if templateData == false and frame:preprocess('{{REVISIONID}}') == '' then
previewNote = '<div class="warningbox">' .. noDocNote .. '</div>';
end
return previewNote .. frame:preprocess( out ) .. frame:extensionTag{
name = 'syntaxhighlight',
args = { lang = 'html' },
content = out,
};
end
return p;
|
Index:
pl ar de en es fr it arz nl ja pt ceb sv uk vi war zh ru af ast az bg zh-min-nan bn be ca cs cy da et el eo eu fa gl ko hi hr id he ka la lv lt hu mk ms min no nn ce uz kk ro simple sk sl sr sh fi ta tt th tg azb tr ur zh-yue hy my ace als am an hyw ban bjn map-bms ba be-tarask bcl bpy bar bs br cv nv eml hif fo fy ga gd gu hak ha hsb io ig ilo ia ie os is jv kn ht ku ckb ky mrj lb lij li lmo mai mg ml zh-classical mr xmf mzn cdo mn nap new ne frr oc mhr or as pa pnb ps pms nds crh qu sa sah sco sq scn si sd szl su sw tl shn te bug vec vo wa wuu yi yo diq bat-smg zu lad kbd ang smn ab roa-rup frp arc gn av ay bh bi bo bxr cbk-zam co za dag ary se pdc dv dsb myv ext fur gv gag inh ki glk gan guw xal haw rw kbp pam csb kw km kv koi kg gom ks gcr lo lbe ltg lez nia ln jbo lg mt mi tw mwl mdf mnw nqo fj nah na nds-nl nrm nov om pi pag pap pfl pcd krc kaa ksh rm rue sm sat sc trv stq nso sn cu so srn kab roa-tara tet tpi to chr tum tk tyv udm ug vep fiu-vro vls wo xh zea ty ak bm ch ny ee ff got iu ik kl mad cr pih ami pwn pnt dz rmy rn sg st tn ss ti din chy ts kcg ve
Portal di Ensiklopedia Dunia