Модуль:Tl
Функции tl / tlf / lg[]
Модуль {{<img src="{{filepath:Lua-logo-nolabel.svg}}" style="height:1em" /> tl}} предназначен для создания ссылок на шаблоны (а также модули, системные сообщения и, в принципе, на любые страницы). Рассчитан преимущественно для использования в шаблонах (например, {{tl}} и {{tlf}} )).
В модуле определены функции {{#invoke:tl|tl}}
, {{#invoke:tl|tlf}}
, сопоставляемые одноименным шаблонам, а также универсальная функция {{#invoke:tl|lg}}
, которая позволяет настраивать все параметры шаблона. Все функции рассчитаны на вызов из специального шаблона и под параметрами функции далее понимаются параметры вызывающего шаблона.
Первым параметром всех указанных функций является имя шаблона, на который осуществляется ссылка, далее задаются параметры этого шаблона. Различие в функциях состоит исключительно в значениях параметров по умолчанию, любое из них может быть изменено явным заданием. Эти параметры задаются следующим образом:
параметр | назначение параметра | умолчание | ||
---|---|---|---|---|
lg | tl | tlf | ||
#code= | ссылка обертывается в теги <code>...</code> |
no | no | yes |
#icon= | вместо имен пространств Module, MediaWiki, HTML отображаются соответствующие иконки | no | yes | no |
#edit= | после ссылки отображается иконка для перехода к редактированию шаблона | no | yes | no |
#talk= | после ссылки отображается иконка для перехода к странице обсуждения | no | no | no |
#output= | вместе со ссылкой отображается результат исполнения соответствующего шаблона. Возможные значения:
|
no | no | no |
#wh= | из показываемого текста ссылки исключается указание интервики | no | no | no |
#compact= | вместо красивой ссылки показывается текст, указанный в этом параметре | no | no | no |
Если шаблон, на который необходимо сослаться, содержит ключевые параметры с именами, начинающимися с символа #
, то этот символ следует удвоить.
Demo[]
Функция {{#invoke:tl|Demo}}
предназначена для демонстрации вики-текста и результата его действия. Первым параметром функции (точнее, вызывающего функцию шаблона) должен являться вики-текст заключенный в теги <nowiki>...</nowiki>
. Другим параметром может являться |#output=
, описанный выше.
Задавайте категории на странице документации, а не здесь! Показать подстраницы
local p = {}
function p.tlf (frame)
local args = frame:getParent().args
args ['#code'] = args ['#code'] or 'y'
args ['#edit'] = args ['#edit'] or 'n'
args ['#icon'] = args ['#icon'] or 'n'
return p.tlexec (args,frame)
end
function p.tl (frame)
local args = frame:getParent().args
args ['#code'] = args ['#code'] or 'n'
args ['#edit'] = args ['#edit'] or 'y'
args ['#icon'] = args ['#icon'] or 'y'
return p.tlexec (args,frame)
end
function p.lg (frame)
local args = frame.args
return p.tlexec (args,frame)
end
function p.tlexec (args,frame)
local yesno = require('Module:Yesno')
local code = yesno (args['#code'], true)
local icon = yesno (args['#icon'], true)
local edit = yesno (args['#edit'], false)
local talk = yesno (args['#talk'], false)
local subst = yesno (args['#subst'], true)
local hiddenwiki = yesno (args['#wh'], true)
local compact = yesno (args['#compact'], args['#compact'])
local outformat = yesno (args['#output'],args['#output'])
if outformat == true then
outformat = '$t → <samp>$r</samp>'
elseif outformat == 'tdtd' then
outformat = '<tr><td>$t</td><td><samp>$r</samp></td><tr>'
end
local target = args[1] or 'tl'
local debugs = ''
local splitarget = mw.text.split(target,':')
local wiki, namesp, pagename, titarget
if #splitarget > 1 and splitarget[1] ~= '' and mw.ustring.lower(mw.ustring.sub(splitarget[1],1,1)) ~= mw.ustring.upper(mw.ustring.sub(splitarget[1],1,1)) then
-- if mw.site.interwikiMap('')[splitarget[1]] then -- появится лишь в 1.25. Пока проверям, что не изменился регистр
if mw.title.makeTitle( '', 'A', '', splitarget[1] )['fullText'] == mw.ustring.lower(splitarget[1]) ..':' .. 'A' then
wiki = splitarget[1] .. ':'
table.remove(splitarget, 1)
else
wiki = ''
end
else
wiki = ''
end
if #splitarget > 1 then
if splitarget[1] == '' then
namesp = 0
table.remove(splitarget, 1)
elseif splitarget[1] == '#invoke' then
namesp = 828
table.remove(splitarget, 1)
elseif mw.site.namespaces[splitarget[1]] then
namesp = mw.site.namespaces[splitarget[1]]['id']
table.remove(splitarget, 1)
else
namesp = 10
end
else
namesp = 10
end
pagename = table.concat (splitarget, ':')
titarget = wiki .. mw.site.namespaces[namesp]['canonicalName'] .. ':' .. pagename
local talktarget
if mw.site.namespaces[namesp]['isSubject'] then
talktarget = wiki .. mw.site.namespaces[namesp]['talk']['canonicalName'] .. ':' .. pagename
end
if mw.ustring.sub (titarget,1,1) ~= ':' then
titarget = ':' .. titarget
end
-- titarget - ссылка
-- wiki - интервики (с двоеточием) или пусто
-- namesp - номер пространства
-- pagename - имя без интервики и пространства
local showname = mw.site.namespaces[namesp]['canonicalName'] .. ':' .. pagename
if code then
res = '<code>'
else
res = ''
end
res = res .. '<span class="wp-templatelink"><span class="wp-templatelink-syntax">'
if compact then
-- skip
elseif namesp == 8 or namesp == 10 or namesp == 206 or namesp == 828 then -- MediaWiki, Template, HTML, Module
res = res .. '{{'
showname = pagename
else
res = res .. '[['
end
res = res .. '</span>'
if subst then
res = res .. '[[wp:Help:Substitution|subst]]<span class="wp-templatelink-syntax">:</span>'
end
if wiki ~= '' and not hiddenwiki and not compact then
res = res .. '[[:' .. wiki ..'|' .. wiki .. ']]'
end
if compact then
-- skip
elseif namesp == 828 then
if icon then
res = res .. '<img src="{{filepath:Lua-logo-nolabel.svg}}" style="height:1em" /> '
else
res = res .. '#invoke:'
end
elseif namesp == 8 then
if icon then
res = res .. '<i class="fa fa-cog" style="color:#006699"></i> '
else
res = 'MediaWiki:'
end
elseif namesp == 206 then
if icon then
res = res .. '<i class="fa fa-globe" style="color:#006699"></i> '
else
res = 'HTML:'
end
end
if compact then
showname = compact
end
res = res .. '[[' .. titarget .. '|' .. showname .. ']]'
local argu = {}
for k, v in ipairs (args) do
argu[k-1] = v -- argu[0] is defined
if k ~= 1 then
res = res .. '<span class="wp-templatelink-syntax">|</span>' .. v
end
end
for k, v in pairs (args) do
if type (k) == 'string' then
if mw.ustring.sub (k,1,1) == '#' then
if mw.ustring.sub (k,2,2) ~= '#' then
k = 0
else
k = mw.ustring.sub (k,2)
end
end
else
k = k-1
end
if not argu[k] then
argu[k] = v
res = res .. '<span class="wp-templatelink-syntax">|</span>' .. k .. '<span class="wp-templatelink-syntax">=</span>' .. v
end
end
argu[0]=nil
res = res .. '<span class="wp-templatelink-syntax">'
if compact then
-- skip
elseif namesp == 8 or namesp == 10 or namesp == 206 or namesp == 828 then -- MediaWiki, Template, HTML, Module
res = res .. '}}'
else
res = res .. ']]'
end
res = res .. '</span></span>'
if code then
res = res .. '</code>'
end
if edit then
res = res .. '<small class=plainlinks> [' .. tostring(mw.uri.fullUrl( titarget, 'action=edit' )) ..
' <i class="fa fa-edit"></i>]</small>'
end
if talk and talktarget then
res = res .. '<small class=plainlinks> [[' .. talktarget ..
'|<i class="far fa-comment"></i>]]</small>'
end
if outformat then
res = mw.ustring.gsub (outformat, '%$[tr]',
function (match)
if match == '$t' then
return res
elseif match == '$r' then
return frame:expandTemplate {title=titarget,args=argu}
end
end
)
end
return res
end
function p.Demo (frame)
local yesno = require('Module:Yesno')
local args = frame:getParent().args
local stro = args[1]
-- local outformat = args['#output'] or '$t → <samp>$r</samp>'
local outformat = yesno (args['#output'],args['#output'])
if outformat == true then
outformat = '$t → <samp>$r</samp>'
elseif outformat == 'tdtd' then
outformat = '<tr><td>$t</td><td><samp>$r</samp></td><tr>'
end
local res
res = frame:preprocess (mw.text.decode(mw.text.unstrip(stro)))
if outformat then
res = mw.ustring.gsub (outformat, '%$[tr]',
function (match)
if match == '$t' then
return stro
elseif match == '$r' then
return res
end
end
)
end
return res
end
function p.z ()
local h = { 'F', '12 часов:время', ':F', 'Template:F', 'Module:F', '#invoke:F', 'Help:F', 'wp:F', 'wp::F', 'wp:Template:F', 'wp:Module:F', 'wp:Help:F',
'F/a', ':F/a', 'Template:F/a', 'Module:F/a', 'Help:F/a', 'wp:F/a', 'wp::F/a', 'wp:Template:F/a', 'wp:Module:F/a', 'wp:Help:F/a',}
src = ''
res = ''
res1 = ''
res2p = ''
res2 = ''
res3p = ''
res3 = ''
for k, v in ipairs (h) do
res = res .. v .. ' -- ' .. p.tlexec ({v, ['#edit']='yes'}) .. '\n'
end
return res
end
return p