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

Материал из Space Stories Wiki
Нет описания правки
Нет описания правки
Строка 4: Строка 4:
local args = frame:getParent().args
local args = frame:getParent().args
local name = args.name or "Без названия"
local name = args.name or "Без названия"
local recipe = args.recipe or "Неизвестно"
local recipe = args.recipe or ""
local effects = args.effects or "Нет описания."
local effects = args.effects or "Нет описания."
local method = recipe ~= "" and "Смешайте" or "Неизвестно"


local function formatReagents(str)
local function formatReagents(str)
Строка 13: Строка 14:
local name, qty = trimmed:match("^(.-)%s*%[(%d+)%]$")
local name, qty = trimmed:match("^(.-)%s*%[(%d+)%]$")
if name and qty then
if name and qty then
table.insert(out, string.format('<div class="reagent-item">🧪 <span class="reagent">%s</span> <span class="qty">[%s]</span></div>', name, qty))
table.insert(out, string.format(
'<div class="reagent-item">🧪 <span class="reagent">%s</span> <span class="qty">[%s]</span></div>',
mw.text.nowiki(name),
mw.text.nowiki(qty)
))
else
else
table.insert(out, '<div class="reagent-item">' .. mw.text.nowiki(trimmed) .. '</div>')
table.insert(out, '<div class="reagent-item">' .. mw.text.nowiki(trimmed) .. '</div>')
Строка 38: Строка 43:
html:tag("div")
html:tag("div")
:addClass("alchemy-section")
:addClass("alchemy-section")
:wikitext(collapsible("Рецепт", formatReagents(recipe) .. '<div class="method">🔄 ' .. recipe and "Смешайте" or "Неизвестно" .. '</div>'))
:wikitext(collapsible("Рецепт", formatReagents(recipe) .. '<div class="method">🔄 ' .. method .. '</div>'))


html:tag("div")
html:tag("div")

Версия от 10:32, 22 июня 2025

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

local p = {}

function p.main(frame)
	local args = frame:getParent().args
	local name = args.name or "Без названия"
	local recipe = args.recipe or ""
	local effects = args.effects or "Нет описания."
	local method = recipe ~= "" and "Смешайте" or "Неизвестно"

	local function formatReagents(str)
		local out = {}
		for reagent in mw.text.gsplit(str, ",") do
			local trimmed = mw.text.trim(reagent)
			local name, qty = trimmed:match("^(.-)%s*%[(%d+)%]$")
			if name and qty then
				table.insert(out, string.format(
					'<div class="reagent-item">🧪 <span class="reagent">%s</span> <span class="qty">[%s]</span></div>',
					mw.text.nowiki(name),
					mw.text.nowiki(qty)
				))
			else
				table.insert(out, '<div class="reagent-item">' .. mw.text.nowiki(trimmed) .. '</div>')
			end
		end
		return table.concat(out)
	end

	local function collapsible(title, content)
		return string.format([[
<div class="collapsible-container">
	<div class="collapsible-header" onclick="this.nextElementSibling.classList.toggle('collapsed')">%s</div>
	<div class="collapsible-body collapsed">%s</div>
</div>]], title, content)
	end

	local html = mw.html.create("div")
	html:addClass("alchemy-card")

	html:tag("div")
		:addClass("alchemy-title")
		:wikitext(name)

	html:tag("div")
		:addClass("alchemy-section")
		:wikitext(collapsible("Рецепт", formatReagents(recipe) .. '<div class="method">🔄 ' .. method .. '</div>'))

	html:tag("div")
		:addClass("alchemy-section")
		:wikitext(collapsible("Эффекты", effects))

	return tostring(html)
end

return p