|
|
| (не показано 6 промежуточных версий этого же участника) |
| Строка 2: |
Строка 2: |
| local getArgs = require('Module:Arguments').getArgs | | local getArgs = require('Module:Arguments').getArgs |
|
| |
|
| -- Формирует строку ссылки на изображение | | -- Форматирует файл изображения |
| local function formatImage(file, size) | | local function formatImage(file, size, link) |
| return string.format("[[Файл:%s|%s|link=]]", file, size)
| | return string.format('[[Файл:%s|%s|link=%s]]', file, size, link) |
| end | | 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) | | local function wrapLink(text, target) |
| if text == "" then
| | return (text ~= '' and target and string.format('[[%s|%s]]', target, text) or text) |
| return ""
| |
| else
| |
| return string.format("[[%s|%s]]", target, text)
| |
| end
| |
| end | | end |
|
| |
| --------------------------------------------------------------------------------
| |
| -- Основная функция модуля
| |
| --------------------------------------------------------------------------------
| |
|
| |
|
| function p.main(frame) | | function p.main(frame) |
| -- Получаем аргументы шаблона
| | local argsRaw = getArgs(frame, { trim = false, removeBlanks = false }) |
| local args = getArgs(frame)
| | local args = {} |
| local argsRaw = getArgs(frame, { trim = false, removeBlanks = false })
| | 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 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 id = args[1] or '' |
| local rawLabel = argsRaw.label or argsRaw.l
| | local size = args.size or '32px' |
| local label = rawLabel == nil and itemName or resolveArg(rawLabel, itemName)
| | local prefix = args[2] or '' |
| | local isRepo = argsRaw.repository ~= nil |
| | local isWrap = argsRaw.wrapper ~= nil |
| | local isVert = argsRaw.vertical ~= nil |
|
| |
|
| -- Обработка изображения
| | -- Получение имени |
| local rawImage = argsRaw.image or argsRaw.img
| | local nameStr = frame:preprocess( |
| local image
| | string.format('{{#invoke:Entity Lookup|getname|%s}}', id) |
| if rawImage == nil then
| | ) |
| image = formatImage(id .. ".png", size)
| |
| else
| |
| image = (rawImage ~= "") and formatImage(rawImage, size) or ""
| |
| end
| |
|
| |
|
| -- Переопределение изображения через imageTooltip
| | -- Лейбл и ссылка |
| if argsRaw.imageTooltip ~= nil then
| | local labelRaw = argsRaw.label or argsRaw.l |
| image = frame:preprocess(string.format(
| | local label = (labelRaw == nil and nameStr) or (labelRaw == '' and '' or labelRaw) |
| "{{#invoke:Entity Lookup|createimagetooltip|Файл:%s.png|%s|Мета=%s,link=}}",
| | local linkRaw = argsRaw.link |
| id, id, size
| | local linkTgt = linkRaw == nil and '' or (linkRaw == '' and nameStr or linkRaw) |
| ))
| | local labelOut = (linkRaw ~= nil) and wrapLink(label, linkTgt) or label |
| end
| |
|
| |
|
| -- Обработка ссылки
| | -- Изображение |
| local rawLink = argsRaw.link
| | local imgFile = argsRaw.image or argsRaw.img or (id .. '.png') |
| local labelOutput = (rawLink ~= nil)
| | local img = imgFile ~= '' and formatImage(imgFile, size, linkTgt) or '' |
| and wrapLink(label, (rawLink == "" and itemName or rawLink))
| | if argsRaw.imageTooltip then |
| or label
| | img = frame:preprocess( |
| | string.format( |
| | '{{#invoke:Entity Lookup|createimagetooltip|Файл:%s.png|%s|Мета=%s,link=}}', |
| | id, id, size |
| | ) |
| | ) |
| | end |
|
| |
|
| -- Формирование вывода
| | -- Репозиторий |
| local imagerepository, result
| | local repoStr = '' |
| if vertical ~= nil then
| | if isRepo then |
| -- Вертикальное отображение
| | local parts = {} |
| imagerepository = string.format(
| | for _, slot in ipairs({ 'contained', 'slot', 'chem' }) do |
| "<span style='display:inline-flex;flex-direction:column;align-items:center;'>%s<b>%s</b></span>",
| | table.insert(parts, |
| image, labelOutput
| | frame:preprocess( |
| )
| | string.format( |
| result = table.concat({ imagerepository, itemStack, prefix }, " ")
| | '{{#invoke:Prototypes/Хранилище/Предмет|main|framing|%s|%s}}', |
| else
| | slot, id |
| -- Горизонтальное отображение
| | ) |
| imagerepository = string.format("<span style='display:inline-block;'>%s%s</span>", image, repository)
| | ) |
| result = table.concat({ imagerepository, labelOutput, itemStack, prefix }, " ")
| | ) |
| end
| | end |
| | repoStr = table.concat(parts, ' ') |
| | end |
|
| |
|
| -- Обёртка LinkCard
| | -- Основная сборка |
| if argsRaw.wrapper ~= nil then
| | local parts = {} |
| local pixel = string.match(size, "(%d+)")
| | if isVert then |
| local linkParam = (rawLink ~= nil) and ((rawLink == "" and itemName) or rawLink) or ""
| | table.insert(parts, |
| local sideStyle = (vertical ~= nil) and "" or "|SideStyle=1"
| | 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) |
|
| |
|
| return frame:preprocess(string.format(
| | -- Обёртка LinkCard |
| "{{LinkCard|name=%s %s %s|pin=%s|image-full=%s|pixel=%s|link=%s%s}}",
| | if isWrap then |
| labelOutput, itemStack, prefix, repository, image, pixel, linkParam, sideStyle
| | local side = isVert and '' or '|горизонт_стиль=1' |
| ))
| | local card = string.format( |
| end
| | '{{LinkCard|название=%s %s %s|пин=%s|изображение=%s|ссылка=%s%s}}', |
| | labelOut, |
| | frame:preprocess(string.format( |
| | '{{#invoke:Prototypes/Хранилище/Предмет|main|framing|stack|%s}}', id |
| | )), |
| | prefix, |
| | repoStr, |
| | img, |
| | linkTgt, |
| | side |
| | ) |
| | return frame:preprocess(card) |
| | end |
|
| |
|
| return frame:preprocess(string.format("<span>%s</span>", result))
| | -- Финальный вывод |
| | return frame:preprocess('<span>' .. table.concat(parts, ' ') .. '</span>') |
| end | | end |
|
| |
|
| return p | | return p |