м 1 версия импортирована
м 1 версия импортирована
 
(не показаны 3 промежуточные версии 3 участников)
Строка 1: Строка 1:
require( 'strict' )
local p = {}
local p = {}


function p.main (frame)
local getArgs = require( 'Module:Arguments' ).getArgs
local title = mw.ustring.gsub(mw.ustring.gsub(mw.text.trim(frame.args[1]),"%[%[",""),"%]%]","")
 
local currentTitle = mw.title.getCurrentTitle().prefixedText
local templateStylesPage = 'Module:Shortcut/styles.css'
if title ~= '' then
local errorCat = 'Википедия:Страницы с неправильно заполненным шаблоном Shortcut'
local res = title
 
local page = mw.title.makeTitle('', title)
local currentTitle = mw.title.getCurrentTitle()
 
local headerSubpage = '/шапка'
local imageParams = { 'ВП:ВП', 'Короткая ссылка-перенаправление' }
 
local function isEmpty( val )
return val == nil or val == ''
end
 
local function getLink( val, nocat )
local category = not nocat and '[[Category:' .. errorCat .. ']]' or ''
return string.format( '[[%s]]%s', val, category )
end
 
local function getShortcut( val, nocat )
local thisTitle = mw.title.makeTitle( '', val )
-- No error category on header subpages
if mw.ustring.find( mw.ustring.lower( currentTitle.prefixedText ), headerSubpage ) then
nocat = true
end
if not thisTitle then
return getLink( val, nocat )
end
local doesNotEqualCurrent = not mw.title.equals( thisTitle, currentTitle )
if doesNotEqualCurrent and ( not thisTitle.redirectTarget or not mw.title.equals( thisTitle.redirectTarget, currentTitle ) ) then
return getLink( val, nocat )
end
return string.format( '%s', val )
end
 
local function parseList( args, element, anchors, nocat )
local list = mw.html.create( 'ul' )
local listIsEmpty = true
for key, val in ipairs( args ) do
local title = mw.ustring.gsub( mw.ustring.gsub( mw.text.trim( val ), "%[%[","" ), "%]%]", "" )
if title ~= '' then
list:tag( 'li' ):wikitext( getShortcut( title, nocat ) )
if anchors then
anchors:tag( 'span' ):attr( 'id', title )
end
listIsEmpty = false
end
end
element:node( list )
if listIsEmpty and not nocat then
element:wikitext( '[[Category:' .. errorCat .. ']]' )
end
end
 
function p.indicator( frame )
local args = getArgs( frame )
local nocat = args[ 'nocat' ] or false
if not isEmpty( args[ 1 ] ) and isEmpty( args[ '2' ] ) then
args = mw.text.split( args[ 1 ], '/' )
end
local result = mw.html.create( 'div' )
:addClass( 'ts-shortcut-indicator hlist inline' )
:wikitext( '[[File:OOjs UI icon articleRedirect-ltr-progressive.svg|20px|link=' .. table.concat( imageParams, '|' ) .. ']]' )
:wikitext( ' ' )
parseList( args, result, nil, nocat )
local content = frame:extensionTag{
name = 'templatestyles',
args = { src = templateStylesPage },
} .. tostring( result )
return frame:extensionTag{
name = 'indicator',
content = content,
args = { name = '0-shortcut' },
}
end
 
function p.shortcut( frame )
local args = getArgs( frame )
local style = args[ 'style' ] or nil
local nocat = args[ 'nocat' ] or false
local result = mw.html.create( 'div' )
:addClass( 'ts-shortcut noprint' )
:attr( 'role', 'note' )
:attr( 'style', style )
local anchors = result:tag( 'div' )
:addClass( 'ts-shortcut-anchors' )
result:tag( 'div' )
if not page or (page.prefixedText ~= currentTitle and (not page.redirectTarget or page.redirectTarget.prefixedText ~= currentTitle)) then
:addClass( 'ts-shortcut-image' )
res = '<span style="color:red;">[[' .. res .. ']]</span>[[Категория:Википедия:Страницы с неправильно заполненным шаблоном Shortcut]]'
:wikitext( '[[File:OOjs UI icon articleRedirect-ltr-progressive.svg|20px|link=' .. table.concat( imageParams, '|' ) .. ']]' )
end
return res -- .. ' => ' .. page.redirectTarget.prefixedText .. ' vs. ' .. currentTitle
local shortcuts = result:tag( 'div' )
end
:addClass( 'ts-shortcut-list hlist' )
parseList( args, shortcuts, anchors, nocat )
return frame:extensionTag{
name = 'templatestyles',
args = { src = templateStylesPage },
} .. tostring( result )
end
end


return p
return p

Текущая версия от 17:04, 22 июня 2026

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

require( 'strict' )

local p = {}

local getArgs = require( 'Module:Arguments' ).getArgs

local templateStylesPage = 'Module:Shortcut/styles.css'
local errorCat = 'Википедия:Страницы с неправильно заполненным шаблоном Shortcut'

local currentTitle = mw.title.getCurrentTitle()

local headerSubpage = '/шапка'
local imageParams = { 'ВП:ВП', 'Короткая ссылка-перенаправление' }

local function isEmpty( val )
	return val == nil or val == ''
end

local function getLink( val, nocat )
	local category = not nocat and '[[Category:' .. errorCat .. ']]' or ''
	return string.format( '[[%s]]%s', val, category )
end

local function getShortcut( val, nocat )
	local thisTitle = mw.title.makeTitle( '', val )
	
	-- No error category on header subpages
	if mw.ustring.find( mw.ustring.lower( currentTitle.prefixedText ), headerSubpage ) then
		nocat = true
	end
	
	if not thisTitle then
		return getLink( val, nocat )
	end
	
	local doesNotEqualCurrent = not mw.title.equals( thisTitle, currentTitle )
	if doesNotEqualCurrent and ( not thisTitle.redirectTarget or not mw.title.equals( thisTitle.redirectTarget, currentTitle ) ) then
		return getLink( val, nocat )
	end
	
	return string.format( '%s', val )
end

local function parseList( args, element, anchors, nocat )
	local list = mw.html.create( 'ul' )
	local listIsEmpty = true
	for key, val in ipairs( args ) do
		local title = mw.ustring.gsub( mw.ustring.gsub( mw.text.trim( val ), "%[%[","" ), "%]%]", "" )
		if title ~= '' then
			list:tag( 'li' ):wikitext( getShortcut( title, nocat ) )
			if anchors then
				anchors:tag( 'span' ):attr( 'id', title )
			end
			listIsEmpty = false
		end
	end
	
	element:node( list )
	if listIsEmpty and not nocat then
		element:wikitext( '[[Category:' .. errorCat .. ']]' )
	end
end

function p.indicator( frame )
	local args = getArgs( frame )
	local nocat = args[ 'nocat' ] or false
	
	if not isEmpty( args[ 1 ] ) and isEmpty( args[ '2' ] ) then
		args = mw.text.split( args[ 1 ], '/' )
	end
	
	local result = mw.html.create( 'div' )
		:addClass( 'ts-shortcut-indicator hlist inline' )
		:wikitext( '[[File:OOjs UI icon articleRedirect-ltr-progressive.svg|20px|link=' .. table.concat( imageParams, '|' ) .. ']]' )
		:wikitext( ' ' )
	
	parseList( args, result, nil, nocat )
	
	local content = frame:extensionTag{
		name = 'templatestyles',
		args = { src = templateStylesPage },
	} .. tostring( result )
	return frame:extensionTag{
		name = 'indicator',
		content = content,
		args = { name = '0-shortcut' },
	}
end

function p.shortcut( frame )
	local args = getArgs( frame )
	local style = args[ 'style' ] or nil
	local nocat = args[ 'nocat' ] or false
	
	local result = mw.html.create( 'div' )
		:addClass( 'ts-shortcut noprint' )
		:attr( 'role', 'note' )
		:attr( 'style', style )
		
	local anchors = result:tag( 'div' )
		:addClass( 'ts-shortcut-anchors' )
		
	result:tag( 'div' )
		:addClass( 'ts-shortcut-image' )
		:wikitext( '[[File:OOjs UI icon articleRedirect-ltr-progressive.svg|20px|link=' .. table.concat( imageParams, '|' ) .. ']]' )
	
	local shortcuts = result:tag( 'div' )
		:addClass( 'ts-shortcut-list hlist' )
	parseList( args, shortcuts, anchors, nocat )
	
	return frame:extensionTag{
		name = 'templatestyles',
		args = { src = templateStylesPage },
	} .. tostring( result )
end

return p