Модуль:Надстрочное предупреждение: различия между версиями
Перейти к навигации
Перейти к поиску
(корректная обработка комментариев) |
(fix) |
||
Строка 1: | Строка 1: | ||
− | require( 'strict' ) | + | require( 'strict' ) |
local p = {} | local p = {} | ||
Строка 24: | Строка 24: | ||
-- Форматирование выделяемого шаблоном текста | -- Форматирование выделяемого шаблоном текста | ||
local function getSpanText( text, style ) | local function getSpanText( text, style ) | ||
− | style = style or 'background:#ffeaea; color:#444444;' | + | style = style or 'background:#ffeaea; color:#444444;' |
local tag = mw.html.create( 'span' ) | local tag = mw.html.create( 'span' ) | ||
Строка 45: | Строка 45: | ||
local function getLink( link, text, comment ) | local function getLink( link, text, comment ) | ||
if not isEmpty( comment ) then | if not isEmpty( comment ) then | ||
− | local delink = require( 'Module:Delink' )._delink | + | local delink = require( 'Module:Delink' )._delink |
comment = delink( { comment } ) | comment = delink( { comment } ) | ||
if comment then | if comment then | ||
local html = mw.html.create( 'span' ) | local html = mw.html.create( 'span' ) | ||
:attr( 'style', 'border-bottom:1px dotted;' ) | :attr( 'style', 'border-bottom:1px dotted;' ) | ||
− | :attr( 'title', | + | :attr( 'title', comment ) |
:wikitext( text ) | :wikitext( text ) | ||
Версия от 03:51, 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
if tonumber( 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 not isEmpty( comment ) then
local delink = require( 'Module:Delink' )._delink
comment = delink( { comment } )
if comment then
local html = mw.html.create( 'span' )
:attr( 'style', 'border-bottom:1px dotted;' )
:attr( 'title', comment )
:wikitext( text )
text = tostring( html )
end
end
if link then
return string.format( '<i>[[%s|%s]]</i>', 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