Модуль:TemplateDataDoc

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

local docSubPage = mw.message.new( 'Templatedata-doc-subpage' ):plain();

p = {};

function getTemplateData( pageName )
	local title = mw.title.makeTitle( 0, pageName );
	if not title or not title.exists or not title:getContent() then
		return nil;
	end;

	local rawData = mw.ustring.match( title:getContent(), '<templatedata%s*>(.*)</templatedata%s*>' );
	if not rawData then
		return nil;
	end

	local status, data = pcall( mw.text.jsonDecode, rawData );
	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
	
	local deprecatedParams = {
		'nocat',
		'nocoord',
		'nocatcoord',
	};
	for _, param in ipairs( deprecatedParams ) do
		if data[ 'params' ][ param ] ~= nil then
			data[ 'params' ][ param ][ 'deprecated' ] = 'Не для документации.';
		end
	end

	return data;
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 = getTemplateData( docPage );

	local paramaterLength = 0;
	for i, parameterName in ipairs( templateData[ 'paramOrder' ] ) do
		local parameterData = templateData[ 'params' ][ parameterName ] or {};
		if not parameterData[ 'deprecated' ] then
			local length = mw.ustring.len( parameterName );
			if length > paramaterLength then
				paramaterLength = length;
			end
		end
	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]]
	local templateFormat = templateData[ 'format' ];
	if templateFormat == nil then
		error('Пожалуйста, укажите формат кода шаблона в его TemplateData')
	end
	local isBlockFormatted = false;
	if templateFormat == 'block' then
		templateFormat = '{{_\n| _ = _\n}}';
		isBlockFormatted = true;
	elseif templateFormat == 'inline' then
		templateFormat = '{{_|_=_}}';
	end
	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 out = mw.ustring.gsub( nameFormat, '_', templateName );

	local lastNumber = 0;
	for i, parameterName in ipairs( templateData[ 'paramOrder' ] ) do
		local parameterData = templateData[ 'params' ][ parameterName ] or {};
		if parameterData[ 'inherits' ] then
			parameterData = templateData[ 'params' ][ parameterData[ 'inherits' ] ] or {};
		end
		
		if not parameterData[ 'deprecated' ] then
			local key = parameterName;
			local nkey = tonumber(key);
			if isBlockFormatted then
				if nkey == nil or lastNumber ~= nkey - 1 then
					while mw.ustring.len( key ) < paramaterLength do
						key = key .. ' ';
					end
				end
			end
			
			if nkey ~= nil and lastNumber == nkey - 1 then
				key = '';
				lastNumber = nkey;
			end
	
			local value = '';
			if args[ 'description' ] == '1' and parameterData[ 'description' ] then
				value = parameterData[ 'description' ];
				if value ~= '' then
					value = '&lt;!-- ' .. value .. ' --&gt;';
				end
			elseif parameterData[ 'autovalue' ] then
				value = parameterData[ 'autovalue' ];
			end
			if args[ '$' .. parameterName ] and args[ '$' .. parameterName ] ~= '' then
				value = args[ '$' .. parameterName ];
			end
	
			local formattedKey = mw.ustring.gsub( paramKeyFormat, '_+', key, 1 );
			if key == '' then
				formattedKey = mw.ustring.gsub( formattedKey, '=', '' );
			end
			out = out .. formattedKey;

			out = out .. mw.ustring.gsub( paramValueFormat, '_', value, 1 );
		end
	end
	
	out = out .. endFormat;

	return frame:extensionTag{ name = 'pre', content = out };
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 = getTemplateData( docPage );
	
	local paramaterLength = 0;
	for i, parameterName in ipairs( templateData[ 'paramOrder' ] ) do
		local parameterData = templateData[ 'params' ][ parameterName ] or {};
		if parameterData[ 'example' ] and not parameterData[ 'deprecated' ] then
			local length = mw.ustring.len( parameterName );
			if length > paramaterLength then
				paramaterLength = length;
			end
		end
	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]]
	local templateFormat = templateData[ 'format' ];
	local isBlockFormatted = false;
	if templateFormat == 'block' then
		templateFormat = '{{_\n| _ = _\n}}';
		isBlockFormatted = true;
	elseif templateFormat == 'inline' then
		templateFormat = '{{_|_=_}}';
	end
	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 out = mw.ustring.gsub( nameFormat, '_', templateName );

	local lastNumber = 0;
	for i, parameterName in ipairs( templateData[ 'paramOrder' ] ) do
		local parameterData = templateData[ 'params' ][ parameterName ] or {};
		if parameterData[ 'inherits' ] then
			parameterData = templateData[ 'params' ][ parameterData[ 'inherits' ] ] or {};
		end
		
		if parameterData[ 'example' ] and not parameterData[ 'deprecated' ] then
			local key = parameterName;
			local nkey = tonumber( key );
			if isBlockFormatted then
				if nkey == nil or lastNumber ~= nkey - 1 then
					while mw.ustring.len( key ) < paramaterLength do
						key = key .. ' ';
					end
				end
			end
			
			if nkey ~= nil and lastNumber == nkey - 1 then
				key = '';
				lastNumber = nkey;
			end

			local value = parameterData[ 'example' ];

			local formattedKey = mw.ustring.gsub( paramKeyFormat, '_+', key, 1 );
			if key == '' then
				formattedKey = mw.ustring.gsub( formattedKey, '=', '' );
			end
			out = out .. formattedKey;

			out = out .. mw.ustring.gsub( paramValueFormat, '_', value, 1 );
		end
	end
	
	out = out .. endFormat;
	
	return frame:preprocess( out ) .. frame:extensionTag{ name = 'pre', content = out };
end

return p;