Модуль:Предмет: различия между версиями
Материал из Space Station 14 Вики
Pok (обсуждение | вклад) мНет описания правки |
Pok (обсуждение | вклад) мНет описания правки |
||
| (не показано 5 промежуточных версий этого же участника) | |||
| Строка 2: | Строка 2: | ||
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]]", file, size) | return string.format("[[Файл:%s|%s|link=]]", file, size) | ||
end | end | ||
-- | -- Возвращает значение аргумента: если nil, то значение по умолчанию; если пустая строка, то пустую строку; иначе само значение | ||
local function resolveArg(input, defaultValue) | local function resolveArg(input, defaultValue) | ||
if input == nil then | if input == nil then | ||
| Строка 17: | Строка 21: | ||
end | end | ||
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) | function p.main(frame) | ||
| Строка 23: | Строка 46: | ||
local argsRaw = getArgs(frame, { trim = false, removeBlanks = false }) | local argsRaw = getArgs(frame, { trim = false, removeBlanks = false }) | ||
local id = args[1] or "" | -- Основные параметры | ||
local size = args["size"] or "32px" | local id = args[1] or "" | ||
local prefix = args[2] or "" | local size = args["size"] or "32px" | ||
local prefix = args[2] or "" | |||
local vertical = argsRaw.vertical | |||
local | |||
local itemStack = invoke("Prototypes/Хранилище/Предмет", "main", "framing", "stack", id) | -- Получаем данные о предмете через вызовы других модулей | ||
local itemContained = invoke("Prototypes/Хранилище/Предмет", "main", "framing", "contained", id) | local itemStack = invoke(frame, "Prototypes/Хранилище/Предмет", "main", "framing", "stack", id) | ||
local itemSlot = invoke("Prototypes/Хранилище/Предмет", "main", "framing", "slot", id) | local itemContained = invoke(frame, "Prototypes/Хранилище/Предмет", "main", "framing", "contained", id) | ||
-- Для химии оборачиваем в CollapsibleMenu | local itemSlot = invoke(frame, "Prototypes/Хранилище/Предмет", "main", "framing", "slot", id) | ||
local itemChem | -- Для химии оборачиваем в CollapsibleMenu (из-за ошибки в модуле) | ||
local itemName | 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)) | |||
-- Обработка ярлыка | -- Обработка ярлыка | ||
| Строка 45: | Строка 66: | ||
-- Обработка изображения | -- Обработка изображения | ||
local rawImage = argsRaw.image or argsRaw.img | local rawImage = argsRaw.image or argsRaw.img | ||
local image | local image | ||
if rawImage == nil then | if rawImage == nil then | ||
image = formatImage(id .. ".png", size) | image = formatImage(id .. ".png", size) | ||
else | else | ||
image = | image = (rawImage ~= "") and formatImage(rawImage, size) or "" | ||
end | end | ||
-- | -- Если указан imageTooltip, переопределяем изображение | ||
if argsRaw.imageTooltip ~= nil then | if argsRaw.imageTooltip ~= nil then | ||
image = frame:preprocess(string.format( | image = frame:preprocess(string.format( | ||
"{{#invoke:Entity Lookup|createimagetooltip|Файл:%s.png|%s|Мета=%s}}", id, id, size)) | "{{#invoke:Entity Lookup|createimagetooltip|Файл:%s.png|%s|Мета=%s,link=}}", | ||
id, id, size | |||
)) | |||
end | end | ||
-- Обработка | -- Обработка ссылки | ||
local rawLink = argsRaw.link | local rawLink = argsRaw.link | ||
local labelOutput = | local labelOutput = (rawLink ~= nil) and wrapLink(label, (rawLink == "" and itemName or rawLink)) or label | ||
-- Формирование описания репозитория | -- Формирование описания репозитория | ||
local repository = "" | local repository = (argsRaw.repository == nil) and prefix or table.concat({ itemContained, itemSlot, itemChem }, " ") | ||
if | |||
-- Формирование итогового блока с изображением и описанием | |||
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 | else | ||
repository = table.concat({ | imagerepository = string.format("<span>%s%s</span>", image, repository) | ||
result = table.concat({ imagerepository, labelOutput, itemStack, prefix }, " ") | |||
end | end | ||
-- Если задан параметр wrapper, формируем LinkCard | |||
-- Если | |||
if argsRaw.wrapper ~= nil then | if argsRaw.wrapper ~= nil then | ||
local pixel = string.match(size, "(%d+)") | local pixel = string.match(size, "(%d+)") | ||
local linkParam = "" | local linkParam = (rawLink ~= nil) and ((rawLink == "" and itemName) or rawLink) or "" | ||
-- Если | -- Если vertical не задан, добавляем SideStyle=1 | ||
local sideStyle = (vertical ~= nil) and "" or "|SideStyle=1" | |||
return frame:preprocess(string.format( | return frame:preprocess(string.format( | ||
"{{LinkCard|name=%s %s %s|pin=%s|image-full=%s|pixel=%s|link=%s | "{{LinkCard|name=%s %s %s|pin=%s|image-full=%s|pixel=%s|link=%s%s}}", | ||
labelOutput, itemStack, prefix, repository, image, pixel, linkParam | labelOutput, itemStack, prefix, repository, image, pixel, linkParam, sideStyle | ||
)) | )) | ||
end | end | ||
return frame:preprocess("<span> | return frame:preprocess(string.format("<span>%s</span>", result)) | ||
end | end | ||
return p | return p | ||
Версия от 14:39, 7 апреля 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 itemContained = invoke(frame, "Prototypes/Хранилище/Предмет", "main", "framing", "contained", id)
local itemSlot = invoke(frame, "Prototypes/Хранилище/Предмет", "main", "framing", "slot", id)
-- Для химии оборачиваем в CollapsibleMenu (из-за ошибки в модуле)
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 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 repository = (argsRaw.repository == nil) and prefix or table.concat({ itemContained, itemSlot, itemChem }, " ")
-- Формирование итогового блока с изображением и описанием
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>%s%s</span>", image, repository)
result = table.concat({ imagerepository, labelOutput, itemStack, prefix }, " ")
end
-- Если задан параметр wrapper, формируем LinkCard
if argsRaw.wrapper ~= nil then
local pixel = string.match(size, "(%d+)")
local linkParam = (rawLink ~= nil) and ((rawLink == "" and itemName) or rawLink) or ""
-- Если vertical не задан, добавляем SideStyle=1
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