Модуль:Предмет: различия между версиями

мНет описания правки
м параметры LinkCard
 
(не показано 48 промежуточных версий этого же участника)
Строка 2: Строка 2:
local getArgs = require('Module:Arguments').getArgs
local getArgs = require('Module:Arguments').getArgs


function p.main(frame)
-- Форматирует файл изображения
    local args = getArgs(frame)
local function formatImage(file, size, link)
    local id = args[1] or ""
return string.format('[[Файл:%s|%s|link=%s]]', file, size, link)
end


    -- Инициализация данных предмета
-- Оборачивает текст в ссылку
    local itemStack = frame:preprocess(string.format('{{#invoke:Prototypes/Хранилище/Предмет|main|framing|stack|%s}}', id))
local function wrapLink(text, target)
    local itemContained = frame:preprocess(string.format('{{#invoke:Prototypes/Хранилище/Предмет|main|framing|contained|%s}}', id))
return (text ~= '' and target and string.format('[[%s|%s]]', target, text) or text)
    local itemSlot = frame:preprocess(string.format('{{#invoke:Prototypes/Хранилище/Предмет|main|framing|slot|%s}}', id))
end
    local itemChem = frame:preprocess(string.format('{{СollapsibleMenu|color=#3e7c82|{{#invoke:Prototypes/Хранилище/Предмет|main|chem|%s}}}}', id))
    local itemName = frame:preprocess("{{#invoke:Entity Lookup|getname|" .. id .. "}}")


    local prefix = args[2] or ""
function p.main(frame)
    local label = args["label"] or itemName
local argsRaw = getArgs(frame, { trim = false, removeBlanks = false })
    local size = args["size"] or "32px"
local args = {}
for k, v in pairs(argsRaw) do
if v ~= '' then args[k] = v end
end


    -- Формирование строки изображения
local id   = args[1] or ''
    local image = ""
local size = args.size or '32px'
    if args["image"] ~= nil then
local prefix  = args[2] or ''
        if args["image"] ~= "" then
local isRepo  = argsRaw.repository ~= nil
            image = string.format("[[Файл:%s|%s]]", args["image"], size)
local isWrap  = argsRaw.wrapper ~= nil
        else
local isVert  = argsRaw.vertical  ~= nil
            image = ""
        end
    else
        image = string.format("[[Файл:%s.png|%s]]", id, size)
    end


    -- Формирование ярлыка с ссылкой, если задана
-- Получение имени
    local labelOutput = label
local nameStr = frame:preprocess(
    if args["link"] then
string.format('{{#invoke:Entity Lookup|getname|%s}}', id)
        local linkTarget = args["link"]
)
        if linkTarget == "" then
            linkTarget = itemName
        end
        if label ~= "" then
            labelOutput = string.format("[[%s|%s]]", linkTarget, label)
        else
            labelOutput = ""
        end
    end


    -- Сбор информации в зависимости от наличия репозитория
-- Лейбл и ссылка
    local additionalInfo
local labelRaw = argsRaw.label or argsRaw.l
    if args["repository"] then
local label = (labelRaw == nil and nameStr) or (labelRaw == '' and '' or labelRaw)
        additionalInfo = itemStack .. " " .. labelOutput .. " " .. itemContained .. " " .. itemSlot .. " " .. itemChem
local linkRaw  = argsRaw.link
    else
local linkTgt  = linkRaw == nil and '' or (linkRaw == '' and nameStr or linkRaw)
        additionalInfo = prefix .. " " .. labelOutput
local labelOut = (linkRaw ~= nil) and wrapLink(label, linkTgt) or label
    end


    local result = image .. " " .. additionalInfo
-- Изображение
local imgFile = argsRaw.image or argsRaw.img or (id .. '.png')
local img   = imgFile ~= '' and formatImage(imgFile, size, linkTgt) or ''
if argsRaw.imageTooltip then
img = frame:preprocess(
string.format(
'{{#invoke:Entity Lookup|createimagetooltip|Файл:%s.png|%s|Мета=%s,link=}}',
id, id, size
)
)
end


    -- Формирование вывода через LinkCard, если указан обёртка
-- Репозиторий
    if args["wrapper"] and args["wrapper"] ~= "" then
local repoStr = ''
        local pixel = string.match(size, "(%d+)")
if isRepo then
        local wrapperInfo
local parts = {}
        if args["repository"] then
for _, slot in ipairs({ 'contained', 'slot', 'chem' }) do
            wrapperInfo = label .. itemStack .. " " .. itemContained .. " " .. itemSlot .. " " .. itemChem
table.insert(parts,
        else
frame:preprocess(
            wrapperInfo = label .. prefix
string.format(
        end
'{{#invoke:Prototypes/Хранилище/Предмет|main|framing|%s|%s}}',
        local imageParam = ""
slot, id
        if args["image"] ~= nil then
)
            if args["image"] ~= "" then
)
                imageParam = args["image"]
)
            else
end
                imageParam = ""
repoStr = table.concat(parts, ' ')
            end
end
        else
            imageParam = id .. ".png"
        end


        local linkParam = args["link"]
-- Основная сборка
        if not linkParam or linkParam == "" then
local parts = {}
            linkParam = itemName
if isVert then
        end
table.insert(parts,
string.format(
"<span style='display:inline-flex;flex-direction:column;align-items:center;'>%s<b>%s</b></span>",
img, labelOut
)
)
else
table.insert(parts,
string.format(
"<span style='display:inline-block;'>%s%s</span>", img, repoStr
)
)
table.insert(parts, labelOut)
end
-- Стек предметов
table.insert(parts,
frame:preprocess(
string.format(
'{{#invoke:Prototypes/Хранилище/Предмет|main|framing|stack|%s}}', id
)
)
)
table.insert(parts, prefix)


        return frame:preprocess(string.format("{{LinkCard|name=%s|image=%s|pixel=%s|link=%s|SideStyle=1}}", wrapperInfo, imageParam, pixel, linkParam))
-- Обёртка LinkCard
    end
if isWrap then
local side  = isVert and '' or '|горизонт_стиль=1'
local card = string.format(
'{{LinkCard|название=%s %s %s|пин=%s|изображение=%s|ссылка=%s%s}}',
labelOut,
frame:preprocess(string.format(
'{{#invoke:Prototypes/Хранилище/Предмет|main|framing|stack|%s}}', id
)),
prefix,
repoStr,
img,
linkTgt,
side
)
return frame:preprocess(card)
end


    return frame:preprocess("<span>" .. result .. "</span>")
-- Финальный вывод
return frame:preprocess('<span>' .. table.concat(parts, ' ') .. '</span>')
end
end


return p
return p