Перейти к содержанию

Модуль:Родственные проекты

Википедия сайттаҥ

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

local p = {};

-- Configuration data
local config = require('Module:Родственные_проекты/config');

-- Modules 
local getArgs = require('Module:Arguments').getArgs;
local errorMsg = require('Module:Error').error;
local yesno = require('Module:Yesno');

-- Variables
local modules = {};

-- Helpers
local function formatProject( frame, item )
	local out = '';
	local context = frame;

	if (item.value == nil or item.value == '') and item['module'] ~= nil and item['function'] ~= nil then
		if modules[item['module']] == nil then
			modules[item['module']] = require( 'Module:' .. item['module'] );
		end
		if item['args'] ~= nil then
			context.args = item['args'];
		end
		item.wikiValue = modules[item['module']][item['function']]( context );
		-- add wiki prefix
		if item.wikiValue ~= nil and item.wikiValue ~= '' and item.wikiPrefix ~= nil and item.wikiPrefix ~= '' then
			item.wikiValue = item.wikiPrefix.. ':' .. item.wikiValue;
		end
		-- override value
		item.value = item.wikiValue;
	end
	
	if item.value ~= nil and item.value ~= '' then
		-- replace variables
		if item.title ~= nil and item.title ~= '' then
			item.title = string.gsub( item.title, '%$1', item.value );
		end
		-- format prefix
		if string.find( item.value, '^' .. item.prefix .. ':' ) ~= nil then
			item.prefixFormatted = '';
		else
			item.prefixFormatted = item.prefix .. ':';
		end
		-- format image
		if item['image-template'] ~= nil and item['image-template'] ~= '' then
			item.imageFormatted = frame:expandTemplate{ title = item['image-template'], args = { item.value, size = config.params.itemImageSize } };
		elseif item.image ~= nil and item.image ~= '' then
			item.imageFormatted = '[[File:' .. item.image .. '|link=|alt=|' .. config.params.itemImageSize .. 'px]]';
		end
		-- format wikilink
		out = out .. '[[' .. item.prefixFormatted .. item.value .. '|' .. item.title .. ']]';
	end
	
	return out;
end

local function getParamsList( frame, args, list )
	local result = {};
	
	for i, item in ipairs( list ) do
		local value = args[item.id];
		if value == nil or value ~= '-' then
			item.localValue = value;
			item.value = value;
			item.formatted = formatProject( frame, item );
			if item.formatted ~= '' then
				table.insert( result, item );
			end
		end
	end
	
	return result;
end

-- Renders
local function renderProjects( frame, list )
	local res = mw.html.create();
	
	for i, item in ipairs( list ) do
		-- wrapper
		local li = res:tag( 'li' )
			:addClass( config.params.tsClass .. '-item' );
		-- class
		local class = '';
		if item.name == 'project' or item.name == 'portal' then
			class = item.name .. '-box';
		else
			class = item.name .. '-ref';
		end
		-- image
		if item.imageFormatted ~= nil and item.imageFormatted ~= '' then
			li:tag( 'span' )
				:addClass( config.params.tsClass .. '-image' )
				:wikitext( item.imageFormatted );
		end
		-- formatted value
		li:tag( 'span' )
			:addClass( config.params.tsClass .. '-label' )
			:addClass( class )
			:wikitext( item.formatted );
	end
	
	return res;
end

local function render( frame, title, interprojects, otherprojects )
	local templateStyles = frame:extensionTag{ name = 'templatestyles', args = { src = config.params.templateStyles } };
	local interprojectsFormatted = renderProjects( frame, interprojects );
	local otherprojectsFormatted = renderProjects( frame, otherprojects );
	-- wrapper
	local res = mw.html.create()
		:wikitext( templateStyles );
	local body = res:tag( 'div' )
		:addClass( config.params.tsClass )
		:addClass( config.params.bodyClass );
	-- render title
	if table.getn( interprojects ) > 1  and ( title == nil or title == '' ) then
		title = config.strings['title'];
	end
	if title ~= nil and title ~= '' then
		body:tag( 'div' )
			:addClass( config.params.tsClass .. '-header' )
			:wikitext( "'''" .. title .. "'''" );
	end
	-- render inter projects
	if table.getn( interprojects ) > 0 then
		body:tag( 'ul' )
			:node( interprojectsFormatted );
	end
	-- render separator
	if table.getn( interprojects ) > 0 and table.getn( otherprojects ) > 0 then
		body:tag( 'hr' );
	end
	-- render other projects
	if table.getn( otherprojects ) > 0 then
		body:tag( 'ul' )
			:node( otherprojectsFormatted );
	end

	return res;
end

-- Categories
local function formatCategory( value )
	return '[[Category:' .. value .. ']]';
end

local function formatCategories( frame, list )
	local categories = {};
	
	for i, item in ipairs( list ) do
		local categoryItem = config.categories[ item.name ];
		if categoryItem ~= nil then
			if categoryItem['local'] ~= nil and item.localValue ~= nil and item.localValue ~= '' then
				local category = formatCategory( categoryItem['local'] )
				table.insert( categories, category );
			elseif categoryItem['default'] ~= nil then
				local category = formatCategory( categoryItem['default'] )
				table.insert( categories, category );
			end
			-- when local value equal to wikidata
			if categoryItem['equal'] ~= nil 
				and item.localValue ~= nil and item.localValue ~= '' 
				and item.wikiValue ~= nil and item.wikiValue ~= ''
				and item.localValue == item.wikiValue
			then
				local category = formatCategory( categoryItem['equal'] )
				table.insert( categories, category );
			end
			-- category for non existing pages
			if categoryItem['not_exists'] ~= nil then
				local page = mw.title.new( item.value, item.prefix );
				if not page.exists then
					local category = formatCategory( categoryItem['not_exists'] )
					table.insert( categories, category );
				end
			end
		end
	end
	
	return table.concat( categories );
end

local function renderCategories( frame, interprojects, otherprojects )
	-- wrapper
	local res = mw.html.create();
	-- format iterprojects categories
	local interprojectsCategories = formatCategories( frame, interprojects );
	res:wikitext( interprojectsCategories );
	-- format otherprojects categories
	local otherprojectsCategories = formatCategories( frame, otherprojects );
	res:wikitext( otherprojectsCategories );
	-- service categories
	if next( interprojects ) == nil and next( otherprojects ) == nil then
		local category = formatCategory( config.categories['empty'] );
		res:wikitext( category );
	end
	
	return res;
end

-- Preview
local function renderPreview( frame )
	local res = mw.html.create();
	if config.strings['preview-error'] ~= nil and config.strings['preview-error'] ~= '' then
		local message = errorMsg{config.strings['preview-error']};
		res:wikitext( message );
	end
	return res;
end;

-- Main
function p.main( frame )
	local args = getArgs( frame, { wrappers = config.params.wrappers } );
	local nocat = yesno( args['nocat'] );
	-- title parameter
	local title = '';
	for i, item in ipairs( config.title.id ) do
		if args[item] ~= nil and args[item] ~= '' then
			title = args[item];
		end
	end
	if title ~= nil and title ~= '' then
		title = title .. ':'
	end
	-- get parameters list
	local interprojects = getParamsList( frame, args, config['interprojects'] );
	local otherprojects = getParamsList( frame, args, config['otherprojects'] );
	-- wrapper
	local res = mw.html.create();
	-- render body
	if next( interprojects ) ~= nil or next( otherprojects ) ~= nil then
		local body = render( frame, title, interprojects, otherprojects );
		res:node( body );
	elseif frame:preprocess( '{{REVISIONID}}' ) == '' then
		local preview = renderPreview( frame );
		res:node( preview );
	end
	-- render category list
	if not nocat then
		local pageTitle = mw.title.getCurrentTitle();
		if pageTitle.namespace == 0 then
			local categories = renderCategories( frame, interprojects, otherprojects );
			res:node( categories );
		end
	end
	
	return tostring( res );
end

return p;
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy