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

мНет описания правки
Нет описания правки
 
(не показаны 24 промежуточные версии этого же участника)
Строка 1: Строка 1:
local p = {}
local p = {}
local getArgs = require('Module:Arguments').getArgs
local getArgs = require('Module:Arguments').getArgs
local JsonPaths = require('Module:JsonPaths')
local function trim(value)
return mw.text.trim(value or '')
end
local function formatImage(file, size, link)
return string.format('[[Файл:%s|%s|link=%s]]', file, size, link)
end
local function wrapLink(text, target)
return (text ~= '' and target and string.format('[[%s|%s]]', target, text) or text)
end


function p.main(frame)
function p.main(frame)
    -- Получение аргументов шаблона
local argsRaw = getArgs(frame, { trim = false, removeBlanks = false })
    local args = getArgs(frame)
local args = {}
    local argsWithWhitespace = getArgs(frame, { trim = false, removeBlanks = false })
for k, v in pairs(argsRaw) do
if v ~= '' then
args[k] = v
end
end


    local id = args[1] or ""
local id = args[1] or ''
local size = args.size or '32px'
local prefix = args[2] or ''
local isRepo = argsRaw.repository ~= nil
local isWrap = argsRaw.wrapper ~= nil
local isVert = argsRaw.vertical ~= nil


    -- Получение данных о предмете через другие модули
-- Если id найден в JsonPaths, префиксы проекта нужны
    local itemStack = frame:preprocess(string.format('{{#invoke:Prototypes/Хранилище/Предмет|main|framing|stack|%s}}', id))
local hasId = (JsonPaths.has(id) == true)
    local itemContained = frame:preprocess(string.format('{{#invoke:Prototypes/Хранилище/Предмет|main|framing|contained|%s}}', id))
local projPrefix = hasId and JsonPaths.prefix() or ''
    local itemSlot = frame:preprocess(string.format('{{#invoke:Prototypes/Хранилище/Предмет|main|framing|slot|%s}}', id))
local projPrefixFile = hasId and JsonPaths.prefixFile() or ''
    local itemChem = frame:preprocess(string.format('{{СollapsibleMenu|color=#3e7c82|{{#invoke:Prototypes/Хранилище/Предмет|main|chem|%s}}}}', id))
    local itemName = frame:preprocess(string.format("{{#invoke:Entity Lookup|getname|%s}}", id))


    local prefix = args[2] or ""
-- Получение имени
    local size = args["size"] or "32px"
local nameStr = frame:preprocess(
string.format('{{#invoke:Entity Lookup|getname|%s}}', id)
)


    -- Обработка ярлыка
-- Лейбл и ссылка
    local input_label = argsWithWhitespace.label or argsWithWhitespace.l
local labelRaw = argsRaw.label or argsRaw.l
    local label
local label = (labelRaw == nil and nameStr) or (labelRaw == '' and '' or labelRaw)
    if input_label == nil then
local linkRaw = argsRaw.link
        label = itemName
local linkTgt = linkRaw == nil and '' or (linkRaw == '' and nameStr or linkRaw)
    elseif input_label == "" then
        label = ""
    else
        label = input_label
    end


    -- Обработка изображения
if linkTgt ~= '' and projPrefix ~= '' then
    local input_image = argsWithWhitespace.image or argsWithWhitespace.img
linkTgt = projPrefix .. linkTgt
    local image
end
    if input_image == nil then
        image = string.format("[[Файл:%s.png|%s]]", id, size)
    elseif input_image == "" then
image = ""
    else
        image = string.format("[[Файл:%s|%s]]", input_image, size)
    end


    -- Обработка Tooltip
local labelOut = (linkRaw ~= nil) and wrapLink(label, linkTgt) or label
    local input_imageTooltip = argsWithWhitespace.imageTooltip
    local imageTooltip
    if input_imageTooltip ~= nil then
        image = string.format("{{#invoke:Entity Lookup|createimagetooltip|Файл:%s.png|%s|Мета=%s}}", id, id, size)
    end
   
    -- Формирование ярлыка-ссылки, если указан параметр link
    local labelOutput = label
    if args["link"] then
        local linkTarget = args["link"]
        if linkTarget == "" then
            linkTarget = itemName
        end


        if label ~= "" then
-- Изображение
            labelOutput = string.format("[[%s|%s]]", linkTarget, label)
local imgFile = argsRaw.image or argsRaw.img or (id .. '.png')
        else
if imgFile ~= '' and projPrefixFile ~= '' then
            labelOutput = ""
imgFile = projPrefixFile .. imgFile
        end
end
    end


    -- Формирование описания репозитория
local img = imgFile ~= '' and formatImage(imgFile, size, linkTgt) or ''
    local input_repository = argsWithWhitespace.repository
if argsRaw.imageTooltip then
    local repository
img = frame:preprocess(
    if input_repository == nil then
string.format(
        repository = labelOutput .. prefix
'{{#invoke:Entity Lookup|createimagetooltip|Файл:%s|%s|Мета=%s,link=}}',
    else
imgFile, id, size
        repository = table.concat({
)
            itemContained,
)
            itemSlot,
end
            itemChem
 
        }, " ")
-- Репозиторий
    end
local repoStr = ''
if isRepo then
local parts = {}
for _, slot in ipairs({ 'contained', 'slot', 'chem' }) do
table.insert(parts,
frame:preprocess(
string.format(
'{{#invoke:Prototypes/Хранилище/Предмет|main|framing|%s|%s}}',
slot, id
)
)
)
end
repoStr = table.concat(parts, ' ')
end
 
-- Основная сборка
local parts = {}
if isVert then
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


    local result = image .. " " .. labelOutput .. " " .. itemStack .. " " .. prefix .. " " .. repository
-- Стек предметов
table.insert(parts,
frame:preprocess(
string.format(
'{{#invoke:Prototypes/Хранилище/Предмет|main|framing|stack|%s}}', id
)
)
)
table.insert(parts, prefix)


    -- Обработка LinkCard, если указан параметр wrapper
-- Обёртка LinkCard
    local input_wrapper = argsWithWhitespace.wrapper
if isWrap then
    local wrapper
local side = isVert and '' or '|горизонт_стиль=1'
    if input_wrapper == nil then
local card = string.format(
        wrapper = ""
'{{LinkCard|название=%s %s %s|пин=%s|изображение=%s|ссылка=%s%s}}',
    else
labelOut,
    local pixel = string.match(size, "(%d+)")
frame:preprocess(string.format(
'{{#invoke:Prototypes/Хранилище/Предмет|main|framing|stack|%s}}', id
    local imageParam = args["image"]
)),
    if imageParam == nil then
prefix,
        imageParam = id .. ".png"
repoStr,
    elseif imageParam == "" then
img,
        imageParam = ""
linkTgt,
    end
side
)
    local linkParam = args["link"]
return frame:preprocess(card)
    if not linkParam or linkParam == "" then
        linkParam = itemName
    end
    return frame:preprocess(string.format(
        "{{LinkCard|name=%s %s %s|pin=%s|image=%s|pixel=%s|link=%s|SideStyle=1}}",
        labelOutput, itemStack, prefix, repository, imageParam, pixel, linkParam
    ))
end
end


    return frame:preprocess("<span>" .. result .. "</span>")
return frame:preprocess('<span>' .. table.concat(parts, ' ') .. '</span>')
end
end


return p
return p