Модуль:Предмет: различия между версиями
Материал из Space Station 14 Вики
Pok (обсуждение | вклад) мНет описания правки |
Pok (обсуждение | вклад) мНет описания правки |
||
| Строка 1: | Строка 1: | ||
local p = {} | local p = {} | ||
local getArgs = require('Module:Arguments').getArgs | local getArgs = require('Module:Arguments').getArgs | ||
-- Формирует строку ссылки на изображение | -- Формирует строку ссылки на изображение | ||
local function formatImage(file, size) | local function formatImage(file, size) | ||
return string.format("[[Файл:%s|%s|link=]]", file, size) | |||
end | end | ||
-- Возвращает значение аргумента: если nil, то значение по умолчанию; если пустая строка, то пустую строку; иначе само значение | -- Возвращает значение аргумента: если nil, то значение по умолчанию; если пустая строка, то пустую строку; иначе само значение | ||
local function resolveArg(input, defaultValue) | local function resolveArg(input, defaultValue) | ||
if input == nil then | |||
return defaultValue | |||
elseif input == "" then | |||
return "" | |||
else | |||
return input | |||
end | |||
end | end | ||
-- Обёртка для вызова другого модуля через #invoke | -- Обёртка для вызова другого модуля через #invoke | ||
local function invoke(frame, module, method, subMethod, extra, id) | local function invoke(frame, module, method, subMethod, extra, id) | ||
local invocation = string.format("{{#invoke:%s|%s|%s|%s|%s}}", module, method, subMethod, extra, id) | |||
return frame:preprocess(invocation) | |||
end | end | ||
-- Оборачивает текст в вики-ссылку, если цель задана | -- Оборачивает текст в вики-ссылку, если цель задана | ||
local function wrapLink(text, target) | local function wrapLink(text, target) | ||
if text == "" then | |||
return "" | |||
else | |||
return string.format("[[%s|%s]]", target, text) | |||
end | |||
end | end | ||
| Строка 42: | Строка 38: | ||
function p.main(frame) | function p.main(frame) | ||
-- Получаем аргументы шаблона | |||
local args = getArgs(frame) | |||
local argsRaw = getArgs(frame, { trim = false, removeBlanks = false }) | |||
-- Основные параметры | |||
local id = args[1] or "" | |||
local size = args["size"] or "32px" | |||
local prefix = args[2] or "" | |||
local vertical = argsRaw.vertical | |||
-- Всегда получаем данные стэка | |||
local itemStack = invoke(frame, "Prototypes/Хранилище/Предмет", "main", "framing", "stack", id) | |||
-- Режим репозитория: дополнительные данные только при необходимости | |||
local repositoryMode = argsRaw.repository ~= nil | |||
local repository = prefix | |||
local itemContained, itemSlot, itemChem | |||
if repositoryMode then | |||
-- Получаем данные о вложенных предметах, слоте и химии | |||
itemContained = invoke(frame, "Prototypes/Хранилище/Предмет", "main", "framing", "contained", id) | |||
itemSlot = invoke(frame, "Prototypes/Хранилище/Предмет", "main", "framing", "slot", id) | |||
-- Для химии оборачиваем в CollapsibleMenu (из-за ошибки в модуле) | |||
itemChem = frame:preprocess(string.format( | |||
"{{СollapsibleMenu|color=#3e7c82|{{#invoke:Prototypes/Хранилище/Предмет|main|chem|%s}}}}", id | |||
)) | |||
repository = table.concat({ itemContained, itemSlot, itemChem }, " ") | |||
end | |||
-- Получаем имя предмета | |||
local itemName = frame:preprocess(string.format("{{#invoke:Entity Lookup|getname|%s}}", id)) | |||
-- Обработка ярлыка | |||
local rawLabel = argsRaw.label or argsRaw.l | |||
local label = rawLabel == nil and itemName or resolveArg(rawLabel, itemName) | |||
-- Обработка изображения | |||
local rawImage = argsRaw.image or argsRaw.img | |||
local image | |||
if rawImage == nil then | |||
image = formatImage(id .. ".png", size) | |||
else | |||
image = (rawImage ~= "") and formatImage(rawImage, size) or "" | |||
end | |||
-- Переопределение изображения через imageTooltip | |||
if argsRaw.imageTooltip ~= nil then | |||
image = frame:preprocess(string.format( | |||
"{{#invoke:Entity Lookup|createimagetooltip|Файл:%s.png|%s|Мета=%s,link=}}", | |||
id, id, size | |||
)) | |||
end | |||
-- Обработка ссылки | |||
local rawLink = argsRaw.link | |||
local labelOutput = (rawLink ~= nil) | |||
and wrapLink(label, (rawLink == "" and itemName or rawLink)) | |||
or label | |||
-- Формирование вывода | |||
local imagerepository, result | |||
if vertical ~= nil then | |||
-- Вертикальное отображение | |||
imagerepository = string.format( | |||
"<span style='display:inline-flex;flex-direction:column;align-items:center;'>%s<b>%s</b></span>", | |||
image, labelOutput | |||
) | |||
result = table.concat({ imagerepository, itemStack, prefix }, " ") | |||
else | |||
-- Горизонтальное отображение | |||
imagerepository = string.format("<span style='display:inline-block;'>%s%s</span>", image, repository) | |||
result = table.concat({ imagerepository, labelOutput, itemStack, prefix }, " ") | |||
end | |||
-- Обёртка LinkCard | |||
if argsRaw.wrapper ~= nil then | |||
local pixel = string.match(size, "(%d+)") | |||
local linkParam = (rawLink ~= nil) and ((rawLink == "" and itemName) or rawLink) or "" | |||
local sideStyle = (vertical ~= nil) and "" or "|SideStyle=1" | |||
return frame:preprocess(string.format( | |||
"{{LinkCard|name=%s %s %s|pin=%s|image-full=%s|pixel=%s|link=%s%s}}", | |||
labelOutput, itemStack, prefix, repository, image, pixel, linkParam, sideStyle | |||
)) | |||
end | |||
return frame:preprocess(string.format("<span>%s</span>", result)) | |||
end | end | ||
return p | return p | ||
Версия от 09:05, 16 апреля 2025
Для документации этого модуля может быть создана страница Модуль:Предмет/doc
local p = {}
local getArgs = require('Module:Arguments').getArgs
-- Формирует строку ссылки на изображение
local function formatImage(file, size)
return string.format("[[Файл:%s|%s|link=]]", file, size)
end
-- Возвращает значение аргумента: если nil, то значение по умолчанию; если пустая строка, то пустую строку; иначе само значение
local function resolveArg(input, defaultValue)
if input == nil then
return defaultValue
elseif input == "" then
return ""
else
return input
end
end
-- Обёртка для вызова другого модуля через #invoke
local function invoke(frame, module, method, subMethod, extra, id)
local invocation = string.format("{{#invoke:%s|%s|%s|%s|%s}}", module, method, subMethod, extra, id)
return frame:preprocess(invocation)
end
-- Оборачивает текст в вики-ссылку, если цель задана
local function wrapLink(text, target)
if text == "" then
return ""
else
return string.format("[[%s|%s]]", target, text)
end
end
--------------------------------------------------------------------------------
-- Основная функция модуля
--------------------------------------------------------------------------------
function p.main(frame)
-- Получаем аргументы шаблона
local args = getArgs(frame)
local argsRaw = getArgs(frame, { trim = false, removeBlanks = false })
-- Основные параметры
local id = args[1] or ""
local size = args["size"] or "32px"
local prefix = args[2] or ""
local vertical = argsRaw.vertical
-- Всегда получаем данные стэка
local itemStack = invoke(frame, "Prototypes/Хранилище/Предмет", "main", "framing", "stack", id)
-- Режим репозитория: дополнительные данные только при необходимости
local repositoryMode = argsRaw.repository ~= nil
local repository = prefix
local itemContained, itemSlot, itemChem
if repositoryMode then
-- Получаем данные о вложенных предметах, слоте и химии
itemContained = invoke(frame, "Prototypes/Хранилище/Предмет", "main", "framing", "contained", id)
itemSlot = invoke(frame, "Prototypes/Хранилище/Предмет", "main", "framing", "slot", id)
-- Для химии оборачиваем в CollapsibleMenu (из-за ошибки в модуле)
itemChem = frame:preprocess(string.format(
"{{СollapsibleMenu|color=#3e7c82|{{#invoke:Prototypes/Хранилище/Предмет|main|chem|%s}}}}", id
))
repository = table.concat({ itemContained, itemSlot, itemChem }, " ")
end
-- Получаем имя предмета
local itemName = frame:preprocess(string.format("{{#invoke:Entity Lookup|getname|%s}}", id))
-- Обработка ярлыка
local rawLabel = argsRaw.label or argsRaw.l
local label = rawLabel == nil and itemName or resolveArg(rawLabel, itemName)
-- Обработка изображения
local rawImage = argsRaw.image or argsRaw.img
local image
if rawImage == nil then
image = formatImage(id .. ".png", size)
else
image = (rawImage ~= "") and formatImage(rawImage, size) or ""
end
-- Переопределение изображения через imageTooltip
if argsRaw.imageTooltip ~= nil then
image = frame:preprocess(string.format(
"{{#invoke:Entity Lookup|createimagetooltip|Файл:%s.png|%s|Мета=%s,link=}}",
id, id, size
))
end
-- Обработка ссылки
local rawLink = argsRaw.link
local labelOutput = (rawLink ~= nil)
and wrapLink(label, (rawLink == "" and itemName or rawLink))
or label
-- Формирование вывода
local imagerepository, result
if vertical ~= nil then
-- Вертикальное отображение
imagerepository = string.format(
"<span style='display:inline-flex;flex-direction:column;align-items:center;'>%s<b>%s</b></span>",
image, labelOutput
)
result = table.concat({ imagerepository, itemStack, prefix }, " ")
else
-- Горизонтальное отображение
imagerepository = string.format("<span style='display:inline-block;'>%s%s</span>", image, repository)
result = table.concat({ imagerepository, labelOutput, itemStack, prefix }, " ")
end
-- Обёртка LinkCard
if argsRaw.wrapper ~= nil then
local pixel = string.match(size, "(%d+)")
local linkParam = (rawLink ~= nil) and ((rawLink == "" and itemName) or rawLink) or ""
local sideStyle = (vertical ~= nil) and "" or "|SideStyle=1"
return frame:preprocess(string.format(
"{{LinkCard|name=%s %s %s|pin=%s|image-full=%s|pixel=%s|link=%s%s}}",
labelOutput, itemStack, prefix, repository, image, pixel, linkParam, sideStyle
))
end
return frame:preprocess(string.format("<span>%s</span>", result))
end
return p