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

мНет описания правки
м параметры LinkCard
 
(не показано 40 промежуточных версий этого же участника)
Строка 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 argsWithWhitespace = getArgs(frame, { trim = false, removeBlanks = false })
return string.format('[[Файл:%s|%s|link=%s]]', file, size, link)
   
end
    local id = args[1] or ""


    -- Инициализация данных предмета
-- Оборачивает текст в ссылку
    local itemStack = string.format('{{#invoke:Prototypes/Хранилище/Предмет|main|framing|stack|%s}}', id)
local function wrapLink(text, target)
    local itemContained = string.format('{{#invoke:Prototypes/Хранилище/Предмет|main|framing|contained|%s}}', id)
return (text ~= '' and target and string.format('[[%s|%s]]', target, text) or text)
    local itemSlot = string.format('{{#invoke:Prototypes/Хранилище/Предмет|main|framing|slot|%s}}', id)
end
    local itemChem = string.format('{{СollapsibleMenu|color=#3e7c82|{{#invoke:Prototypes/Хранилище/Предмет|main|chem|%s}}}}', id)
    local itemName = string.format('{{#invoke:Entity Lookup|getname|" .. id .. "}}', 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
    local input_label = argsWithWhitespace.label or argsWithWhitespace.l
if v ~= '' then args[k] = v end
    local label
end
    if input_label == nil then
        label = itemName
    else
if input_label == "" then
            label = ""
        else
            label = input_label
        end
    end
 
    -- Формирование строки изображения с проверкой на пустое значение
    local input_image = argsWithWhitespace.image or argsWithWhitespace.img
    local image
    if input_image == nil then
        image = ""
    else
if input_image == "" then
            image = string.format("[[Файл:%s.png|%s]]", id, size)
        else
            image = string.format("[[Файл:%s|%s]]", input_image, size)
        end
    end


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


    -- Сбор информации в зависимости от наличия репозитория
-- Получение имени
    local additionalInfo = args["repository"]
local nameStr = frame:preprocess(
    if additionalInfo == nil then
string.format('{{#invoke:Entity Lookup|getname|%s}}', id)
        additionalInfo = itemStack .. " " .. labelOutput .. " " .. itemContained .. " " .. itemSlot .. " " .. itemChem
)
    else
        additionalInfo = prefix .. " " .. labelOutput
    end


    local result = image .. " " .. additionalInfo
-- Лейбл и ссылка
local labelRaw = argsRaw.label or argsRaw.l
local label = (labelRaw == nil and nameStr) or (labelRaw == '' and '' or labelRaw)
local linkRaw  = argsRaw.link
local linkTgt  = linkRaw == nil and '' or (linkRaw == '' and nameStr or linkRaw)
local labelOut = (linkRaw ~= nil) and wrapLink(label, linkTgt) or label


    -- Формирование вывода через LinkCard, если указан непустой обёртка
-- Изображение
    if args["wrapper"] and args["wrapper"] ~= "" then
local imgFile = argsRaw.image or argsRaw.img or (id .. '.png')
        local pixel = string.match(size, "(%d+)")
local img   = imgFile ~= '' and formatImage(imgFile, size, linkTgt) or ''
        local wrapperInfo
if argsRaw.imageTooltip then
        if args["repository"] then
img = frame:preprocess(
            wrapperInfo = label .. itemStack .. " " .. itemContained .. " " .. itemSlot .. " " .. itemChem
string.format(
        else
'{{#invoke:Entity Lookup|createimagetooltip|Файл:%s.png|%s|Мета=%s,link=}}',
            wrapperInfo = label .. prefix
id, id, size
        end
)
)
end


        local imageParam
-- Репозиторий
        if args["image"] then
local repoStr = ''
            if args["image"] == "" then
if isRepo then
                imageParam = ""
local parts = {}
            else
for _, slot in ipairs({ 'contained', 'slot', 'chem' }) do
                imageParam = args["image"]
table.insert(parts,
            end
frame:preprocess(
        else
string.format(
            imageParam = id .. ".png"
'{{#invoke:Prototypes/Хранилище/Предмет|main|framing|%s|%s}}',
        end
slot, id
)
)
)
end
repoStr = table.concat(parts, ' ')
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