Модуль:Родственные проекты: различия между версиями
Содержимое удалено Содержимое добавлено
Putnik (обсуждение | вклад) |
(нет различий)
|
Версия от 02:02, 2 сентября 2019
Для документации этого модуля может быть создана страница Модуль:Родственные проекты/doc
local p = {};
-- Configuration data
local config = require('Модуль:Родственные_проекты/config');
-- Modules
local getArgs = require('Module:Arguments').getArgs;
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 = '[[Файл:' .. 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' );
-- 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( 'image' )
:wikitext( item.imageFormatted );
end
-- formatted value
li:tag( 'span' )
:addClass( 'label' )
:addClass( class )
:wikitext( item.formatted );
end
return res;
end
local function renderPreview( frame )
local res = mw.html.create( 'div' )
:addClass( 'hatnote' )
:css( 'color', 'red' )
:wikitext( config.strings['preview-error'] );
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( 'ts-Родственные_проекты tright metadata plainlinks plainlist ruwikiWikimediaNavigation' );
-- render title
if table.getn( interprojects ) > 1 then
body:tag( 'div' )
:addClass( 'ts-Родственные_проекты-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 '[[Категория:' .. 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
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
-- Main
function p.main( frame )
local args = getArgs( frame, { wrappers = config.params.wrappers } );
local nocat = yesno( args['nocat'] );
-- title parameter
local title = args['Тема'] or args['Заголовок'];
if title ~= nil then
title = title .. ':'
else
title = config.strings['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;