Модуль:Предмет: различия между версиями

Материал из Space Station 14 Вики
мНет описания правки
мНет описания правки
Строка 3: Строка 3:


function p.main(frame)
function p.main(frame)
    -- Получение аргументов шаблона
     local args = getArgs(frame)
     local args = getArgs(frame)
     local argsWithWhitespace = getArgs(frame, { trim = false, removeBlanks = false })
     local argsWithWhitespace = getArgs(frame, { trim = false, removeBlanks = false })
   
 
     local id = args[1] or ""
     local id = args[1] or ""


     -- Инициализация данных предмета
     -- Получение данных о предмете через другие модули
     local itemStack = string.format('{{#invoke:Prototypes/Хранилище/Предмет|main|framing|stack|%s}}', id)
     local itemStack = string.format('{{#invoke:Prototypes/Хранилище/Предмет|main|framing|stack|%s}}', id)
     local itemContained = string.format('{{#invoke:Prototypes/Хранилище/Предмет|main|framing|contained|%s}}', id)
     local itemContained = string.format('{{#invoke:Prototypes/Хранилище/Предмет|main|framing|contained|%s}}', id)
Строка 16: Строка 17:


     local prefix = args[2] or ""
     local prefix = args[2] or ""
    local label = args["label"] or itemName
     local size = args["size"] or "32px"
     local size = args["size"] or "32px"


    -- Обработка ярлыка
     local input_label = argsWithWhitespace.label or argsWithWhitespace.l
     local input_label = argsWithWhitespace.label or argsWithWhitespace.l
     local label
     local label
     if input_label == nil then
     if input_label == nil then
         label = itemName
         label = itemName
    elseif input_label == "" then
        label = ""
     else
     else
if input_label == "" then
         label = input_label
            label = ""
         else
            label = input_label
        end
     end
     end


     -- Формирование строки изображения с проверкой на пустое значение
     -- Обработка изображения
     local input_image = argsWithWhitespace.image or argsWithWhitespace.img
     local input_image = argsWithWhitespace.image or argsWithWhitespace.img
     local image
     local image
     if input_image == nil then
     if input_image == nil then
        image = string.format("[[Файл:%s.png|%s]]", id, size)
    elseif input_image == "" then
         image = ""
         image = ""
     else
     else
if input_image == "" then
         image = string.format("[[Файл:%s|%s]]", input_image, size)
            image = string.format("[[Файл:%s.png|%s]]", id, size)
         else
            image = string.format("[[Файл:%s|%s]]", input_image, size)
        end
     end
     end


     -- Формирование ярлыка с ссылкой, если задана
     -- Формирование ярлыка-ссылки, если указан параметр link
     local labelOutput = label
     local labelOutput = label
     if args["link"] then
     if args["link"] then
Строка 51: Строка 48:
             linkTarget = itemName
             linkTarget = itemName
         end
         end
         if label ~= "" then
         if label ~= "" then
             labelOutput = string.format("[[%s|%s]]", linkTarget, label)
             labelOutput = string.format("[[%s|%s]]", linkTarget, label)
Строка 58: Строка 56:
     end
     end


     -- Сбор информации в зависимости от наличия репозитория
     -- Формирование описания репозитория
     local additionalInfo = args["repository"]
     local input_repository = argsWithWhitespace.repository
     if additionalInfo == nil then
    local repository
         additionalInfo = itemStack .. " " .. labelOutput .. " " .. itemContained .. " " .. itemSlot .. " " .. itemChem
     if input_repository == nil then
         repository = prefix .. " " .. labelOutput
     else
     else
         additionalInfo = prefix .. " " .. labelOutput
         repository = table.concat({
            itemStack,
            labelOutput,
            prefix,
            itemContained,
            itemSlot,
            itemChem
        }, " ")
     end
     end


     local result = image .. " " .. additionalInfo
     local result = image .. " " .. repository


     -- Формирование вывода через LinkCard, если указан непустой обёртка
     -- Обработка LinkCard, если указан параметр wrapper
     if args["wrapper"] and args["wrapper"] ~= "" then
    local input_wrapper = argsWithWhitespace.wrapper
        local pixel = string.match(size, "(%d+)")
    local wrapper
        local wrapperInfo
     if input_wrapper == nil then
        if args["repository"] then
        wrapper = ""
            wrapperInfo = label .. itemStack .. " " .. itemContained .. " " .. itemSlot .. " " .. itemChem
    else
        else
    local pixel = string.match(size, "(%d+)")
            wrapperInfo = label .. prefix
   
        end
    local wrapperInfo
 
    if input_repository == nil then
        local imageParam
        wrapperInfo = label .. prefix
        if args["image"] then
    else
            if args["image"] == "" then
        wrapperInfo = label .. itemStack .. " " .. prefix .. " " .. itemContained .. " " .. itemSlot .. " " .. itemChem
                imageParam = ""
    end
            else
                imageParam = args["image"]
    local imageParam = args["image"]
            end
    if imageParam == nil then
        else
        imageParam = id .. ".png"
            imageParam = id .. ".png"
    elseif imageParam == "" then
        end
        imageParam = ""
 
    end
        local linkParam = args["link"]
        if not linkParam or linkParam == "" then
    local linkParam = args["link"]
            linkParam = itemName
    if not linkParam or linkParam == "" then
        end
        linkParam = itemName
 
    end
        return frame:preprocess(string.format("{{LinkCard|name=%s|image=%s|pixel=%s|link=%s|SideStyle=1}}", wrapperInfo, imageParam, pixel, linkParam))
    end
    return frame:preprocess(string.format(
        "{{LinkCard|name=%s|image=%s|pixel=%s|link=%s|SideStyle=1}}",
        wrapperInfo, imageParam, pixel, linkParam
    ))
end


     return frame:preprocess("<span>" .. result .. "</span>")
     return frame:preprocess("<span>" .. result .. "</span>")

Версия от 08:27, 4 апреля 2025

Для документации этого модуля может быть создана страница Модуль:Предмет/doc

local p = {}
local getArgs = require('Module:Arguments').getArgs

function p.main(frame)
    -- Получение аргументов шаблона
    local args = getArgs(frame)
    local argsWithWhitespace = getArgs(frame, { trim = false, removeBlanks = false })

    local id = args[1] or ""

    -- Получение данных о предмете через другие модули
    local itemStack = string.format('{{#invoke:Prototypes/Хранилище/Предмет|main|framing|stack|%s}}', id)
    local itemContained = string.format('{{#invoke:Prototypes/Хранилище/Предмет|main|framing|contained|%s}}', id)
    local itemSlot = string.format('{{#invoke:Prototypes/Хранилище/Предмет|main|framing|slot|%s}}', id)
    local itemChem = string.format('{{СollapsibleMenu|color=#3e7c82|{{#invoke:Prototypes/Хранилище/Предмет|main|chem|%s}}}}', id)
    local itemName = string.format('{{#invoke:Entity Lookup|getname|%s}}', id)

    local prefix = args[2] or ""
    local size = args["size"] or "32px"

    -- Обработка ярлыка
    local input_label = argsWithWhitespace.label or argsWithWhitespace.l
    local label
    if input_label == nil then
        label = itemName
    elseif input_label == "" then
        label = ""
    else
        label = input_label
    end

    -- Обработка изображения
    local input_image = argsWithWhitespace.image or argsWithWhitespace.img
    local image
    if input_image == nil then
        image = string.format("[[Файл:%s.png|%s]]", id, size)
    elseif input_image == "" then
        image = ""
    else
        image = string.format("[[Файл:%s|%s]]", input_image, size)
    end

    -- Формирование ярлыка-ссылки, если указан параметр link
    local labelOutput = label
    if args["link"] then
        local linkTarget = args["link"]
        if linkTarget == "" then
            linkTarget = itemName
        end

        if label ~= "" then
            labelOutput = string.format("[[%s|%s]]", linkTarget, label)
        else
            labelOutput = ""
        end
    end

    -- Формирование описания репозитория
    local input_repository = argsWithWhitespace.repository
    local repository
    if input_repository == nil then
        repository = prefix .. " " .. labelOutput
    else
        repository = table.concat({
            itemStack,
            labelOutput,
            prefix,
            itemContained,
            itemSlot,
            itemChem
        }, " ")
    end

    local result = image .. " " .. repository

    -- Обработка LinkCard, если указан параметр wrapper
    local input_wrapper = argsWithWhitespace.wrapper
    local wrapper
    if input_wrapper == nil then
        wrapper = ""
    else
	    local pixel = string.match(size, "(%d+)")
	    
	    local wrapperInfo
	    if input_repository == nil then
	        wrapperInfo = label .. prefix
	    else
	        wrapperInfo = label .. itemStack .. " " .. prefix .. " " .. itemContained .. " " .. itemSlot .. " " .. itemChem
	    end
	
	    local imageParam = args["image"]
	    if imageParam == nil then
	        imageParam = id .. ".png"
	    elseif imageParam == "" then
	        imageParam = ""
	    end
	
	    local linkParam = args["link"]
	    if not linkParam or linkParam == "" then
	        linkParam = itemName
	    end
	
	    return frame:preprocess(string.format(
	        "{{LinkCard|name=%s|image=%s|pixel=%s|link=%s|SideStyle=1}}",
	        wrapperInfo, imageParam, pixel, linkParam
	    ))
	end

    return frame:preprocess("<span>" .. result .. "</span>")
end

return p