Undid revision 1212853521 by Jdlrobson (talk) no, that class is exclusive to the module in which it appears
не помогло
Строка 1: Строка 1:
-- This module implements {{shortcut}}.
require( 'strict' )
 
-- Set constants
local CONFIG_MODULE = 'Module:Shortcut/config'
 
-- Load required modules
local checkType = require('libraryUtil').checkType
local yesno = require('Module:Yesno')


local p = {}
local p = {}


local function message(msg, ...)
local getArgs = require( 'Module:Arguments' ).getArgs
return mw.message.newRawMessage(msg, ...):plain()
end


local function makeCategoryLink(cat)
local templateStylesPage = 'Module:Shortcut/styles.css'
return string.format('[[%s:%s]]', mw.site.namespaces[14].name, cat)
local errorCat = 'Википедия:Страницы с неправильно заполненным шаблоном Shortcut'
end


function p._main(shortcuts, options, frame, cfg)
local currentTitle = mw.title.getCurrentTitle()
checkType('_main', 1, shortcuts, 'table')
checkType('_main', 2, options, 'table', true)
options = options or {}
frame = frame or mw.getCurrentFrame()
cfg = cfg or mw.loadData(CONFIG_MODULE)
local templateMode = options.template and yesno(options.template)
local redirectMode = options.redirect and yesno(options.redirect)
local isCategorized = not options.category or yesno(options.category) ~= false


-- Validate shortcuts
local headerSubpage = '/шапка'
for i, shortcut in ipairs(shortcuts) do
local imageParams = { 'ВП:ВП', 'Короткая ссылка-перенаправление' }
if type(shortcut) ~= 'string' or #shortcut < 1 then
error(message(cfg['invalid-shortcut-error'], i), 2)
end
end


-- Make the list items. These are the shortcuts plus any extra lines such
local function isEmpty( val )
-- as options.msg.
return val == nil or val == ''
local listItems = {}
end
for i, shortcut in ipairs(shortcuts) do
local templatePath, prefix
if templateMode then
-- Namespace detection
local titleObj = mw.title.new(shortcut, 10)
if titleObj.namespace == 10 then
templatePath = titleObj.fullText
else
templatePath = shortcut
end
prefix = options['pre' .. i] or options.pre or ''
end
if options.target and yesno(options.target) then
listItems[i] = templateMode
and string.format("&#123;&#123;%s[[%s|%s]]&#125;&#125;", prefix, templatePath, shortcut)
or string.format("[[%s]]", shortcut)
else
listItems[i] = frame:expandTemplate{
title = 'No redirect',
args = templateMode and {templatePath, shortcut} or {shortcut, shortcut}
}
if templateMode then
listItems[i] = string.format("&#123;&#123;%s%s&#125;&#125;", prefix, listItems[i])
end
end
end
table.insert(listItems, options.msg)


-- Return an error if we have nothing to display
local function getLink( val, nocat )
if #listItems < 1 then
local category = not nocat and '[[Category:' .. errorCat .. ']]' or ''
local msg = cfg['no-content-error']
return string.format( '[[%s]]%s', val, category )
msg = string.format('<strong class="error">%s</strong>', msg)
end
if isCategorized and cfg['no-content-error-category'] then
msg = msg .. makeCategoryLink(cfg['no-content-error-category'])
end
return msg
end


local root = mw.html.create()
local function getShortcut( val, nocat )
root:wikitext(frame:extensionTag{ name = 'templatestyles', args = { src = 'Module:Shortcut/styles.css'} })
local thisTitle = mw.title.makeTitle( '', val )
-- Anchors
local anchorDiv = root
-- No error category on header subpages
:tag('div')
if mw.ustring.find( mw.ustring.lower( currentTitle.prefixedText ), headerSubpage ) then
:addClass('module-shortcutanchordiv')
nocat = true
for i, shortcut in ipairs(shortcuts) do
local anchor = mw.uri.anchorEncode(shortcut)
anchorDiv:tag('span'):attr('id', anchor)
end
end
 
-- Shortcut heading
if not thisTitle then
local shortcutHeading
return getLink( val, nocat )
do
local nShortcuts = #shortcuts
if nShortcuts > 0 then
local headingMsg = options['shortcut-heading'] or
redirectMode and cfg['redirect-heading'] or
cfg['shortcut-heading']
shortcutHeading = message(headingMsg, nShortcuts)
shortcutHeading = frame:preprocess(shortcutHeading)
end
end
end
 
-- Shortcut box
local doesNotEqualCurrent = not mw.title.equals( thisTitle, currentTitle )
local shortcutList = root
if doesNotEqualCurrent and ( not thisTitle.redirectTarget or not mw.title.equals( thisTitle.redirectTarget, currentTitle ) ) then
:tag('div')
return getLink( val, nocat )
:addClass('module-shortcutboxplain noprint')
:attr('role', 'note')
if options.float and options.float:lower() == 'left' then
shortcutList:addClass('module-shortcutboxleft')
end
if options.clear and options.clear ~= '' then
shortcutList:css('clear', options.clear)
end
if shortcutHeading then
shortcutList
:tag('div')
:addClass('module-shortcutlist')
:wikitext(shortcutHeading)
end
end
local ubl = require('Module:List').unbulleted(listItems)
return string.format( '%s', val )
shortcutList:wikitext(ubl)
return tostring(root)
end
end


function p.main(frame)
local function parseList( args, element, anchors, nocat )
local args = require('Module:Arguments').getArgs(frame)
local list = mw.html.create( 'ul' )
 
local listIsEmpty = true
-- Separate shortcuts from options
for key, val in ipairs( args ) do
local shortcuts, options = {}, {}
local title = mw.ustring.gsub( mw.ustring.gsub( mw.text.trim( val ), "%[%[","" ), "%]%]", "" )
for k, v in pairs(args) do
if title ~= '' then
if type(k) == 'number' then
list:tag( 'li' ):wikitext( getShortcut( title, nocat ) )
shortcuts[k] = v
if anchors then
else
anchors:tag( 'span' ):attr( 'id', title )
options[k] = v
end
listIsEmpty = false
end
end
end
end
element:node( list )
if listIsEmpty and not nocat then
element:wikitext( '[[Category:' .. errorCat .. ']]' )
end
end


-- Compress the shortcut array, which may contain nils.
function p.indicator( frame )
local function compressArray(t)
local args = getArgs( frame )
local nums, ret = {}, {}
local nocat = args[ 'nocat' ] or false
for k in pairs(t) do
nums[#nums + 1] = k
if not isEmpty( args[ 1 ] ) and isEmpty( args[ '2' ] ) then
end
args = mw.text.split( args[ 1 ], '/' )
table.sort(nums)
for i, num in ipairs(nums) do
ret[i] = t[num]
end
return ret
end
end
shortcuts = compressArray(shortcuts)
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


return p._main(shortcuts, options, frame)
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
end


return p
return p

Версия от 18:00, 16 августа 2025

Для документации этого модуля может быть создана страница Модуль: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