Модуль:Transclude

ХӀокху модулах лаьцна хааман Модуль:Transclude/doc агӀо кхолла мега

local p={}-- Вызывает внутренний шаблон с аргументами объемлющего шаблонаfunction p.call(frame)local template = frame.args[1]local args = frame:getParent().args    return frame:expandTemplate{ title=template, args=args }end-- Общая реализация для forall и calllocal function forallImpl(args, separator, conjunction, func)-- нумерованные ключи из argslocal keys = {}-- перебор в произвольном порядке, даже для нумерованных ключейfor key, value in pairs(args) doif type(key) == 'number' and value and value ~= '' thentable.insert(keys, key)endendtable.sort(keys)local results = {}for _, key in ipairs(keys) dolocal value = func(args[key])table.insert(results, value)endreturn mw.text.listToText(results, separator, conjunction)end-- Вызывает внутренний шаблон, передавая ему нумерованные параметры объемлющего шаблона по-одномуfunction p.forall(frame)local template = frame.args[1]local separator = frame.args.separator or ''local conjunction = frame.args.conjunction or separatorlocal args = frame:getParent().argslocal func = function(value)return frame:expandTemplate{ title = template, args = {value} } -- или другой frame?endreturn forallImpl(args, separator, conjunction, func)end-- Берёт нумерованные аргументы объемлющего шаблона и склеивает их в единую строкуfunction p.join(frame)local separator = frame.args[1] or ''local conjunction = frame.args[2] or separatorlocal args = frame:getParent().argslocal func = function(value)return valueendreturn forallImpl(args, separator, conjunction, func)end-- Служебная функция: удаляет дубликаты из отсортированного массива с нумерованными индексамиlocal function deleteDuplicates(args)local res = {}for key, value in pairs(args) doif args[key+1] ~= value thentable.insert(res, value)endendreturn resend-- Вызывает внутренний шаблон несколько раз, передавая в него блоки аргументов объемлющего шаблонаfunction p.npc(frame)local args = frame:getParent().argslocal templateFrame = frame:getParent()local template = frame.args[1]-- определение, блоки аргументов с какими номерами нужны:-- если в объемлющем шаблоне есть "параметр12" и в вызове модуля есть "параметр", то вызывается 12-й блокlocal nums = {}for key, _ in pairs(args) dolocal main, num = string.match(key, '^(.-)%s*(%d*)$')num = tonumber(num)-- учитывать "параметр12", только если задано "параметр"if num and frame.args[main] thentable.insert(nums, num)endendtable.sort(nums)nums = deleteDuplicates(nums)-- проходить по нужным номерам блоков по возрастанию и однократно-- подставлять в шаблон:-- 1. общие аргументы данного модуля-- 2. аргументы объемлющего шаблона вида "параметр12" как "параметр" в 12-й блокlocal results = {}for _, blockNum in ipairs(nums) do-- общие аргументы модуля, которые передаются в каждый блокlocal blockArgs = mw.clone(frame.args) -- metatable ломает expandTemplatesetmetatable(blockArgs, nil)for key, value in pairs(args) dolocal main, num = string.match(key, '^(.-)%s*(%d*)$')num = tonumber(num)-- передавать "параметр12" как "параметр" в 12-й блок, только если есть "параметр" в вызове модуляif blockNum == num and frame.args[main] thenblockArgs[main] = valueendendlocal blockText = templateFrame:expandTemplate{ title=template; args=blockArgs }table.insert(results, blockText)end    return table.concat(results)end-- Действует аналогично forall по числовой переменной, изменяющейся (по умолчанию, от 1) до f.args[2].function p.cycle(f)    local tf,ac,op=f:getParent(), {}, f.args.output or 'inline';    local sep='';    if op == 'newline' then    sep='\n';    end    for p,k in pairs(f.args) do        if type(p)=='number' then            if p>2 then ac[p-1]=k end        else ac[p]=k        end    end    local s,fh = f.args[2]:match('^%s*(%-?%d+)%s*%.%.') or 1,        f.args[2]:match('%.%.%s*(%S.*)%s*$') or f.args[2] or '';    fh=tonumber(fh) or fh:match('^%s*(.-)%s*$');s=tonumber(s);    local acr={};    if not s then error('Начало цикла «'..s..'» — не число') end    local function dc(order)        local r=tf:expandTemplate{ title=f.args[1]; args={s,unpack(ac)} }        if order == 'desc' then        s=s-1;        else        s=s+1;    end        if r~='' then table.insert(acr,r); return r end    end    if type(fh)=='number' then    if fh > s then        while s<=fh do dc('asc') end    else        while s>=fh do dc('desc') end    end    elseif fh~='' then        while tf:expandTemplate{ title=fh; args={s,unpack(ac)} } do dc('asc') end    else        while dc('asc') do end    end    return table.concat(acr, sep)endreturn p