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

Материал из Space Station 14 Вики
м параметры LinkCard
Нет описания правки
Строка 1: Строка 1:
local p = {}
local p = {}
local getArgs = require('Module:Arguments').getArgs
local getArgs = require('Module:Arguments').getArgs
local JsonPaths = require('Module:JsonPaths')
local function trim(value)
return mw.text.trim(value or '')
end


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


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


Строка 16: Строка 26:
local args = {}
local args = {}
for k, v in pairs(argsRaw) do
for k, v in pairs(argsRaw) do
if v ~= '' then args[k] = v end
if v ~= '' then
args[k] = v
end
end
end


local id   = args[1] or ''
local id = args[1] or ''
local size = args.size or '32px'
local size = args.size or '32px'
local prefix   = args[2] or ''
local prefix = args[2] or ''
local isRepo   = argsRaw.repository ~= nil
local isRepo = argsRaw.repository ~= nil
local isWrap   = argsRaw.wrapper ~= nil
local isWrap = argsRaw.wrapper ~= nil
local isVert   = argsRaw.vertical   ~= nil
local isVert = argsRaw.vertical ~= nil
 
local project = JsonPaths.project(argsRaw.path or '')
local useProject = (project ~= '' and id ~= '' and JsonPaths.has(id, project) == true)
local projPrefix = useProject and projectPrefix(project) or ''


-- Получение имени
-- Получение имени
Строка 33: Строка 49:
-- Лейбл и ссылка
-- Лейбл и ссылка
local labelRaw = argsRaw.label or argsRaw.l
local labelRaw = argsRaw.label or argsRaw.l
local label = (labelRaw == nil and nameStr) or (labelRaw == '' and '' or labelRaw)
local label = (labelRaw == nil and nameStr) or (labelRaw == '' and '' or labelRaw)
local linkRaw = argsRaw.link
local linkRaw = argsRaw.link
local linkTgt = linkRaw == nil and '' or (linkRaw == '' and nameStr or linkRaw)
local linkTgt = linkRaw == nil and '' or (linkRaw == '' and nameStr or linkRaw)
 
if linkTgt ~= '' and projPrefix ~= '' then
linkTgt = projPrefix .. linkTgt
end
 
local labelOut = (linkRaw ~= nil) and wrapLink(label, linkTgt) or label
local labelOut = (linkRaw ~= nil) and wrapLink(label, linkTgt) or label


-- Изображение
-- Изображение
local imgFile = argsRaw.image or argsRaw.img or (id .. '.png')
local imgFile = argsRaw.image or argsRaw.img or (id .. '.png')
local img   = imgFile ~= '' and formatImage(imgFile, size, linkTgt) or ''
if imgFile ~= '' and projPrefix ~= '' then
imgFile = projPrefix .. imgFile
end
 
local img = imgFile ~= '' and formatImage(imgFile, size, linkTgt) or ''
if argsRaw.imageTooltip then
if argsRaw.imageTooltip then
img = frame:preprocess(
img = frame:preprocess(
string.format(
string.format(
'{{#invoke:Entity Lookup|createimagetooltip|Файл:%s.png|%s|Мета=%s,link=}}',
'{{#invoke:Entity Lookup|createimagetooltip|Файл:%s|%s|Мета=%s,link=}}',
id, id, size
imgFile, id, size
)
)
)
)
Строка 84: Строка 109:
table.insert(parts, labelOut)
table.insert(parts, labelOut)
end
end
-- Стек предметов
-- Стек предметов
table.insert(parts,
table.insert(parts,
Строка 96: Строка 122:
-- Обёртка LinkCard
-- Обёртка LinkCard
if isWrap then
if isWrap then
local side = isVert and '' or '|горизонт_стиль=1'
local side = isVert and '' or '|горизонт_стиль=1'
local card = string.format(
local card = string.format(
'{{LinkCard|название=%s %s %s|пин=%s|изображение=%s|ссылка=%s%s}}',
'{{LinkCard|название=%s %s %s|пин=%s|изображение=%s|ссылка=%s%s}}',
Строка 112: Строка 138:
end
end


-- Финальный вывод
return frame:preprocess('<span>' .. table.concat(parts, ' ') .. '</span>')
return frame:preprocess('<span>' .. table.concat(parts, ' ') .. '</span>')
end
end


return p
return p

Версия от 19:51, 21 марта 2026

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

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

local function trim(value)
	return mw.text.trim(value or '')
end

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

local function projectPrefix(project)
	if project ~= '' then
		return project .. ':'
	end
	return ''
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 project = JsonPaths.project(argsRaw.path or '')
	local useProject = (project ~= '' and id ~= '' and JsonPaths.has(id, project) == true)
	local projPrefix = useProject and projectPrefix(project) or ''

	-- Получение имени
	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)

	if linkTgt ~= '' and projPrefix ~= '' then
		linkTgt = projPrefix .. linkTgt
	end

	local labelOut = (linkRaw ~= nil) and wrapLink(label, linkTgt) or label

	-- Изображение
	local imgFile = argsRaw.image or argsRaw.img or (id .. '.png')
	if imgFile ~= '' and projPrefix ~= '' then
		imgFile = projPrefix .. imgFile
	end

	local img = imgFile ~= '' and formatImage(imgFile, size, linkTgt) or ''
	if argsRaw.imageTooltip then
		img = frame:preprocess(
			string.format(
				'{{#invoke:Entity Lookup|createimagetooltip|Файл:%s|%s|Мета=%s,link=}}',
				imgFile, 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