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

Материал из Space Stories Wiki
Нет описания правки
Нет описания правки
 
(не показано 11 промежуточных версий этого же участника)
Строка 1: Строка 1:
local p = {}
local p = {}


-- функция для разбивки ингредиентов/результатов в блок div вертикально
function p.recipe(frame)
local function listBlock(text)
local args = frame:getParent().args
local items = {}
local name = args.name or 'Неизвестно'
for item in mw.text.gsplit(text, ";") do
local color = args.color or '#cccccc'
table.insert(items, '<div class="alchemy-item">' .. mw.text.trim(item) .. '</div>')
local product = args.product or ''
end
local description = args.description or ''
return table.concat(items, "\n")
local effect = args.effect or ''
end
local recipe = args.recipe or ''
 
-- функция для разбивки эффектов в ul
local function listUL(text)
local items = {}
for item in mw.text.gsplit(text, ";") do
table.insert(items, '<li>' .. mw.text.trim(item) .. '</li>')
end
return table.concat(items, "\n")
end
 
local function makeCard(recipe)
local title = recipe["Название"] or "Без названия"
local ingredients = recipe["Ингредиенты"] or ""
local result = recipe["Результат"] or ""
local effects = recipe["Эффекты"] or ""
local effectsDesc = recipe["ОписаниеЭффектов"] or ""
local borderColor = recipe["ЦветРамки"] or "#338833" -- зелёный тёмный
local backgroundColor = recipe["ЦветФона"] or "#1a2a1a" -- фон тёмно-зелёный
 
local html = {}
table.insert(html, string.format(
'<div class="alchemy-card" style="border-color:%s;background-color:%s;">',
borderColor, backgroundColor
))
table.insert(html, '<h3 class="alchemy-title">' .. mw.text.encode(title) .. '</h3>')
 
table.insert(html, '<div class="alchemy-main-row">')
 
table.insert(html, '<div class="alchemy-ingredients">')
table.insert(html, listBlock(ingredients))
table.insert(html, '</div>')
 
table.insert(html, '<div class="alchemy-plus">СМЕШАТЬ</div>')
 
table.insert(html, '<div class="alchemy-result">')
table.insert(html, listBlock(result))
table.insert(html, '</div>')
 
table.insert(html, '</div>')
 
if effects ~= "" then
table.insert(html, '<b>Эффекты:</b><ul class="alchemy-effects">')
table.insert(html, listUL(effects))
table.insert(html, '</ul>')
if effectsDesc ~= "" then
table.insert(html, '<div class="alchemy-effects-desc">' .. mw.text.encode(effectsDesc) .. '</div>')
end
end
 
table.insert(html, '</div>')
return table.concat(html, "\n")
end
 
function p.grid(frame)
local count = tonumber(frame.args.count) or 0
if count == 0 then return "Ошибка: параметр count обязателен и должен быть числом." end
 
local allRecipes = {}
for i = 1, count do
local r = {}
for _, key in ipairs({"Название", "Ингредиенты", "Результат", "Эффекты", "ОписаниеЭффектов", "ЦветРамки", "ЦветФона"}) do
local val = frame.args[key .. i]
if val then r[key] = val end
end
table.insert(allRecipes, r)
end
 
local html = {}
html[#html + 1] = '<div class="alchemy-grid">'


for _, recipe in ipairs(allRecipes) do
local function collapse(title, content)
html[#html + 1] = makeCard(recipe)
return string.format(
'<div class="alchemy-collapse">' ..
'<div class="alchemy-toggle" onclick="this.classList.toggle(\'open\')">%s</div>' ..
'<div class="alchemy-content">%s</div>' ..
'</div>',
title, content
)
end
end


html[#html + 1] = '</div>'
local recipeBlock = collapse('Рецепт', recipe)
local effectBlock = collapse('Эффекты', effect)


return table.concat(html, "\n")
return string.format([[
<div class="alchemy-block" style="border-color:%s">
<div class="alchemy-header" style="background:%s">%s</div>
<div class="alchemy-body">
<div class="alchemy-product">%s</div>
%s
%s
<div class="alchemy-description">%s</div>
</div>
</div>
]], color, color, name, product, recipeBlock, effectBlock, description)
end
end


return p
return p

Текущая версия от 02:45, 17 июня 2025

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

local p = {}

function p.recipe(frame)
	local args = frame:getParent().args
	local name = args.name or 'Неизвестно'
	local color = args.color or '#cccccc'
	local product = args.product or ''
	local description = args.description or ''
	local effect = args.effect or ''
	local recipe = args.recipe or ''

	local function collapse(title, content)
		return string.format(
			'<div class="alchemy-collapse">' ..
				'<div class="alchemy-toggle" onclick="this.classList.toggle(\'open\')">%s</div>' ..
				'<div class="alchemy-content">%s</div>' ..
			'</div>',
			title, content
		)
	end

	local recipeBlock = collapse('Рецепт', recipe)
	local effectBlock = collapse('Эффекты', effect)

	return string.format([[
<div class="alchemy-block" style="border-color:%s">
	<div class="alchemy-header" style="background:%s">%s</div>
	<div class="alchemy-body">
		<div class="alchemy-product">%s</div>
		%s
		%s
		<div class="alchemy-description">%s</div>
	</div>
</div>
]], color, color, name, product, recipeBlock, effectBlock, description)
end

return p