Модуль:Предмет
Материал из Space Station 14 Вики
Версия от 18:10, 24 июля 2025; Pok (обсуждение | вклад)
Для документации этого модуля может быть создана страница Модуль:Предмет/doc
local p = {}
local getArgs = require('Module:Arguments').getArgs
-- Форматирует файл изображения
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)
local argsRaw = getArgs(frame, { trim = false, removeBlanks = false })
local args = {}
for k, v in pairs(argsRaw) do
if v ~= '' then args[k] = v end
end
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
-- Получение имени
local nameStr = frame:preprocess(
string.format('{{#invoke:Entity Lookup|getname|%s}}', id)
)
-- Лейбл и ссылка
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
-- Изображение
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
-- Репозиторий
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
-- Стек предметов
table.insert(parts,
frame:preprocess(
string.format(
'{{#invoke:Prototypes/Хранилище/Предмет|main|framing|stack|%s}}', id
)
)
)
table.insert(parts, prefix)
-- Обёртка LinkCard
if isWrap then
local pixel = size:match('(%d+)') or ''
local side = isVert and '' or '|SideStyle=1'
local card = string.format(
'{{LinkCard|name=%s %s %s|pin=%s|image-full=%s|pixel=%s|link=%s%s}}',
labelOut,
frame:preprocess(string.format(
'{{#invoke:Prototypes/Хранилище/Предмет|main|framing|stack|%s}}', id
)),
prefix,
repoStr,
img,
pixel,
linkTgt,
side
)
return frame:preprocess(card)
end
-- Финальный вывод
return frame:preprocess('<span>' .. table.concat(parts, ' ') .. '</span>')
end
return p