Перайсьці да зьместу

Модуль:Ня блытаць

Зьвесткі зь Вікіпэдыі — вольнай энцыкляпэдыі
-- Апрацоўвае {{Ня блытаць}}
local mHatnote = require('Модуль:Іншае значэньне')
local mHatlist = require('Модуль:Сьпіс верхніх зносак')
local mGrammar = require('Модуль:Grammar') --лянівая ініцыялізацыя
local mArguments --лянівая ініцыялізацыя
local mTableTools --лянівая ініцыялізацыя
local libraryUtil = require('libraryUtil')
local checkType = libraryUtil.checkType
local p = {}

function p.distinguish(frame)
    mArguments = require('Модуль:Аргумэнты')
    mTableTools = require('Модуль:TableTools')
    local args = mArguments.getArgs(frame)
    local selfref = args.selfref
    local text = args.text
    local distinctions = {}
    
    -- Зьбіраем трыплеты «назва старонкі — тэкст спасылкі — тлумачэньне» для працы існага шаблёну
    for i = 1, math.huge do
        local page = args[i .. '-page'] or (i == 1 and args[1])
        local link = args[i .. '-link'] or (i == 1 and args[2])
        local desc = args[i .. '-desc'] or (i == 1 and args[3])
        
        if not page then break end
        
        local distinction = ''
        if link and link ~= '' then
            distinction = string.format('[[%s|%s]]', page, link)
        else
            distinction = string.format('[[%s]]', page)
        end
        
        if desc and desc ~= '' then
            distinction = distinction .. ' — ' .. desc
        end
        
        table.insert(distinctions, distinction)
    end
    
    return p._distinguish(distinctions, text, selfref)
end

function p._distinguish(distinctions, text, selfref)
    checkType("_distinguish", 1, distinctions, 'table')
    if #distinctions == 0 and not text then return '' end
    
    local formattedText
    if text then
        formattedText = text
    else
        if #distinctions == 1 then
            -- Вызначаем правільны варыянт з / зь
            local page = distinctions[1]:match('%[%[(.-)%|') or distinctions[1]:match('%[%[(.-)%]%]')
            if page then
                local preposition = "з"
                -- Выклікаем softenPrepo з модулю Grammar
                local processedPrepo = mGrammar.softenPrepo({args = {preposition, page}})
                formattedText = string.format('Ня варта блытаць %s %s.', processedPrepo, distinctions[1])
            else
                formattedText = string.format('Ня варта блытаць з %s.', distinctions[1])
            end
        else

            formattedText = string.format('Ня варта блытаць з: %s.', table.concat(distinctions, ', '))
        end
    end
    
    local hnOptions = {selfref = selfref}
    return mHatnote._hatnote(formattedText, hnOptions)
end

return p