Модуль:Предмет: различия между версиями
Pok (обсуждение | вклад) мНет описания правки |
Pok (обсуждение | вклад) м параметры LinkCard |
||
| (не показано 7 промежуточных версий этого же участника) | |||
| Строка 2: | Строка 2: | ||
local getArgs = require('Module:Arguments').getArgs | local getArgs = require('Module:Arguments').getArgs | ||
-- | -- Форматирует файл изображения | ||
local function formatImage(file, size, link) | |||
return string.format('[[Файл:%s|%s|link=%s]]', file, size, link) | |||
local function formatImage(file, size) | |||
return string.format( | |||
end | end | ||
-- Оборачивает текст в | -- Оборачивает текст в ссылку | ||
local function wrapLink(text, target) | local function wrapLink(text, target) | ||
return (text ~= '' and target and string.format('[[%s|%s]]', target, text) or text) | |||
end | end | ||
function p.main(frame) | function p.main(frame) | ||
local argsRaw = getArgs(frame, { trim = false, removeBlanks = false }) | 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 id | local size = args.size or '32px' | ||
local size | local prefix = args[2] or '' | ||
local prefix = args[2] or | local isRepo = argsRaw.repository ~= nil | ||
local | local isWrap = argsRaw.wrapper ~= nil | ||
local isVert = argsRaw.vertical ~= nil | |||
-- | -- Получение имени | ||
local | local nameStr = frame:preprocess( | ||
string.format('{{#invoke:Entity Lookup|getname|%s}}', id) | |||
) | |||
-- | -- Лейбл и ссылка | ||
local | local labelRaw = argsRaw.label or argsRaw.l | ||
local label = | 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 | local imgFile = argsRaw.image or argsRaw.img or (id .. '.png') | ||
local | local img = imgFile ~= '' and formatImage(imgFile, size, linkTgt) or '' | ||
if | if argsRaw.imageTooltip then | ||
img = frame:preprocess( | |||
string.format( | |||
'{{#invoke:Entity Lookup|createimagetooltip|Файл:%s.png|%s|Мета=%s,link=}}', | |||
id, id, size | |||
) | |||
) | |||
end | end | ||
-- | -- Репозиторий | ||
if | 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 | end | ||
-- | -- Основная сборка | ||
local | 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 | else | ||
table.insert(parts, | |||
string.format( | |||
"<span style='display:inline-block;'>%s%s</span>", img, repoStr | |||
) | |||
) | |||
table.insert(parts, labelOut) | |||
end | end | ||
-- Стек предметов | |||
table.insert(parts, | |||
frame:preprocess( | |||
string.format( | |||
'{{#invoke:Prototypes/Хранилище/Предмет|main|framing|stack|%s}}', id | |||
) | |||
) | |||
) | |||
table.insert(parts, prefix) | |||
-- | -- Обёртка LinkCard | ||
if | if isWrap then | ||
local | local side = isVert and '' or '|горизонт_стиль=1' | ||
local card = string.format( | |||
'{{LinkCard|название=%s %s %s|пин=%s|изображение=%s|ссылка=%s%s}}', | |||
local | labelOut, | ||
frame:preprocess(string.format( | |||
'{{#invoke:Prototypes/Хранилище/Предмет|main|framing|stack|%s}}', id | |||
)), | |||
prefix, | |||
)) | repoStr, | ||
img, | |||
linkTgt, | |||
side | |||
) | |||
return frame:preprocess(card) | |||
end | end | ||
return frame:preprocess( | -- Финальный вывод | ||
return frame:preprocess('<span>' .. table.concat(parts, ' ') .. '</span>') | |||
end | end | ||
return p | return p | ||