Модуль:ExampleList
Версия от 01:27, 19 марта 2016; t>Jack who built the house (пустой параметр → вызов {{пример}} без пустого второго параметра, -_ из названий параметров, +wide)
Для документации этого модуля может быть создана страница Модуль:ExampleList/doc
local p = {}
-- используется для того, чтобы можно было удалять элементы из таблицы
local function copy(other)
local res = {}
for k,v in pairs(other) do
res[k] = v
end
return res
end
-- вызов шаблона, при ошибке возвращает пустую строку
local function expand(frame, tname, targs)
local success, result = pcall(
frame.expandTemplate,
frame,
{title = tname, args = targs}
)
if success then
return result
else
return ''
end
--return frame:expandTemplate({title = tname, args = args})
end
function p.main(frame)
if not getArgs then
getArgs = require('Module:Arguments').getArgs
end
yesno = require('Module:Yesno')
local trim = not (yesno(frame:getParent().args.trim or frame:getParent().args._trim, false) == false) --по умолчанию true
local args = copy(getArgs(frame, {trim = trim, removeBlanks = false})) --copy(frame.args)
local tname = args.t or args._t or args.template or args._template or args[1]
local opener = args.opener or args._opener or '* '
local sep = args.sep or args._sep
local style = args.style or args._style
local pre_text = args['pre-text'] or args['_pre-text'] or args.prefix or args._prefix or ''
local post_text = args['post-text'] or args['_post-text'] or args.postfix or args._postfix or ''
local inthemiddle = yesno(args.inthemiddle or args._inthemiddle, false)
local wide = yesno(args.wide or args._wide, false)
if style == 'wikitable' then
opener = '|-\n| '
end
if tname == '' or tname == nil then --при опущенном первом параметре берём имя шаблона из названия страницы
tname = mw.language.new('ru'):lcfirst(mw.title.getCurrentTitle().rootText)
end
if args._template == nil then --имя вызываемого шаблона в неименованном первом параметре (или же взято из названия страницы или
table.remove(args, 1) --в этой строчке вреда нет в любом случае), больше его обрабатывать не надо
end
local targs, content = {}, ''
if (style == 'wikitable' and not inthemiddle) then
content = '{| class="wikitable '
if wide then
content = content .. 'wide'
end
content = content .. '"\n! Код !! Результат\n'
end
for k, v in pairs(args) do
if type(k) == 'number' then
if v ~= '' then
targs = mw.text.split(v, '\\')
end
table.insert(targs, 1, tname)
targs._sep, targs._style, targs['_pre-text'], targs['_post-text'] = sep, style, pre_text, post_text
content = content .. opener .. tostring(expand(frame, 'пример', targs)) .. '\n'
end
end
if (style == 'wikitable' and not inthemiddle) then
content = content .. '|}'
else
content = content:sub(1, -2)
end
return content
end
return p