Модуль:ChemCard

Материал из Space Stories Wiki

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

local p = {}
local html = mw.html

local function splitLines(s)
	local t = {}
	if not s then return t end
	for line in mw.text.gsplit(s, "\n") do
		line = mw.text.trim(line)
		if line ~= "" then table.insert(t, line) end
	end
	return t
end

local function parseLineToItem(l)
	local name, qty = mw.ustring.match(l, "^(.*)%s*%[%s*([%d,%.]+)%s*%]%s*$")
	if not name then
		name = l
		qty = "1"
	end
	return {name = mw.text.trim(name), qty = qty or "1"}
end

local function parseRecipe(recipeStr)
	if not recipeStr or recipeStr == "" then return {inputs={}, outputs={}, action=""} end
	local lines = splitLines(recipeStr)
	local splitIdx, action
	for i, line in ipairs(lines) do
		if mw.ustring.find(mw.ustring.lower(line), "смеш") then
			splitIdx = i
			action = line
			break
		end
	end
	local inputs, outputs = {}, {}
	if splitIdx then
		for i = 1, splitIdx - 1 do table.insert(inputs, parseLineToItem(lines[i])) end
		for i = splitIdx + 1, #lines do table.insert(outputs, parseLineToItem(lines[i])) end
	else
		for i = 1, #lines do table.insert(inputs, parseLineToItem(lines[i])) end
	end
	return {inputs = inputs, outputs = outputs, action = action}
end

local function buildRecipeHtml(parsed)
	if #parsed.inputs == 0 and #parsed.outputs == 0 and (not parsed.action or parsed.action=="") then return nil end
	local container = html.create("div"):addClass("mw-collapsible mw-collapsed"):attr("style","width:100%; overflow:auto; border-bottom: 1px solid #303038;")
	container:tag("div"):attr("style","font-weight:bold;line-height:1.6; padding:1px"):wikitext("Рецепты")
	local content = html.create("div"):addClass("mw-collapsible-content")
	for _, it in ipairs(parsed.inputs) do
		content:tag("div"):wikitext(it.name.." ["..it.qty.."]")
	end
	if parsed.action and parsed.action~="" then
		content:tag("div"):wikitext(parsed.action)
	end
	for _, it in ipairs(parsed.outputs) do
		content:tag("div"):wikitext(it.name.." ["..it.qty.."]")
	end
	container:node(content)
	return container
end

local function buildEffectsHtml(effects)
	if not effects or effects=="" or effects=="Нет" then return nil end
	local container = html.create("div"):addClass("mw-collapsible mw-collapsed"):attr("style","width:100%; overflow:auto; border-bottom: 1px solid #303038;")
	container:tag("div"):attr("style","font-weight:bold;line-height:1.6; padding:1px"):wikitext("Эффекты")
	local content = html.create("div"):addClass("mw-collapsible-content"):attr("style","padding:0 0.5em")
	for effect in mw.text.gsplit(effects, "[;\n]") do
		effect = mw.text.trim(effect)
		if effect ~= "" then content:tag("div"):wikitext(effect) end
	end
	container:node(content)
	return container
end

function p.card(frame)
	local args = frame.args or {}
	local name = args.name or "—"
	local color = args.color or "#ffffff"
	local border = args.border or "#000000"
	local bg = args.bg or "#101010"
	local recipeStr = args.recipe or ""
	local effects = args.effects or ""
	local desc = args.desc or ""

	local parsed = parseRecipe(recipeStr)

	local card = html.create("div"):addClass("quickbox"):attr("style","width:25em;")
	local outer = html.create("div"):attr("style","border:2px solid black; border-radius:0 0 21px 21px")
	local middle = html.create("div"):attr("style","border:3px solid "..border.."; border-radius:0 0 20px 20px")
	local inner = html.create("div"):attr("style","border:2px solid black; border-radius:0 0 18px 18px")

	inner:tag("div"):addClass("quickboxhead"):attr("style","font-weight:500; background-color:"..color.."; color:#000000"):wikitext("''' "..name.."'''")

	if parsed then
		local rhtml = buildRecipeHtml(parsed)
		if rhtml then inner:node(rhtml) end
	end

	local ehtml = buildEffectsHtml(effects)
	if ehtml then inner:node(ehtml) end

	if desc and desc~="" then
		inner:tag("div"):attr("style","padding:0 0.5em"):wikitext(desc)
	end

	middle:node(inner)
	outer:node(middle)
	card:node(outer)

	return tostring(card)
end

return p