Модуль:Надстрочное предупреждение: различия между версиями
Перейти к навигации
Перейти к поиску
(вроде теперь должно в базовом виде работать) |
(вроде бы вышел на финишную прямую) |
||
Строка 1: | Строка 1: | ||
require( 'strict' ); | require( 'strict' ); | ||
local p = {} | local p = {} | ||
+ | |||
+ | local mwLang = mw.getContentLanguage() | ||
+ | |||
+ | local function isEmpty( val ) | ||
+ | return val == nil or val == '' | ||
+ | end | ||
+ | |||
+ | -- Игнорирование некорректно указанных дат с помощью стандартных методов | ||
+ | local function getValidDate( day, month, year ) | ||
+ | local dateString = year .. '-' .. month .. '-' .. day | ||
+ | |||
+ | local success, result = pcall(mwLang.formatDate, mwLang, 'U', dateString) | ||
+ | if success then | ||
+ | result = tonumber( result ) | ||
+ | if result then | ||
+ | return result | ||
+ | end | ||
+ | end | ||
+ | |||
+ | return nil | ||
+ | end | ||
-- Форматирование выделяемого шаблоном текста | -- Форматирование выделяемого шаблоном текста | ||
Строка 14: | Строка 35: | ||
-- Форматирование даты как в шаблоне | -- Форматирование даты как в шаблоне | ||
− | local function getDate( | + | local function getDate( date ) |
− | + | if isEmpty( date ) then | |
+ | return nil | ||
+ | end | ||
− | return | + | return mwLang:formatDate( ' (j xg Y)', date ) |
end | end | ||
Строка 40: | Строка 63: | ||
-- Форматирование ссылки на обсуждение как в шаблоне | -- Форматирование ссылки на обсуждение как в шаблоне | ||
− | local function getTalkLink( page ) | + | local function getTalkLink( page, noprint ) |
if not page then return '' end | if not page then return '' end | ||
Строка 48: | Строка 71: | ||
end | end | ||
− | + | local result = ' ([[%s|обс.]]' | |
+ | if isEmpty( noprint ) or not noprint then | ||
+ | local html = mw.html.create( 'span' ) | ||
+ | :addClass( 'noprint' ) | ||
+ | :wikitext( result ) | ||
+ | |||
+ | result = tostring( html ) | ||
+ | end | ||
+ | return string.format( result, page ) | ||
end | end | ||
− | -- | + | -- Простановка категорий |
+ | local function getCategory( category, config, date ) | ||
+ | if isEmpty( date ) then | ||
+ | return nil | ||
+ | end | ||
+ | |||
+ | if isEmpty( category ) then | ||
+ | return nil | ||
+ | end | ||
+ | |||
+ | config = ' ' .. ( config or '>= 0' ) | ||
+ | local mDate = require( 'Module:Date' )._Date | ||
+ | local today = mwLang:formatDate( 'Y-m-d H:i:s' ) | ||
+ | local input = mwLang:formatDate( 'Y-m-d H:i:s', date ) | ||
+ | local diff = ( mDate( today ) - mDate( input ) ) | ||
+ | |||
+ | local success, result = pcall( mw.ext.ParserFunctions.expr, diff .. config ) | ||
+ | if success and result == '1' then | ||
+ | return category | ||
+ | end | ||
+ | |||
+ | return nil | ||
+ | end | ||
+ | |||
+ | local function getError( comment, anchor ) | ||
+ | local html = mw.html.create( 'strong' ) | ||
+ | :addClass( 'error noprint' ) | ||
+ | :wikitext( string.format( '[[Шаблон:Надстрочное предупреждение#%s|Ошибка:]] %s', anchor, comment ) ) | ||
+ | |||
+ | return tostring( html ) | ||
+ | end | ||
+ | |||
+ | -- Поддержка подстановки без Unsubst | ||
function p.subst( frame ) | function p.subst( frame ) | ||
local mTemplateInvocation = require( 'Module:Template invocation' ) | local mTemplateInvocation = require( 'Module:Template invocation' ) | ||
Строка 63: | Строка 126: | ||
-- | -- | ||
function p.main( frame ) | function p.main( frame ) | ||
− | local getArgs = require( 'Module:Arguments' ).getArgs | + | local getArgs = require( 'Module:Arguments' ).getArgs |
− | local args = getArgs( frame ) | + | local args = getArgs( frame ) |
− | local result = '' | + | local date = getValidDate( args.day, args.month, args.year ) |
+ | local isMainNamespace = mw.title.getCurrentTitle().namespace == 0 | ||
+ | local result = '' | ||
-- Поддержка подстановки | -- Поддержка подстановки | ||
Строка 72: | Строка 137: | ||
end | end | ||
− | -- Вывод ошибки | + | -- Вывод надстрочного предупреждения |
− | if | + | local tag = mw.html.create( 'sup' ) |
− | + | :attr( 'style', 'white-space:nowrap' ) | |
+ | |||
+ | if not isEmpty( args.noprint ) then | ||
+ | tag:addClass( 'noprint' ) | ||
+ | end | ||
+ | |||
+ | -- Вывод ошибки о параметре text | ||
+ | if isEmpty( args.text ) then | ||
+ | result = getError( 'не задан параметр <code>text</code>', 'Использование' ) | ||
+ | tag:wikitext( result ) | ||
+ | |||
+ | return tostring( tag ) | ||
end | end | ||
Строка 83: | Строка 159: | ||
-- Поддержка параметра {{{comment|}}} | -- Поддержка параметра {{{comment|}}} | ||
− | local comment = args.comment or args[ 'comment-default' ] | + | local comment = args.comment or args[ 'comment-default' ] or args.link |
− | + | comment = comment .. getDate( date ) | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
tag | tag | ||
:wikitext( '[' ) -- [ | :wikitext( '[' ) -- [ | ||
:wikitext( getLink( args.link, args.text, comment ) ) | :wikitext( getLink( args.link, args.text, comment ) ) | ||
− | :wikitext( getTalkLink( args.talk ) ) | + | :wikitext( getTalkLink( args.talk, args.noprint ) ) |
:wikitext( ']' ) -- ] | :wikitext( ']' ) -- ] | ||
result = result .. tostring( tag ) | result = result .. tostring( tag ) | ||
− | -- | + | -- Проверка для [[Категория:Википедия:Надстрочные предупреждения с некорректно заданной датой]] |
+ | local errorcat = args.errorcat or '[[Категория:Википедия:Надстрочные предупреждения с некорректно заданной датой]]' | ||
+ | if not isEmpty( args.day ) or not isEmpty( args.month ) or not isEmpty( args.year ) then | ||
+ | if isEmpty( date ) then | ||
+ | result = getError( 'некорректно задана дата установки', 'Дата установки' ) | ||
+ | |||
+ | if isMainNamespace then | ||
+ | result = result .. errorcat | ||
+ | end | ||
+ | end | ||
+ | end | ||
-- Поддержка параметра {{{anchor|}}} | -- Поддержка параметра {{{anchor|}}} | ||
Строка 108: | Строка 188: | ||
end | end | ||
− | -- | + | -- Установка категорий |
− | if args.nocat | + | if isEmpty( args.nocat ) and isMainNamespace then |
− | + | result = result .. getCategory( args[ 'cat1' ], args[ 'cat1-date' ], date ) | |
+ | result = result .. getCategory( args[ 'cat2' ], args[ 'cat2-date' ], date ) | ||
+ | result = result .. getCategory( args[ 'cat3' ], args[ 'cat3-date' ], date ) | ||
end | end | ||
Версия от 03:35, 16 мая 2023
Для документации этого модуля может быть создана страница Модуль:Надстрочное предупреждение/doc
require( 'strict' );
local p = {}
local mwLang = mw.getContentLanguage()
local function isEmpty( val )
return val == nil or val == ''
end
-- Игнорирование некорректно указанных дат с помощью стандартных методов
local function getValidDate( day, month, year )
local dateString = year .. '-' .. month .. '-' .. day
local success, result = pcall(mwLang.formatDate, mwLang, 'U', dateString)
if success then
result = tonumber( result )
if result then
return result
end
end
return nil
end
-- Форматирование выделяемого шаблоном текста
local function getSpanText( text, style )
style = style or 'background:#ffeaea; color:#444444;';
local tag = mw.html.create( 'span' )
:attr( 'style', style )
:wikitext( text )
return tostring( tag )
end
-- Форматирование даты как в шаблоне
local function getDate( date )
if isEmpty( date ) then
return nil
end
return mwLang:formatDate( ' (j xg Y)', date )
end
-- Форматирование ссылок как в шаблоне
local function getLink( link, text, comment )
if comment then
local delink = require( 'Module:Delink' )._delink;
local tag = mw.html.create( 'span' )
:attr( 'style', 'border-bottom:1px dotted;' )
:attr( 'title', delink( comment ) )
:wikitext( text )
text = tostring( tag )
end
if link then
return string.format( '[[%s|%s]]', link, text )
end
return text
end
-- Форматирование ссылки на обсуждение как в шаблоне
local function getTalkLink( page, noprint )
if not page then return '' end
-- Страница обсуждения для текущей статьи в случае отсутствия якоря
if not mw.ustring.find( page, '#' ) then
page = mw.title.getCurrentTitle().talkPageTitle .. '#' .. page
end
local result = ' ([[%s|обс.]]'
if isEmpty( noprint ) or not noprint then
local html = mw.html.create( 'span' )
:addClass( 'noprint' )
:wikitext( result )
result = tostring( html )
end
return string.format( result, page )
end
-- Простановка категорий
local function getCategory( category, config, date )
if isEmpty( date ) then
return nil
end
if isEmpty( category ) then
return nil
end
config = ' ' .. ( config or '>= 0' )
local mDate = require( 'Module:Date' )._Date
local today = mwLang:formatDate( 'Y-m-d H:i:s' )
local input = mwLang:formatDate( 'Y-m-d H:i:s', date )
local diff = ( mDate( today ) - mDate( input ) )
local success, result = pcall( mw.ext.ParserFunctions.expr, diff .. config )
if success and result == '1' then
return category
end
return nil
end
local function getError( comment, anchor )
local html = mw.html.create( 'strong' )
:addClass( 'error noprint' )
:wikitext( string.format( '[[Шаблон:Надстрочное предупреждение#%s|Ошибка:]] %s', anchor, comment ) )
return tostring( html )
end
-- Поддержка подстановки без Unsubst
function p.subst( frame )
local mTemplateInvocation = require( 'Module:Template invocation' )
local name = mTemplateInvocation.name( frame:getParent():getTitle() )
return mTemplateInvocation.invocation( name )
end
--
-- Модуль на замену шаблону «Надстрочное предупреждение»
--
function p.main( frame )
local getArgs = require( 'Module:Arguments' ).getArgs
local args = getArgs( frame )
local date = getValidDate( args.day, args.month, args.year )
local isMainNamespace = mw.title.getCurrentTitle().namespace == 0
local result = ''
-- Поддержка подстановки
if mw.isSubsting() then
return p.subst( frame )
end
-- Вывод надстрочного предупреждения
local tag = mw.html.create( 'sup' )
:attr( 'style', 'white-space:nowrap' )
if not isEmpty( args.noprint ) then
tag:addClass( 'noprint' )
end
-- Вывод ошибки о параметре text
if isEmpty( args.text ) then
result = getError( 'не задан параметр <code>text</code>', 'Использование' )
tag:wikitext( result )
return tostring( tag )
end
-- Поддержка параметра {{{span-text|}}}
if args[ 'span-text' ] then
result = result .. getSpanText( args[ 'span-text' ], args[ 'span-style' ] )
end
-- Поддержка параметра {{{comment|}}}
local comment = args.comment or args[ 'comment-default' ] or args.link
comment = comment .. getDate( date )
tag
:wikitext( '[' ) -- [
:wikitext( getLink( args.link, args.text, comment ) )
:wikitext( getTalkLink( args.talk, args.noprint ) )
:wikitext( ']' ) -- ]
result = result .. tostring( tag )
-- Проверка для [[Категория:Википедия:Надстрочные предупреждения с некорректно заданной датой]]
local errorcat = args.errorcat or '[[Категория:Википедия:Надстрочные предупреждения с некорректно заданной датой]]'
if not isEmpty( args.day ) or not isEmpty( args.month ) or not isEmpty( args.year ) then
if isEmpty( date ) then
result = getError( 'некорректно задана дата установки', 'Дата установки' )
if isMainNamespace then
result = result .. errorcat
end
end
end
-- Поддержка параметра {{{anchor|}}}
if args.anchor then
local anchor = require( 'Module:Якорь' ).main;
result = anchor{ visible = true, text = result, args.anchor }
end
-- Установка категорий
if isEmpty( args.nocat ) and isMainNamespace then
result = result .. getCategory( args[ 'cat1' ], args[ 'cat1-date' ], date )
result = result .. getCategory( args[ 'cat2' ], args[ 'cat2-date' ], date )
result = result .. getCategory( args[ 'cat3' ], args[ 'cat3-date' ], date )
end
return result
end
return p