Модуль:Wikidata: различия между версиями

Новая страница: «Test»
/>Vlsergey
(Новая страница: «Test»)
/>Vlsergey
(Новая страница: «Test»)
Строка 45: Строка 45:


local formatDatavalue, formatEntityId, formatRefs, formatSnak, formatStatement, formatStatementDefault, formatProperty, getSourcingCircumstances, loadCacheSafe, throwError, toBoolean;
local formatDatavalue, formatEntityId, formatRefs, formatSnak, formatStatement, formatStatementDefault, formatProperty, getSourcingCircumstances, loadCacheSafe, throwError, toBoolean;
local function copyTo( obj, target )
for k, v in pairs( obj ) do
target[k] = v
end
return target;
end


--[[  
--[[  
Строка 220: Строка 227:
     end
     end


local context = { frame = g_frame, entity = entity, options = options, formatSnak = formatSnak, formatStatementDefault = formatStatementDefault }
local context = { frame = g_frame, entity = entity, options = options, formatSnak = formatSnak, formatPropertyDefault = formatPropertyDefault, formatStatementDefault = formatStatementDefault }
context.formatProperty = function( )  
context.formatProperty = function( )  
local func = getUserFunction( context, 'property', context.formatPropertyDefault );
local func = getUserFunction( context, 'property', context.formatPropertyDefault );
return func( context )
return func( context )
end;
end;
context.formatPropertyDefault = function( ) return formatPropertyDefault( context ) end;
context.formatStatement = function( statement ) return formatStatement( context, statement ) end;
context.formatStatement = function( statement ) return formatStatement( context, statement ) end;
context.formatSnak = function( snak, options, circumstances ) return formatSnak( context, snak, options, circumstances ) end;
context.formatSnak = function( snak, options, circumstances ) return formatSnak( context, snak, options, circumstances ) end;
Строка 232: Строка 238:
context.selectClaims = function( propertyId ) return selectClaims( context, propertyId ) end;
context.selectClaims = function( propertyId ) return selectClaims( context, propertyId ) end;


-- duplicate 18n constants to have an options to override them
context.extendsOptions = function( newOptions )
context.i18n = {};
local newContext = copyTo( context, {} );
for key, value in pairs( i18n ) do
newContext.options = copyTo( newOptions, copyTo( context.options, {} ) );
context.i18n[ key ] = value;
return newContext
end
end
 
if ( options.i18n ) then
if ( options.i18n ) then
for key, value in pairs( options.i18n ) do
context.i18n = copyTo( options.i18n, copyTo( i18n, {} ) );
context.i18n[ key ] = value;
else
end
context.i18n = i18n;
end
end


return context.formatPropertyDefault( options.property );
return context.formatProperty();
end
end


function formatPropertyDefault( context, propertyId )
function formatPropertyDefault( context )
local options = context.options;
local options = context.options;
     local claims = context.selectClaims( options.property );
     local claims = context.selectClaims( options.property );
Строка 367: Строка 374:
end
end


         return before .. formatDatavalue( snak.datavalue, options ) .. after;
         return before .. formatDatavalue( context, options, snak.datavalue ) .. after;
     else
     else
         return before .. throwError( 'unknown-snak-type' ) .. after;
         throwError( 'unknown-snak-type' );
     end
     end
end
end
Строка 426: Строка 433:
end
end


local function getDefaultDataValueFunction( datavalue )
local function getDefaultValueFunction( datavalue )
     -- вызов обработчиков по умолчанию для известных типов значений
     -- вызов обработчиков по умолчанию для известных типов значений
     if datavalue.type == 'wikibase-entityid' then
     if datavalue.type == 'wikibase-entityid' then
     -- идентификатор сущности
     -- идентификатор сущности
         return function( value, options) return formatEntityId( getEntityIdFromValue( value ), options ) end;
         return function( context, options, value ) return formatEntityId( getEntityIdFromValue( value ), options ) end;
     elseif datavalue.type == 'string' then
     elseif datavalue.type == 'string' then
     -- строка
     -- строка
         return function( value, options) return value end;
         return function( context, options, value ) return value end;
     elseif datavalue.type == 'monolingualtext' then
     elseif datavalue.type == 'monolingualtext' then
     -- моноязычный текст (строка с указанием языка)
     -- моноязычный текст (строка с указанием языка)
         return function( value, options) return '<span class="lang" lang="' .. value.language .. '">' .. value.text .. '</span>' end;
         return function( context, options, value ) return '<span class="lang" lang="' .. value.language .. '">' .. value.text .. '</span>' end;
     elseif datavalue.type == 'globecoordinate' then
     elseif datavalue.type == 'globecoordinate' then
     -- географические координаты
     -- географические координаты
         return function( value, options) return formatGlobeCoordinate( value, options )  end;
         return function( context, options, value ) return formatGlobeCoordinate( value, options )  end;
     elseif datavalue.type == 'quantity' then
     elseif datavalue.type == 'quantity' then
         return function( value, options)
         return function( context, options, value )
    -- диапазон значений
    -- диапазон значений
        local amount = string.gsub(value['amount'], '^%+', '')
        local amount = string.gsub(value['amount'], '^%+', '')
Строка 448: Строка 455:
         end;
         end;
     elseif datavalue.type == 'time' then
     elseif datavalue.type == 'time' then
         return function( value, options)
         return function( context, options, value )
local moduleDate = require('Module:Wikidata/date')
local moduleDate = require('Module:Wikidata/date')
    return moduleDate.formatDate( value, options );
    return moduleDate.formatDate( context, options, value );
         end;
         end;
     else
     else
Строка 465: Строка 472:
   Возвращает: строку оформленного текста
   Возвращает: строку оформленного текста
]]
]]
function formatDatavalue( datavalue, options )
function formatDatavalue( context, options, datavalue )
if ( not context ) then error( 'context not specified' ); end;
if ( not options ) then error( 'options not specified' ); end;
if ( not datavalue ) then error( 'datavalue not specified' ); end;
if ( not datavalue.value ) then error( 'datavalue.value is missng' ); end;
 
     -- проверка на указание специализированных обработчиков в параметрах,
     -- проверка на указание специализированных обработчиков в параметрах,
     -- переданных при вызове
     -- переданных при вызове
     local functionToCall = getUserFunction( options, 'value', getDefaultDataValueFunction( datavalue ) );
    context.formatValueDefault = getDefaultValueFunction( datavalue );
     return functionToCall( datavalue.value, options );
     local functionToCall = getUserFunction( options, 'value', context.formatValueDefault );
     return functionToCall( context, options, datavalue.value );
end
end


Анонимный участник