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

Материал из Space Station 14 Вики
мНет описания правки
м параметры LinkCard
 
(не показана 51 промежуточная версия этого же участника)
Строка 1: Строка 1:
local p = {}
local p = {}
local getArgs = require('Module:Arguments').getArgs
local getArgs = require('Module:Arguments').getArgs
-- Форматирует файл изображения
local function formatImage(file, size, link)
return string.format('[[Файл:%s|%s|link=%s]]', file, size, link)
end
-- Оборачивает текст в ссылку
local function wrapLink(text, target)
return (text ~= '' and target and string.format('[[%s|%s]]', target, text) or text)
end


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 args = {}
 
for k, v in pairs(argsRaw) do
    -- Инициализация данных предмета
if v ~= '' then args[k] = v end
    local itemStack = frame:preprocess(string.format('{{#invoke:Prototypes/Хранилище/Предмет|main|framing|stack|%s}}', id))
end
    local itemContained = frame:preprocess(string.format('{{#invoke:Prototypes/Хранилище/Предмет|main|framing|contained|%s}}', id))
    local itemSlot = frame:preprocess(string.format('{{#invoke:Prototypes/Хранилище/Предмет|main|framing|slot|%s}}', id))
    local itemChem = frame:preprocess(string.format('{{СollapsibleMenu|color=#3e7c82|{{#invoke:Prototypes/Хранилище/Предмет|main|chem|%s}}}}', id))
    local itemName = frame:preprocess("{{#invoke:Entity Lookup|getname|" .. id .. "}}")


    local prefix = args[2] or ""
local id   = args[1] or ''
    local label = args["label"] or itemName
local size = args.size or '32px'
    local size = args["size"] or "32px"
local prefix  = args[2] or ''
local isRepo  = argsRaw.repository ~= nil
local isWrap  = argsRaw.wrapper ~= nil
local isVert  = argsRaw.vertical  ~= nil


    -- Формирование строки изображения
-- Получение имени
    local image
local nameStr = frame:preprocess(
    if args["image"] then
string.format('{{#invoke:Entity Lookup|getname|%s}}', id)
    if image ~= "" then
)
            image = string.format("[[Файл:%s|%s]]", args["image"], size)
        else
            image = ""
        end
    else
        image = string.format("[[Файл:%s.png|%s]]", id, size)
    end


    -- Формирование ярлыка с ссылкой, если задана
-- Лейбл и ссылка
    local labelOutput = label
local labelRaw = argsRaw.label or argsRaw.l
    if args["link"] then
local label = (labelRaw == nil and nameStr) or (labelRaw == '' and '' or labelRaw)
        local linkTarget = args["link"]
local linkRaw  = argsRaw.link
        if linkTarget == "" then
local linkTgt  = linkRaw == nil and '' or (linkRaw == '' and nameStr or linkRaw)
            linkTarget = itemName
local labelOut = (linkRaw ~= nil) and wrapLink(label, linkTgt) or label
        end
        if label ~= "" then
            labelOutput = string.format("[[%s|%s]]", linkTarget, label)
        else
            labelOutput = ""
        end
    end


    -- Сбор информации в зависимости от наличия репозитория
-- Изображение
    local additionalInfo
local imgFile = argsRaw.image or argsRaw.img or (id .. '.png')
    if args["repository"] then
local img   = imgFile ~= '' and formatImage(imgFile, size, linkTgt) or ''
        additionalInfo = itemStack .. " " .. labelOutput .. " " .. itemContained .. " " .. itemSlot .. " " .. itemChem
if argsRaw.imageTooltip then
    else
img = frame:preprocess(
        additionalInfo = prefix .. " " .. labelOutput
string.format(
    end
'{{#invoke:Entity Lookup|createimagetooltip|Файл:%s.png|%s|Мета=%s,link=}}',
id, id, size
)
)
end


    local result = image .. " " .. additionalInfo
-- Репозиторий
local repoStr = ''
if isRepo then
local parts = {}
for _, slot in ipairs({ 'contained', 'slot', 'chem' }) do
table.insert(parts,
frame:preprocess(
string.format(
'{{#invoke:Prototypes/Хранилище/Предмет|main|framing|%s|%s}}',
slot, id
)
)
)
end
repoStr = table.concat(parts, ' ')
end


    -- Формирование вывода через LinkCard, если указан обёртка
-- Основная сборка
    if args["wrapper"] then
local parts = {}
        local pixel = string.match(size, "(%d+)")
if isVert then
        local wrapperInfo
table.insert(parts,
        if args["repository"] then
string.format(
            wrapperInfo = label .. itemStack .. " " .. itemContained .. " " .. itemSlot .. " " .. itemChem
"<span style='display:inline-flex;flex-direction:column;align-items:center;'>%s<b>%s</b></span>",
        else
img, labelOut
            wrapperInfo = label .. prefix
)
        end
)
        local imageParam = args["image"] or (id .. ".png")
else
        local linkParam = args["link"]
table.insert(parts,
        if not linkParam or linkParam == "" then
string.format(
            linkParam = itemName
"<span style='display:inline-block;'>%s%s</span>", img, repoStr
        end
)
)
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|name=%s|image=%s|pixel=%s|link=%s|SideStyle=1}}", wrapperInfo, imageParam, pixel, linkParam))
-- Обёртка LinkCard
    end
if isWrap then
local side  = isVert and '' or '|горизонт_стиль=1'
local card = string.format(
'{{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("<span>" .. result .. "</span>")
-- Финальный вывод
return frame:preprocess('<span>' .. table.concat(parts, ' ') .. '</span>')
end
end


return p
return p

Текущая версия от 17:06, 28 октября 2025

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

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

-- Форматирует файл изображения
local function formatImage(file, size, link)
	return string.format('[[Файл:%s|%s|link=%s]]', file, size, link)
end

-- Оборачивает текст в ссылку
local function wrapLink(text, target)
	return (text ~= '' and target and string.format('[[%s|%s]]', target, text) or text)
end

function p.main(frame)
	local argsRaw = getArgs(frame, { trim = false, removeBlanks = false })
	local args = {}
	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 isRepo   = argsRaw.repository ~= nil
	local isWrap   = argsRaw.wrapper	~= nil
	local isVert   = argsRaw.vertical   ~= nil

	-- Получение имени
	local nameStr = frame:preprocess(
		string.format('{{#invoke:Entity Lookup|getname|%s}}', id)
	)

	-- Лейбл и ссылка
	local labelRaw = argsRaw.label or argsRaw.l
	local label	= (labelRaw == nil and nameStr) or (labelRaw == '' and '' or labelRaw)
	local linkRaw  = argsRaw.link
	local linkTgt  = linkRaw == nil and '' or (linkRaw == '' and nameStr or linkRaw)
	local labelOut = (linkRaw ~= nil) and wrapLink(label, linkTgt) or label

	-- Изображение
	local imgFile = argsRaw.image or argsRaw.img or (id .. '.png')
	local img	  = imgFile ~= '' and formatImage(imgFile, size, linkTgt) or ''
	if argsRaw.imageTooltip then
		img = frame:preprocess(
			string.format(
				'{{#invoke:Entity Lookup|createimagetooltip|Файл:%s.png|%s|Мета=%s,link=}}',
				id, id, size
			)
		)
	end

	-- Репозиторий
	local repoStr = ''
	if isRepo then
		local parts = {}
		for _, slot in ipairs({ 'contained', 'slot', 'chem' }) do
			table.insert(parts,
				frame:preprocess(
					string.format(
						'{{#invoke:Prototypes/Хранилище/Предмет|main|framing|%s|%s}}',
						slot, id
					)
				)
			)
		end
		repoStr = table.concat(parts, ' ')
	end

	-- Основная сборка
	local parts = {}
	if isVert then
		table.insert(parts,
			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)

	-- Обёртка LinkCard
	if isWrap then
		local side  = isVert and '' or '|горизонт_стиль=1'
		local card = string.format(
			'{{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('<span>' .. table.concat(parts, ' ') .. '</span>')
end

return p