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

Материал из Space Stories Wiki
Нет описания правки
Метка: отменено
Нет описания правки
Метка: отменено
Строка 1: Строка 1:
local p = {}
local p = {}
local mw = mw
local data = mw.loadData("Module:ChemCard/data")
local html = mw.html
 
local function getArgs(frame)
    local args = {}
    if type(frame) == "table" and frame.args then
        args = frame.args
    else
        args = mw.getCurrentFrame():getParent().args or {}
    end
    return args
end
 
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
    name = mw.text.trim(name)
    return {name = name, qty = qty or "1"}
end
 
local function parseRecipeString(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.lower(line):find("смеш") 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(rec)
    local container = html.create("div"):addClass("chem-recipe-block")
    container:tag("span"):addClass("chem-block-heading chem-heading"):attr("data-kind","recipe"):wikitext("Рецепт")
    local collapsible = html.create("div"):addClass("collapsible collapsed"):attr("data-kind","recipe")
    local inner = html.create("div"):addClass("chem-recipe"):css("display","flex"):css("justify-content","space-between")
 
    local inputsDiv = html.create("div"):addClass("recipe-inputs")
    for _,it in ipairs(rec.inputs) do
        inputsDiv:tag("div"):addClass("recipe-item"):wikitext(it.name.." ["..it.qty.."]")
    end
    inner:node(inputsDiv)
 
    local middleDiv = html.create("div"):addClass("recipe-action"):css("margin","0 0.5em"):css("text-align","center")
    if rec.action and rec.action~="" then
        middleDiv:wikitext(rec.action)
    end
    inner:node(middleDiv)
 
    local outputsDiv = html.create("div"):addClass("recipe-outputs")
    for _,it in ipairs(rec.outputs) do
        outputsDiv:tag("div"):addClass("recipe-item"):wikitext(it.name.." ["..it.qty.."]")
    end
    inner:node(outputsDiv)
 
    collapsible:node(inner)
    container:node(collapsible)
    return container
end
 
local function buildEffectsHtml(effects)
    if not effects or effects=="" or effects=="Нет" then return nil end
    local container = html.create("div"):addClass("chem-effects-block")
    container:tag("span"):addClass("chem-block-heading chem-heading"):attr("data-kind","effects"):wikitext("Эффекты")
    local collapsible = html.create("div"):addClass("collapsible collapsed"):attr("data-kind","effects")
    local inner = html.create("div"):addClass("chem-effects")
    local ul = html.create("ul")
    for effect in mw.text.gsplit(effects, "[;\n]") do
        effect = mw.text.trim(effect)
        if effect~="" then ul:tag("li"):wikitext(effect) end
    end
    inner:node(ul)
    collapsible:node(inner)
    container:node(collapsible)
    return container
end


function p.card(frame)
function p.card(frame)
    local args = getArgs(frame)
local proto = frame.args.prototype
    local name = args.name or args.title or "—"
local chem = data[proto]
    local color = args.color or args.colour or ""
if not chem then return "Нет данных для " .. proto end
    local border = args.border or args.bordercolor or ""
    local bg = args.bg or args.bgcolor or ""
    local recipeStr = args.recipe or args.rezept or ""
    local effects = args.effects or args.effect or ""
    local desc = args.description or args.desc or ""
 
    local parsed = parseRecipeString(recipeStr)


    local container = html.create("div"):addClass("chem-card")
local out = mw.html.create("div")
    local styleParts = {}
out:addClass("chem-card")
    if color~="" then table.insert(styleParts,"--card-accent:"..color) end
out:cssText(
    if border~="" then table.insert(styleParts,"--card-border:"..border) end
"background-color:"..chem.bg..";border:2px solid "..chem.border..";color:"..chem.color..
    if bg~="" then table.insert(styleParts,"--card-bg:"..bg) end
";padding:8px;margin:4px;border-radius:8px;flex:0 1 calc(25% - 12px);max-width:320px;min-width:200px;"
    if #styleParts>0 then container:attr("style", table.concat(styleParts,";")) end
)


    container:tag("h3"):wikitext(name)
out:tag("div"):cssText("font-weight:700;margin-bottom:4px;"):wikitext(chem.name)


    if #parsed.inputs>0 or #parsed.outputs>0 or (parsed.action and parsed.action~="") then
if chem.recipes and #chem.recipes > 0 then
        container:node(buildRecipeHtml(parsed))
local recipeBlock = mw.html.create("div")
    end
recipeBlock:wikitext("Рецепт:\n")
for _, r in ipairs(chem.recipes) do
recipeBlock:tag("div"):wikitext(r)
end
out:node(recipeBlock)
end


    container:node(buildEffectsHtml(effects))
if chem.effects and #chem.effects > 0 then
local effectBlock = mw.html.create("div")
effectBlock:wikitext("Эффекты:\n")
for _, e in ipairs(chem.effects) do
effectBlock:tag("div"):wikitext(e)
end
out:node(effectBlock)
end


    if desc and desc~="" then
if chem.desc then
        container:tag("div"):addClass("chem-desc"):wikitext(desc)
out:tag("div"):wikitext(chem.desc)
    end
end


    return tostring(container)
return tostring(out)
end
end


return p
return p

Версия от 07:57, 11 ноября 2025

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

local p = {}
local data = mw.loadData("Module:ChemCard/data")

function p.card(frame)
	local proto = frame.args.prototype
	local chem = data[proto]
	if not chem then return "Нет данных для " .. proto end

	local out = mw.html.create("div")
	out:addClass("chem-card")
	out:cssText(
		"background-color:"..chem.bg..";border:2px solid "..chem.border..";color:"..chem.color..
		";padding:8px;margin:4px;border-radius:8px;flex:0 1 calc(25% - 12px);max-width:320px;min-width:200px;"
	)

	out:tag("div"):cssText("font-weight:700;margin-bottom:4px;"):wikitext(chem.name)

	if chem.recipes and #chem.recipes > 0 then
		local recipeBlock = mw.html.create("div")
		recipeBlock:wikitext("Рецепт:\n")
		for _, r in ipairs(chem.recipes) do
			recipeBlock:tag("div"):wikitext(r)
		end
		out:node(recipeBlock)
	end

	if chem.effects and #chem.effects > 0 then
		local effectBlock = mw.html.create("div")
		effectBlock:wikitext("Эффекты:\n")
		for _, e in ipairs(chem.effects) do
			effectBlock:tag("div"):wikitext(e)
		end
		out:node(effectBlock)
	end

	if chem.desc then
		out:tag("div"):wikitext(chem.desc)
	end

	return tostring(out)
end

return p