Модуль:Предмет: различия между версиями
Pok (обсуждение | вклад) мНет описания правки |
Pok (обсуждение | вклад) мНет описания правки |
||
| Строка 2: | Строка 2: | ||
local getArgs = require('Module:Arguments').getArgs | local getArgs = require('Module:Arguments').getArgs | ||
-- | -- Общий кэширующий инвокер, возвращает строку #invoke | ||
local function makeInvoker( | local function makeInvoker() | ||
local cache = {} | local cache = {} | ||
return function(module, method, sub, extra, id) | return function(module, method, sub, extra, id) | ||
local key = table.concat({module, method, sub or "", extra or "", id or ""}, "|") | local key = table.concat({module, method, sub or "", extra or "", id or ""}, "|") | ||
if not cache[key] then | if not cache[key] then | ||
cache[key] = string.format("{{#invoke:%s|%s|%s|%s|%s}}", | |||
module, method, sub or "", extra or "", id or "") | |||
end | end | ||
return cache[key] | return cache[key] | ||
| Строка 20: | Строка 20: | ||
end | end | ||
-- Обёртка для ссылки | |||
-- Обёртка для | |||
local function wrapLink(text, target) | local function wrapLink(text, target) | ||
if text == "" then return "" end | |||
return string.format("[[%s|%s]]", target, 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 = {} | local args = {} | ||
for k, v in pairs(argsRaw) do if v ~= "" then args[k] = v end end | 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 | local repoM = argsRaw.repository ~= nil | ||
local | local wrapM = argsRaw.wrapper ~= nil | ||
local | local vert = argsRaw.vertical ~= nil | ||
local | |||
local inv = makeInvoker() | |||
local | |||
-- | -- Имя и метка | ||
local | local rawLabel = argsRaw.label or argsRaw.l | ||
local nameStr = inv("Entity Lookup", "getname", id) | |||
local label = rawLabel == nil and nameStr or (rawLabel == "" and "" or rawLabel) | |||
local | local rawLink = argsRaw.link | ||
local labelOut = rawLink ~= nil | |||
and wrapLink(label, rawLink == "" and nameStr or rawLink) | |||
local | or label | ||
local | |||
local | |||
-- Изображение | -- Изображение | ||
local | local rawImg = argsRaw.image or argsRaw.img | ||
local | local img = "" | ||
if rawImg == nil then | |||
img = formatImage(id..".png", size) | |||
elseif rawImg ~= "" then | |||
img = formatImage(rawImg, size) | |||
end | |||
if argsRaw.imageTooltip then | if argsRaw.imageTooltip then | ||
img = string.format( | |||
"{{#invoke:Entity Lookup|createimagetooltip|Файл:%s.png|%s|Мета=%s,link=}}", | "{{#invoke:Entity Lookup|createimagetooltip|Файл:%s.png|%s|Мета=%s,link=}}", | ||
id, id, size | id, id, size | ||
) | |||
end | end | ||
-- Репозиторий — только при необходимости | |||
-- Репозиторий | |||
local repoStr = "" | local repoStr = "" | ||
if | if repoM then | ||
repoStr = table.concat({ | repoStr = table.concat({ | ||
inv("Prototypes/Хранилище/Предмет", "main", "framing|contained", id), | |||
inv("Prototypes/Хранилище/Предмет", "main", "framing|slot", id), | |||
string.format( | |||
"{{СollapsibleMenu|color=#3e7c82|%s}}", | |||
inv("Prototypes/Хранилище/Предмет", "main", "chem", id) | |||
) | |||
}, " ") | |||
end | end | ||
-- | -- Основная сборка | ||
if | local parts = {} | ||
table.insert(parts, string.format( | |||
if vert then | |||
table.insert(parts, | |||
string.format( | |||
table.insert(parts, | "<span style='display:inline-flex;flex-direction:column;align-items:center;'>%s<b>%s</b></span>", | ||
img, labelOut | |||
) | |||
) | |||
table.insert(parts, inv("Prototypes/Хранилище/Предмет", "main", "framing|stack", id)) | |||
table.insert(parts, prefix) | table.insert(parts, prefix) | ||
else | else | ||
table.insert(parts, string.format( | table.insert(parts, | ||
string.format("<span style='display:inline-block;'>%s%s</span>", img, repoStr) | |||
) | |||
table.insert(parts, labelOut) | table.insert(parts, labelOut) | ||
table.insert(parts, | table.insert(parts, inv("Prototypes/Хранилище/Предмет", "main", "framing|stack", id)) | ||
table.insert(parts, prefix) | table.insert(parts, prefix) | ||
end | end | ||
-- Обёртка LinkCard | -- Обёртка LinkCard | ||
if | if wrapM then | ||
local pixel = size:match("(%d+)") | local pixel = size:match("(%d+)") | ||
local linkParam = rawLink and (rawLink == "" and | local linkParam = rawLink and (rawLink == "" and nameStr or rawLink) or "" | ||
local | local side = vert and "" or "|SideStyle=1" | ||
local card = string.format( | local card = string.format( | ||
"{{LinkCard|name=%s %s %s|pin=%s|image-full=%s|pixel=%s|link=%s%s}}", | "{{LinkCard|name=%s %s %s|pin=%s|image-full=%s|pixel=%s|link=%s%s}}", | ||
labelOut, | labelOut, | ||
inv("Prototypes/Хранилище/Предмет", "main", "framing|stack", id), | |||
prefix, | |||
repoStr, | |||
img, | |||
pixel, | |||
linkParam, | |||
side | |||
) | ) | ||
return frame:preprocess(card) | return frame:preprocess(card) | ||
end | end | ||
-- Финальный вывод | -- Финальный вывод | ||
return frame:preprocess | return frame:preprocess("<span>" .. table.concat(parts, " ") .. "</span>") | ||
end | end | ||
return p | return p | ||