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

Материал из Space Stories Wiki
(Новая страница: «local json = require('Module:JSON') local p = {} -- Функция для генерации отдельной карточки (как раньше) local function makeCard(recipe) local title = recipe["Название"] or "Без названия" local ingredients = recipe["Ингредиенты"] or "" local result = recipe["Результат"] or "" local effects = recipe["Эффекты"] or "" local effectsDesc = recipe["ОписаниеЭ...»)
 
Нет описания правки
Строка 1: Строка 1:
local json = require('Module:JSON')
local p = {}


local p = {}
-- Разбивает таблицу tbl на подтаблицы по n элементов
local function chunk(tbl, n)
local chunks = {}
for i = 1, #tbl, n do
local chunk = {}
for j = i, math.min(i + n - 1, #tbl) do
table.insert(chunk, tbl[j])
end
table.insert(chunks, chunk)
end
return chunks
end


-- Функция для генерации отдельной карточки (как раньше)
-- Создаёт HTML карточку из данных рецепта (таблица с ключами-строками)
local function makeCard(recipe)
local function makeCard(recipe)
local title = recipe["Название"] or "Без названия"
local title = recipe["Название"] or "Без названия"
Строка 30: Строка 41:


local html = {}
local html = {}
table.insert(html, '<div class="alchemy-card" style="border: 2px solid ' .. borderColor .. '; padding: 0.5em 1em; border-radius: 8px; background: ' .. backgroundColor .. '; box-sizing: border-box; width: 23%; margin: 0.5%;">')


table.insert(html, ('<div class="alchemy-recipe-card" style="box-sizing: border-box; flex: 0 0 23%%; margin: 1%%; border: 2px solid %s; padding: 1em; border-radius: 8px; background: %s;">'):format(borderColor, backgroundColor))
table.insert(html, '<h3 style="margin-top:0; text-align:center; font-size:1.1em;">' .. mw.text.encode(title) .. '</h3>')
table.insert(html, '<h3 style="margin-top:0; text-align:center;">' .. title .. '</h3>')


-- Flex с ингредиентами, смешать, результат
table.insert(html, '<div style="display: flex; text-align: center; align-items: stretch; font-size:0.9em;">')
table.insert(html, '<div style="display: flex; text-align: center; align-items: stretch;">')


table.insert(html, '<div style="flex: 1; display: flex; flex-direction: column; justify-content: center; border-right: 1px solid #aaa; padding-right: 0.5em;">')
table.insert(html, '<div style="flex: 1; display: flex; flex-direction: column; justify-content: center; border-right: 1px solid #aaa; padding-right: 0.5em;">')
Строка 41: Строка 51:
table.insert(html, '</div>')
table.insert(html, '</div>')


table.insert(html, '<div style="flex: 0 0 80px; display: flex; align-items: center; justify-content: center; font-weight:bold;">СМЕШАТЬ</div>')
table.insert(html, '<div style="flex: 0 0 80px; display: flex; align-items: center; justify-content: center; font-weight:bold; font-size: 0.95em;">СМЕШАТЬ</div>')


table.insert(html, '<div style="flex: 1; display: flex; flex-direction: column; justify-content: center; border-left: 1px solid #aaa; padding-left: 0.5em;">')
table.insert(html, '<div style="flex: 1; display: flex; flex-direction: column; justify-content: center; border-left: 1px solid #aaa; padding-left: 0.5em;">')
Строка 50: Строка 60:


if effects ~= "" then
if effects ~= "" then
table.insert(html, '<hr><b>Эффекты:</b><ul>')
table.insert(html, '<hr style="margin: 0.4em 0;"><b>Эффекты:</b><ul style="margin: 0.3em 0 0.5em 1.2em; padding: 0;">')
table.insert(html, listUL(effects))
table.insert(html, listUL(effects))
table.insert(html, '</ul>')
table.insert(html, '</ul>')
Строка 56: Строка 66:


if effectsDesc ~= "" then
if effectsDesc ~= "" then
table.insert(html, '<div style="margin-top: 0.5em; font-style: italic; color: #555;">' .. mw.text.trim(effectsDesc) .. '</div>')
table.insert(html, '<div style="margin-top: 0.3em; font-style: italic; color: #555; font-size: 0.85em;">' .. mw.text.encode(effectsDesc) .. '</div>')
end
end


table.insert(html, '</div>')
table.insert(html, '</div>')
return table.concat(html, "\n")
return table.concat(html, "\n")
end
end


-- Главная функция — принимает JSON-строку с массивом рецептов
-- Главная функция вывода сетки
function p.grid(frame)
function p.grid(frame)
local args = frame:getParent().args
local count = tonumber(frame.args.count) or 0
local data = args["data"] or ""
if count == 0 then return "Ошибка: параметр count обязателен и должен быть числом." end


if data == "" then
local allRecipes = {}
return "<div style='color:red;'>Ошибка: параметр data пуст</div>"
for i = 1, count do
end
local r = {}
 
for _, key in ipairs({"Название", "Ингредиенты", "Результат", "Эффекты", "ОписаниеЭффектов", "ЦветРамки", "ЦветФона"}) do
local ok, recipes = pcall(json.decode, data)
local val = frame.args[key .. i]
if not ok then
if val then r[key] = val end
return "<div style='color:red;'>Ошибка: неверный формат JSON</div>"
end
table.insert(allRecipes, r)
end
end


local html = {}
local html = {}
html[#html + 1] = '<div class="alchemy-grid" style="display: flex; flex-wrap: wrap; justify-content: space-between;">'


table.insert(html, '<div class="alchemy-recipes-grid" style="display: flex; flex-wrap: wrap; justify-content: space-between;">')
for _, recipe in ipairs(allRecipes) do
 
html[#html + 1] = makeCard(recipe)
for _, recipe in ipairs(recipes) do
table.insert(html, makeCard(recipe))
end
end


table.insert(html, '</div>')
html[#html + 1] = '</div>'


return table.concat(html, "\n")
return table.concat(html, "\n")

Версия от 11:08, 16 мая 2025

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

local p = {}

-- Разбивает таблицу tbl на подтаблицы по n элементов
local function chunk(tbl, n)
	local chunks = {}
	for i = 1, #tbl, n do
		local chunk = {}
		for j = i, math.min(i + n - 1, #tbl) do
			table.insert(chunk, tbl[j])
		end
		table.insert(chunks, chunk)
	end
	return chunks
end

-- Создаёт HTML карточку из данных рецепта (таблица с ключами-строками)
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 "#ccc"
	local backgroundColor = recipe["ЦветФона"] or "#f9f9f9"

	local function listBlock(text)
		local items = {}
		for item in mw.text.gsplit(text, ";") do
			table.insert(items, "<div>" .. mw.text.trim(item) .. "</div>")
		end
		return table.concat(items, "\n")
	end

	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 html = {}
	table.insert(html, '<div class="alchemy-card" style="border: 2px solid ' .. borderColor .. '; padding: 0.5em 1em; border-radius: 8px; background: ' .. backgroundColor .. '; box-sizing: border-box; width: 23%; margin: 0.5%;">')

	table.insert(html, '<h3 style="margin-top:0; text-align:center; font-size:1.1em;">' .. mw.text.encode(title) .. '</h3>')

	table.insert(html, '<div style="display: flex; text-align: center; align-items: stretch; font-size:0.9em;">')

	table.insert(html, '<div style="flex: 1; display: flex; flex-direction: column; justify-content: center; border-right: 1px solid #aaa; padding-right: 0.5em;">')
	table.insert(html, listBlock(ingredients))
	table.insert(html, '</div>')

	table.insert(html, '<div style="flex: 0 0 80px; display: flex; align-items: center; justify-content: center; font-weight:bold; font-size: 0.95em;">СМЕШАТЬ</div>')

	table.insert(html, '<div style="flex: 1; display: flex; flex-direction: column; justify-content: center; border-left: 1px solid #aaa; padding-left: 0.5em;">')
	table.insert(html, listBlock(result))
	table.insert(html, '</div>')

	table.insert(html, '</div>')

	if effects ~= "" then
		table.insert(html, '<hr style="margin: 0.4em 0;"><b>Эффекты:</b><ul style="margin: 0.3em 0 0.5em 1.2em; padding: 0;">')
		table.insert(html, listUL(effects))
		table.insert(html, '</ul>')
	end

	if effectsDesc ~= "" then
		table.insert(html, '<div style="margin-top: 0.3em; font-style: italic; color: #555; font-size: 0.85em;">' .. mw.text.encode(effectsDesc) .. '</div>')
	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" style="display: flex; flex-wrap: wrap; justify-content: space-between;">'

	for _, recipe in ipairs(allRecipes) do
		html[#html + 1] = makeCard(recipe)
	end

	html[#html + 1] = '</div>'

	return table.concat(html, "\n")
end

return p