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

Материал из FetbukWiki
https://ru.wikipedia.org/wiki/>Jack who built the house
(-избыточный код)
м (1 версия импортирована)
 
(не показана 1 промежуточная версия 1 участника)
Строка 13: Строка 13:
end
end


local function makeInvokeFunc(funcName, flags)
local function makeInvokeFunc(funcName)
return function (frame)
return function (frame)
local args = copy(getArgs(frame, {
local args = copy(getArgs(frame, {
Строка 19: Строка 19:
removeBlanks = false
removeBlanks = false
}))
}))
return p[funcName](args, flags)
return p[funcName](args)
end
end
end
end


--предотвращает обработку вики-текста в отображении образца
p.withoutParams = makeInvokeFunc('_withoutParams')
local function processText(str, nowiki)
 
local res = str
function p._withoutParams(args)
if nowiki then
local name = args[1]
str = mw.text.unstripNoWiki(str)
table.remove(args, 1)
str = string.gsub(str, '%[', '[')
str = string.gsub(str, '%]', ']')
-- Вещи типа «=» в первом параметре
str = string.gsub(str, '<', '&lt;')
if not name then
str = string.gsub(str, '>', '&gt;')
for k, v in pairs(args) do
str = string.gsub(str, '{', '&#123;')
if not k:find('^_') then
str = string.gsub(str, '|', '&#124;')
name = k .. '=' .. v
str = string.gsub(str, '}', '&#125;')
args[k] = nil
str = string.gsub(str, '\'', '&#39;')
break
str = string.gsub(str, '"', '&quot;')
end
str = string.gsub(str, '(://)', '<span>%1</span>')
end
end
local flags = {}
for i, v in ipairs(args) do
if v == 'nl' or v == 'nolink' then
flags.noLink = true
elseif v == 's' then
flags.subst = true
elseif v == 'п' then
flags.podst = true
elseif v == 'g' then
flags.global = true
elseif v == 'nav' then
flags.nav = true
elseif v == 'noredir' then
flags.noRedirect = true
elseif v == 'u' then
flags.ucFirst = true
elseif v == 'b' then
flags.black = true
end
end
if name then
local trimmedName = mw.text.trim(name)
if ru:lc(mw.ustring.sub(trimmedName, 1, 6)) == 'subst:' then
flags.subst = true
name = mw.ustring.sub(trimmedName, 7)
end
if ru:lc(mw.ustring.sub(trimmedName, 1, 6)) == 'подст:' then
flags.podst = true
name = mw.ustring.sub(trimmedName, 7)
end
end
end
return str
if args.text == '' then
args.text = nil
end
if args.comment == '' then
args.comment = nil
end
if args.lang == '' then
args.lang = nil
end
if args.sister == '' then
args.sister = nil
end
local currentTitle = mw.title.getCurrentTitle()
-- При опущенном первом параметре берём имя шаблона из названия страницы
if name == '' or not name then
local currentTitleRoot = currentTitle.rootText
if not flags.ucFirst and
((ru:uc(currentTitleRoot) ~= currentTitleRoot and
-- Книга:Литературное наследство, TranslateDate
not mw.ustring.match(currentTitleRoot, '^[А-Яа-яA-Za-z]+:?[А-ЯA-Z]')
) or
#currentTitleRoot == 1
)
then
name = ru:lcfirst(currentTitleRoot)
else
name = currentTitleRoot
end
end
local global = flags.global or mw.ustring.sub(name, 1, 1) == ':'
-- Начинаем собирать код
local linkBody, titleObject, linkBegin, linkDivider, linkEnd
local prefixes = {}
if args.lang then
table.insert(prefixes, args.lang)
end
if args.sister then
table.insert(prefixes, args.sister)
end
linkBody = table.concat(prefixes, ':')
if #linkBody ~= 0 then
linkBody = ':' .. linkBody
end
if mw.ustring.sub(name, 1, 1) ~= ':' then
linkBody = linkBody .. ':'
end
if not global then
linkBody = linkBody .. 'Template:'
end
linkBody = linkBody .. name
titleObject = mw.title.new(linkBody)
local noLink = flags.noLink or currentTitle == titleObject
local takeBracketsInLink = not noLink and
mw.ustring.len(name) == 1 and
not flags.black and
not flags.subst and
not flags.podst
if not noLink then
if not flags.noRedirect or (
flags.noRedirect and
not args.lang and
not args.sister and
not titleObject.exists
) then
linkBegin = '[['
linkEnd = ']]'
linkDivider = '|'
else
linkBegin = '['
linkEnd = ']'
linkDivider = ' '
linkBody = titleObject:fullUrl('redirect=no')
end
end
local text = ''
if flags.nav and currentTitle == titleObject then
text = text .. '\'\'\''
end
if not flags.black then
text = text .. '<span class="wp-templatelink">'
end
text = text .. '{'
if not takeBracketsInLink then
text = text .. '{'
end
if flags.subst then
text = text .. 'subst:'
elseif flags.podst then
text = text .. 'подст:'
end
if not flags.black then
text = text .. '</span>'
end
text = text .. '<span data-navboxnavigation-link="0">'
local commentedLabel
if args.comment then
-- https://phabricator.wikimedia.org/T200704
-- commentedLabel = mw.getCurrentFrame():expandTemplate({title = 'comment', args = {(args.text or name), args.comment}})
commentedLabel = '<span class="commentedText" title="' .. args.comment .. '" style="border-bottom: 1px dotted; cursor: help;">' ..
(args.text or name) ..
'</span>'
end
local label = (commentedLabel or args.text or name)
if not noLink then
if flags.noRedirect then
text = text .. '<span class="plainlinks">'
end
text = text .. linkBegin .. linkBody .. linkDivider
if not noLink and takeBracketsInLink then
text = text .. '<span class="wp-templatelink">{</span>'
end
text = text .. label
if not noLink and takeBracketsInLink then
text = text .. '<span class="wp-templatelink">}</span>'
end
text = text .. linkEnd
if flags.noRedirect then
text = text .. '</span>'
end
else
text = text .. label
end
text = text .. '</span>'
if not flags.black then
text = text .. '<span class="wp-templatelink">'
end
text = text .. '}'
if not takeBracketsInLink then
text = text .. '}'
end
if not flags.black then
text = text .. '</span>'
end
if flags.nav and currentTitle == titleObject then
text = text .. '\'\'\''
end
return text
end
end


local function addParams(args, params)
function addParams(args, params)
local text, equals_pos, param, value = '', 0, '', ''
local text, equals_pos, param, value = '', 0, '', ''
local function addPipe()
function addPipe()
if params.spaced then
if params.spaced then
text = text .. ' '
text = text .. ' '
end
end
text = text .. '<span class="'
text = text .. '<span'
if not params.black then
text = text .. ' class="wp-templatelink"'
end
if not params.spaced then
if not params.spaced then
text = text .. ' ts-templateCallCode-pipe'
text = text .. ' style="margin:0 2px;"'
end
if not params.black then
text = text .. ' ts-templateCallCode-weak'
end
end
text = text .. '>&#124;</span>'
-- &#124;, чтобы не трактовалось как разделитель ячеек в таблицах
text = text .. '">&#124;</span>'
end
end
local beforeParam = '<span class="ts-templateCallCode-param">'
local afterParam = '</span>'
for k, v in pairs(args) do
for k, v in pairs(args) do
if type(k) == 'number' then  -- Неименованные параметры
if type(k) == 'number' then  -- Неименованные параметры
if k >= params.from then
equals_pos = v:find('=')
equals_pos = v:find('=')
if equals_pos and v:find('{{=}}') == equals_pos - 2 then
if equals_pos and v:find('{{=}}') == equals_pos - 2 then
equals_pos = nil
equals_pos = nil
end
end
if equals_pos then  -- Содержащие «=» преобразуем в именованные
if equals_pos then  -- Содержащие «=» преобразуем в именованные
param = v:sub(1, equals_pos - 1)
param = v:sub(1, equals_pos - 1)
value = v:sub(equals_pos + 1)
value = v:sub(equals_pos + 1)
addPipe()
addPipe()
text = text .. param .. '=' .. value
text = text .. beforeParam .. processText(param, params.nowiki) .. '=' .. processText(value, params.nowiki) .. afterParam
else  -- Истинно неименованные
else  -- Истинно неименованные
addPipe()
addPipe()
text = text .. v
local paramValue = processText(v, params.nowiki)
if #paramValue ~= 0 then
text = text .. beforeParam .. paramValue .. afterParam
end
end
end
end
elseif not k:find('^_') then  -- Именованные параметры, исключая модификаторы внешнего вида
elseif not k:find('^_') then  -- Именованные параметры, исключая модификаторы внешнего вида
addPipe()
addPipe()
text = text .. beforeParam .. processText(k, params.nowiki) .. '=' .. processText(v, params.nowiki) .. afterParam
text = text .. k .. '=' .. v
end
end
end
end
Строка 93: Строка 266:
end
end


function p._main(args, flags)
p.withParams = makeInvokeFunc('_withParams')
 
function p._withParams(args)
local name = args[1]
local name = args[1]
table.remove(args, 1)
table.remove(args, 1)
Строка 109: Строка 284:
local optpText
local optpText
if not flags.withoutParams then
if name then
if name then
local spanOffset = mw.ustring.find(name, '<span')  -- След использования шаблона optp
local spanOffset = mw.ustring.find(name, '<span')  -- След использования шаблона optp
if spanOffset then
if spanOffset then
optpText = mw.ustring.sub(name, spanOffset)
optpText = mw.ustring.sub(name, spanOffset)
name = mw.ustring.sub(name, 1, spanOffset - 1)
name = mw.ustring.sub(name, 1, spanOffset - 1)
end
end
end
end
end
Строка 121: Строка 294:
local yesno = require('Module:Yesno')
local yesno = require('Module:Yesno')
local nolink, subst, podst, global, nav, noRedirect, ucFirst, black, nobr
local nobr = yesno(args._nobr, false)
local tag, style, comment, lang, sister, global, textInPlaceOfName,
local tag = args._tag or 'span'
namePrefix, prefix, postfix, nowiki
local style = args._style
local spaced, from
local spaced = yesno(args._spaced, false)
local subst = yesno(args._s, false)
if flags.withoutParams then
local podst = yesno(args['_п'], false)
for i, v in ipairs(args) do
local global = yesno(args._g, false) or name and mw.ustring.sub(name, 1, 1) == ':'
if v == 'nl' or v == 'nolink' then
local lang = args._lang
noLink = true
local sister = args._sister
elseif v == 's' then
local nav = yesno(args._nav, false)
subst = true
local ucFirst = yesno(args._u, false)
elseif v == 'п' then
local black = yesno(args._b, false) or tag ~= 'span'
podst = true
local noLink = yesno(args._nolink or args._nl, false) or not yesno(args._link, false)
elseif v == 'g' then
local textInPlaceOfName = args._text
global = true
local comment = args._comment
elseif v == 'nav' then
local noRedirect = yesno(args._noredir, false)
nav = true
local prefix = args._prefix
elseif v == 'noredir' then
local postfix = args._postfix
noRedirect = true
elseif v == 'u' then
ucFirst = true
elseif v == 'b' then
black = true
elseif v == 'nobr' then
nobr = true
end
end
 
tag = args.tag or 'span'
style = args.style
comment = args.comment
lang = args.lang
sister = args.sister
textInPlaceOfName = args.text
namePrefix = args.nameprefix
prefix = args.prefix
postfix = args.postfix
nowiki = args.nowiki
else
noLink = yesno(args._nolink or args._nl, false) or not yesno(args._link, false)
subst = yesno(args._s, false)
podst = yesno(args['_п'], false)
global = yesno(args._g, false)
nav = yesno(args._nav, false)
noRedirect = yesno(args._noredir, false)
ucFirst = yesno(args._u, false)
black = yesno(args._b, false)
nobr = yesno(args._nobr, false)
tag = args._tag or 'span'
style = args._style
comment = args._comment
lang = args._lang
sister = args._sister
textInPlaceOfName = args._text
namePrefix = args._nameprefix
prefix = args._prefix
postfix = args._postfix
nowiki = args._nowiki
spaced = yesno(args._spaced, false)
from = (tonumber(args._from) or 2) - 1
 
end
global = global or name and mw.ustring.sub(name, 1, 1) == ':'
black = black or tag ~= 'span'
if textInPlaceOfName == '' then
if textInPlaceOfName == '' then
Строка 200: Строка 324:
if sister == '' then
if sister == '' then
sister = nil
sister = nil
end
if namePrefix == '' then
namePrefix = nil
end
end
Строка 215: Строка 336:
name = mw.ustring.sub(trimmedName, 7)
name = mw.ustring.sub(trimmedName, 7)
end
end
end
if subst then
namePrefix = 'subst:'
elseif podst then
namePrefix = 'подст:'
end
end
Строка 228: Строка 343:
local currentTitleRoot = currentTitle.rootText
local currentTitleRoot = currentTitle.rootText
if not ucFirst and
if not ucFirst and
(
((ru:uc(currentTitleRoot) ~= currentTitleRoot and
(
-- Книга:Литературное наследство, TranslateDate
ru:uc(currentTitleRoot) ~= currentTitleRoot and
not mw.ustring.match(currentTitleRoot, '^[А-Яа-яA-Za-z]+:?[А-ЯA-Z]')
-- Книга:Литературное наследство, TranslateDate
) or
not mw.ustring.match(currentTitleRoot, '^[А-Яа-яA-Za-z]+:?[А-ЯA-Z]')
) or
#currentTitleRoot == 1
#currentTitleRoot == 1
)
)
Строка 268: Строка 381:
local noLink = noLink or currentTitle == titleObject
local noLink = noLink or currentTitle == titleObject
 
local takeBracketsInLink = not noLink and
mw.ustring.len(name) == 1 and
not black and
not subst and
not podst
if not noLink then
if not noLink then
if not noRedirect or (
if not noRedirect or (
Строка 289: Строка 407:
local text = ''
local text = ''
if tag then
if tag then
text = text .. '<' .. tag .. ' class="ts-templateCallCode'
text = text .. '<' .. tag .. ' class="templateCallCode'
if nobr then
if nobr then
text = text .. ' nowrap'
text = text .. ' nowrap'
Строка 300: Строка 418:
end
end
if prefix then
if prefix then
text = text .. processText(prefix, nowiki)
text = text .. prefix
end
end
text = text .. '<span class="'
if not black then
if not spaced then
text = text .. '<span class="wp-templatelink">'
text = text .. ' ts-templateCallCode-opening'
end
text = text .. '{{'
if subst then
text = text .. 'subst:'
elseif podst then
text = text .. 'подст:'
end
end
if not black then
if not black then
text = text .. ' ts-templateCallCode-weak'
text = text .. '</span>'
end
end
text = text .. '">{{'
if namePrefix then
text = text .. namePrefix
end
text = text .. '</span>'
if nav and currentTitle == titleObject then
if nav and currentTitle == titleObject then
text = text .. '\'\'\''
text = text .. '\'\'\''
end
end
text = text .. '<span class="ts-templateCallCode-templateName" data-navboxnavigation-link="0">'
local commentedLabel
local commentedLabel
if comment then
if comment then
-- https://phabricator.wikimedia.org/T200704
-- https://phabricator.wikimedia.org/T200704
-- commentedLabel = mw.getCurrentFrame():expandTemplate({title = 'comment', args = {(textInPlaceOfName or name), comment}})
-- commentedLabel = mw.getCurrentFrame():expandTemplate({title = 'comment', args = {(text or name), comment}})
commentedLabel = '<span class="commentedText" title="' .. comment .. '" style="border-bottom: 1px dotted; cursor: help;">' ..
commentedLabel = '<span class="commentedText" title="' .. comment .. '" style="border-bottom: 1px dotted; cursor: help;">' ..
(textInPlaceOfName or name) ..
(textInPlaceOfName or name) ..
Строка 342: Строка 458:
text = text .. label
text = text .. label
end
end
text = text .. '</span>'
if nav and currentTitle == titleObject then
if nav and currentTitle == titleObject then
Строка 349: Строка 463:
end
end
if not flags.withoutParams then
if optpText then
if optpText then
text = text .. optpText
text = text .. optpText
end
end
text = text .. addParams(args, {
text = text .. addParams(args, {
spaced = spaced,
spaced = spaced,
black = black,
black = black,
})
nowiki = nowiki,
from = from
if spaced then
})
text = text .. ' '
if spaced then
text = text .. ' '
end
end
end
text = text .. '<span class="'
if not black then
if not spaced then
text = text .. '<span class="wp-templatelink">'
text = text .. ' ts-templateCallCode-closing'
end
end
text = text .. '}}'
if not black then
if not black then
text = text .. ' ts-templateCallCode-weak'
text = text .. '</span>'
end
end
text = text .. '">}}</span>'
if postfix then
if postfix then
text = text .. processText(postfix, nowiki)
text = text .. postfix
end
end
if tag then
if tag then
Строка 382: Строка 491:
end
end
local ts = mw.getCurrentFrame():extensionTag{ name = 'templatestyles', args = { src = 'Модуль:Template call code/styles.css' } }
return text
return ts .. text
end
end
p.onlyParams = makeInvokeFunc('_onlyParams')


function p._onlyParams(args)
function p._onlyParams(args)
local span = mw.html.create('span')
span:css( 'color', mw.getCurrentFrame():expandTemplate({ title = 'optp/color' }) )
local yesno = require('Module:Yesno')
local yesno = require('Module:Yesno')
return addParams(args, {
span:wikitext(addParams(args, {
spaced = yesno(args._spaced, false),
spaced = yesno(args._spaced, false),
black = true,
black = true,
nowiki = yesno(args._nowiki, false),
}))
from = 1
 
})
return tostring(span)
end
end
p.withoutParams = makeInvokeFunc('_main', {withoutParams = true})
p.withParams = makeInvokeFunc('_main', {withoutParams = false})
p.onlyParams = makeInvokeFunc('_onlyParams')


return p
return p

Текущая версия от 22:00, 9 марта 2022

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

local getArgs = require('Module:Arguments').getArgs
local ru = mw.language.new('ru')

local p = {}

-- Используется для того, чтобы можно было удалять элементы из таблицы
local function copy(other)
	local res = {}
	for k, v in pairs(other) do
		res[k] = v
	end
	return res
end

local function makeInvokeFunc(funcName)
	return function (frame)
		local args = copy(getArgs(frame, {
			trim = false,
			removeBlanks = false
		}))
		return p[funcName](args)
	end
end

p.withoutParams = makeInvokeFunc('_withoutParams')

function p._withoutParams(args)
	local name = args[1]
	table.remove(args, 1)
	
	-- Вещи типа «=» в первом параметре
	if not name then
		for k, v in pairs(args) do
			if not k:find('^_') then
				name = k .. '=' .. v
				args[k] = nil
				break
			end
		end
	end
	
	local flags = {}
	for i, v in ipairs(args) do
		if v == 'nl' or v == 'nolink' then
			flags.noLink = true
		elseif v == 's' then
			flags.subst = true
		elseif v == 'п' then
			flags.podst = true
		elseif v == 'g' then
			flags.global = true
		elseif v == 'nav' then
			flags.nav = true
		elseif v == 'noredir' then
			flags.noRedirect = true
		elseif v == 'u' then
			flags.ucFirst = true
		elseif v == 'b' then
			flags.black = true
		end
	end
	
	if name then
		local trimmedName = mw.text.trim(name)
		if ru:lc(mw.ustring.sub(trimmedName, 1, 6)) == 'subst:' then
			flags.subst = true
			name = mw.ustring.sub(trimmedName, 7)
		end
		if ru:lc(mw.ustring.sub(trimmedName, 1, 6)) == 'подст:' then
			flags.podst = true
			name = mw.ustring.sub(trimmedName, 7)
		end
	end
	
	if args.text == '' then
		args.text = nil
	end
	if args.comment == '' then
		args.comment = nil
	end
	if args.lang == '' then
		args.lang = nil
	end
	if args.sister == '' then
		args.sister = nil
	end
	
	local currentTitle = mw.title.getCurrentTitle()
	-- При опущенном первом параметре берём имя шаблона из названия страницы
	if name == '' or not name then
		local currentTitleRoot = currentTitle.rootText
		if not flags.ucFirst and
			((ru:uc(currentTitleRoot) ~= currentTitleRoot and
				-- Книга:Литературное наследство, TranslateDate
				not mw.ustring.match(currentTitleRoot, '^[А-Яа-яA-Za-z]+:?[А-ЯA-Z]')
			) or
				#currentTitleRoot == 1
			)
		then
			name = ru:lcfirst(currentTitleRoot)
		else
			name = currentTitleRoot
		end
	end
	
	local global = flags.global or mw.ustring.sub(name, 1, 1) == ':'
	
	-- Начинаем собирать код
	local linkBody, titleObject, linkBegin, linkDivider, linkEnd
	
	local prefixes = {}
	if args.lang then
		table.insert(prefixes, args.lang)
	end
	if args.sister then
		table.insert(prefixes, args.sister)
	end
	linkBody = table.concat(prefixes, ':')
	
	if #linkBody ~= 0 then
		linkBody = ':' .. linkBody
	end
	if mw.ustring.sub(name, 1, 1) ~= ':' then
		linkBody = linkBody .. ':'
	end
	if not global then
		linkBody = linkBody .. 'Template:'
	end
	linkBody = linkBody .. name
	titleObject = mw.title.new(linkBody)
	
	local noLink = flags.noLink or currentTitle == titleObject
	local takeBracketsInLink = not noLink and
		mw.ustring.len(name) == 1 and
		not flags.black and
		not flags.subst and
		not flags.podst
	
	if not noLink then
		if not flags.noRedirect or (
			flags.noRedirect and
			not args.lang and
			not args.sister and
			not titleObject.exists
		) then
			linkBegin = '[['
			linkEnd = ']]'
			linkDivider = '|'
		else
			linkBegin = '['
			linkEnd = ']'
			linkDivider = ' '
			linkBody = titleObject:fullUrl('redirect=no')
		end
	end
	
	local text = ''
	if flags.nav and currentTitle == titleObject then
		text = text .. '\'\'\''
	end
	if not flags.black then
		text = text .. '<span class="wp-templatelink">'
	end
	text = text .. '{'
	if not takeBracketsInLink then
		text = text .. '{'
	end
	if flags.subst then
		text = text .. 'subst:'
	elseif flags.podst then
		text = text .. 'подст:'
	end
	if not flags.black then
		text = text .. '</span>'
	end
	
	text = text .. '<span data-navboxnavigation-link="0">'
	
	local commentedLabel
	if args.comment then
		-- https://phabricator.wikimedia.org/T200704
		-- commentedLabel = mw.getCurrentFrame():expandTemplate({title = 'comment', args = {(args.text or name), args.comment}})
		commentedLabel = '<span class="commentedText" title="' .. args.comment .. '" style="border-bottom: 1px dotted; cursor: help;">' ..
			(args.text or name) ..
			'</span>'
	end
	local label = (commentedLabel or args.text or name)
	if not noLink then
		if flags.noRedirect then
			text = text .. '<span class="plainlinks">'
		end
		text = text .. linkBegin .. linkBody .. linkDivider
		if not noLink and takeBracketsInLink then
			text = text .. '<span class="wp-templatelink">{</span>'
		end
		text = text .. label
		if not noLink and takeBracketsInLink then
			text = text .. '<span class="wp-templatelink">}</span>'
		end
		text = text .. linkEnd
		if flags.noRedirect then
			text = text .. '</span>'
		end
	else
		text = text .. label
	end
	
	text = text .. '</span>'
	
	if not flags.black then
		text = text .. '<span class="wp-templatelink">'
	end
	text = text .. '}'
	if not takeBracketsInLink then
		text = text .. '}'
	end
	if not flags.black then
		text = text .. '</span>'
	end
	if flags.nav and currentTitle == titleObject then
		text = text .. '\'\'\''
	end
	
	return text
end

function addParams(args, params)
	local text, equals_pos, param, value = '', 0, '', ''
	
	function addPipe()
		if params.spaced then
			text = text .. ' '
		end
		text = text .. '<span'
		if not params.black then
			text = text .. ' class="wp-templatelink"'
		end
		if not params.spaced then
			text = text .. ' style="margin:0 2px;"'
		end
		text = text .. '>&#124;</span>'
	end
	
	for k, v in pairs(args) do
		if type(k) == 'number' then  -- Неименованные параметры
			equals_pos = v:find('=')
			if equals_pos and v:find('{{=}}') == equals_pos - 2 then
				equals_pos = nil
			end
			if equals_pos then  -- Содержащие «=» преобразуем в именованные
				param = v:sub(1, equals_pos - 1)
				value = v:sub(equals_pos + 1)
				addPipe()
				text = text .. param .. '=' .. value
			else  -- Истинно неименованные
				addPipe()
				text = text .. v
			end
		elseif not k:find('^_') then  -- Именованные параметры, исключая модификаторы внешнего вида
			addPipe()
			text = text .. k .. '=' .. v
		end
	end
	
	return text
end

p.withParams = makeInvokeFunc('_withParams')

function p._withParams(args)
	local name = args[1]
	table.remove(args, 1)
	
	-- Вещи типа «=» в первом параметре
	if not name then
		for k, v in pairs(args) do
			if not k:find('^_') then
				name = k .. '=' .. v
				args[k] = nil
				break
			end
		end
	end
	
	local optpText
	if name then
		local spanOffset = mw.ustring.find(name, '<span')  -- След использования шаблона optp
		if spanOffset then
			optpText = mw.ustring.sub(name, spanOffset)
			name = mw.ustring.sub(name, 1, spanOffset - 1)
		end
	end
	
	local yesno = require('Module:Yesno')
	
	local nobr = yesno(args._nobr, false)
	local tag = args._tag or 'span'
	local style = args._style
	local spaced = yesno(args._spaced, false)
	local subst = yesno(args._s, false)
	local podst = yesno(args['_п'], false)
	local global = yesno(args._g, false) or name and mw.ustring.sub(name, 1, 1) == ':'
	local lang = args._lang
	local sister = args._sister
	local nav = yesno(args._nav, false)
	local ucFirst = yesno(args._u, false)
	local black = yesno(args._b, false) or tag ~= 'span'
	local noLink = yesno(args._nolink or args._nl, false) or not yesno(args._link, false)
	local textInPlaceOfName = args._text
	local comment = args._comment
	local noRedirect = yesno(args._noredir, false)
	local prefix = args._prefix
	local postfix = args._postfix
	
	if textInPlaceOfName == '' then
		textInPlaceOfName = nil
	end
	if comment == '' then
		comment = nil
	end
	if lang == '' then
		lang = nil
	end
	if sister == '' then
		sister = nil
	end
	
	if name then
		local trimmedName = mw.text.trim(name)
		if ru:lc(mw.ustring.sub(trimmedName, 1, 6)) == 'subst:' then
			subst = true
			name = mw.ustring.sub(trimmedName, 7)
		end
		if ru:lc(mw.ustring.sub(trimmedName, 1, 6)) == 'подст:' then
			podst = true
			name = mw.ustring.sub(trimmedName, 7)
		end
	end
	
	local currentTitle = mw.title.getCurrentTitle()
	-- При опущенном первом параметре берём имя шаблона из названия страницы
	if name == '' or not name then
		local currentTitleRoot = currentTitle.rootText
		if not ucFirst and
			((ru:uc(currentTitleRoot) ~= currentTitleRoot and
				-- Книга:Литературное наследство, TranslateDate
				not mw.ustring.match(currentTitleRoot, '^[А-Яа-яA-Za-z]+:?[А-ЯA-Z]')
			) or
				#currentTitleRoot == 1
			)
		then
			name = ru:lcfirst(currentTitleRoot)
		else
			name = currentTitleRoot
		end
	end
	
	-- Начинаем собирать код
	local linkBody, titleObject, linkBegin, linkDivider, linkEnd
	
	local prefixes = {}
	if lang then
		table.insert(prefixes, lang)
	end
	if sister then
		table.insert(prefixes, sister)
	end
	linkBody = table.concat(prefixes, ':')
	
	if #linkBody ~= 0 then
		linkBody = ':' .. linkBody
	end
	if mw.ustring.sub(name, 1, 1) ~= ':' then
		linkBody = linkBody .. ':'
	end
	if not global then
		linkBody = linkBody .. 'Template:'
	end
	linkBody = linkBody .. name
	titleObject = mw.title.new(linkBody)
	
	local noLink = noLink or currentTitle == titleObject
	local takeBracketsInLink = not noLink and
		mw.ustring.len(name) == 1 and
		not black and
		not subst and
		not podst
	
	if not noLink then
		if not noRedirect or (
			noRedirect and
			not lang and
			not sister and
			not titleObject.exists
		) then
			linkBegin = '[['
			linkEnd = ']]'
			linkDivider = '|'
		else
			linkBegin = '['
			linkEnd = ']'
			linkDivider = ' '
			linkBody = titleObject:fullUrl('redirect=no')
		end
	end
	
	local text = ''
	if tag then
		text = text .. '<' .. tag .. ' class="templateCallCode'
		if nobr then
			text = text .. ' nowrap'
		end
		text = text .. '"'
		if style then
			text = text .. ' style="' .. style .. '"'
		end
		text = text .. '>'
	end
	if prefix then
		text = text .. prefix
	end
	
	if not black then
		text = text .. '<span class="wp-templatelink">'
	end
	text = text .. '{{'
	if subst then
		text = text .. 'subst:'
	elseif podst then
		text = text .. 'подст:'
	end
	if not black then
		text = text .. '</span>'
	end
	
	if nav and currentTitle == titleObject then
		text = text .. '\'\'\''
	end
	
	local commentedLabel
	if comment then
		-- https://phabricator.wikimedia.org/T200704
		-- commentedLabel = mw.getCurrentFrame():expandTemplate({title = 'comment', args = {(text or name), comment}})
		commentedLabel = '<span class="commentedText" title="' .. comment .. '" style="border-bottom: 1px dotted; cursor: help;">' ..
			(textInPlaceOfName or name) ..
			'</span>'
	end
	local label = (commentedLabel or textInPlaceOfName or name)
	if not noLink then
		if noRedirect then
			text = text .. '<span class="plainlinks">'
		end
		text = text .. linkBegin .. linkBody .. linkDivider .. label .. linkEnd
		if noRedirect then
			text = text .. '</span>'
		end
	else
		text = text .. label
	end
	
	if nav and currentTitle == titleObject then
		text = text .. '\'\'\''
	end
	
	if optpText then
		text = text .. optpText
	end
	
	text = text .. addParams(args, {
		spaced = spaced,
		black = black,
	})
	
	if spaced then
		text = text .. ' '
	end
	
	if not black then
		text = text .. '<span class="wp-templatelink">'
	end
	text = text .. '}}'
	if not black then
		text = text .. '</span>'
	end
	
	if postfix then
		text = text .. postfix
	end
	if tag then
		text = text .. '</' .. tag .. '>'
	end
	
	return text
end

p.onlyParams = makeInvokeFunc('_onlyParams')

function p._onlyParams(args)
	local span = mw.html.create('span')
	span:css( 'color', mw.getCurrentFrame():expandTemplate({ title = 'optp/color' }) )
	
	local yesno = require('Module:Yesno')
	
	span:wikitext(addParams(args, {
		spaced = yesno(args._spaced, false),
		black = true,
	}))

	return tostring(span)
end

return p