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

Материал из Space Stories Wiki
Нет описания правки
Метка: отменено
Нет описания правки
Метка: ручная отмена
 
(не показано 35 промежуточных версий этого же участника)
Строка 1: Строка 1:
local p = {}
local p = {}
local mw = mw
local html = mw.html
local html = mw.html
local function getArgs(frame)
    if type(frame) == "table" and frame.args then return frame.args end
    return mw.getCurrentFrame():getParent().args or {}
end


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


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


local function parseRecipe(recipeStr)
local function parseRecipeString(recipeStr)
if not recipeStr or recipeStr == "" then return {inputs={}, outputs={}, action=""} end
    if not recipeStr or recipeStr == "" then return {} end
local lines = splitLines(recipeStr)
    local lines = splitLines(recipeStr)
local splitIdx, action
    local recipes = {}
for i, line in ipairs(lines) do
    local currentInputs = {}
if mw.ustring.find(mw.ustring.lower(line), "смеш") then
    local i = 1
splitIdx = i
    while i <= #lines do
action = line
        local line = lines[i]
break
        local lower = mw.ustring.lower(line)
end
        if lower:find("смеш") then
end
            if #currentInputs > 0 then
local inputs, outputs = {}, {}
                local outputs = {}
if splitIdx then
                i = i + 1
for i = 1, splitIdx - 1 do table.insert(inputs, parseLineToItem(lines[i])) end
                while i <= #lines do
for i = splitIdx + 1, #lines do table.insert(outputs, parseLineToItem(lines[i])) end
                    local nextLine = lines[i]
else
                    local nextLower = mw.ustring.lower(nextLine)
for i = 1, #lines do table.insert(inputs, parseLineToItem(lines[i])) end
                    if nextLower:find("смеш") then break end
end
                    table.insert(outputs, parseLineToItem(nextLine))
return {inputs = inputs, outputs = outputs, action = action}
                    i = i + 1
                end
                table.insert(recipes, {
                    inputs = currentInputs,
                    outputs = outputs,
                    action = "Смешайте"
                })
                currentInputs = {}
            else
                i = i + 1
            end
        else
            table.insert(currentInputs, parseLineToItem(line))
            i = i + 1
        end
    end
    if #currentInputs > 0 then
        table.insert(recipes, { inputs = currentInputs, outputs = {}, action = "" })
    end
    return recipes
end
end


local function buildRecipeHtml(parsed)
local function buildRecipeHtml(rec, index)
if #parsed.inputs == 0 and #parsed.outputs == 0 and (not parsed.action or parsed.action=="") then return nil end
    if not rec or (#rec.inputs == 0 and #rec.outputs == 0) 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;")
    local container = html.create("div"):addClass("chem-recipe-block")
container:tag("div"):attr("style","font-weight:bold;line-height:1.6; padding:1px"):wikitext("Рецепты")
 
local content = html.create("div"):addClass("mw-collapsible-content")
    if index == 1 then
for _, it in ipairs(parsed.inputs) do
        local heading = html.create("div"):addClass("chem-heading"):attr("data-kind", "recipe")
content:tag("div"):wikitext(it.name.." ["..it.qty.."]")
        local hcontent = heading:tag("div"):addClass("chem-heading-content")
end
        hcontent:tag("span"):addClass("heading-text"):wikitext("Рецепты")
if parsed.action and parsed.action~="" then
        hcontent:tag("span"):addClass("collapse-btn"):wikitext("развернуть")
content:tag("div"):wikitext(parsed.action)
        container:node(heading)
end
    end
for _, it in ipairs(parsed.outputs) do
 
content:tag("div"):wikitext(it.name.." ["..it.qty.."]")
    local collapsible = html.create("div"):addClass("collapsible collapsed"):attr("data-kind", "recipe")
end
 
container:node(content)
    local inner = html.create("div"):addClass("chem-recipe")
return container
 
    -- INPUTS (левая колонка)
    local inputsDiv = html.create("div"):addClass("recipe-inputs")
    for _, it in ipairs(rec.inputs) do
        local item = inputsDiv:tag("div"):addClass("recipe-item")
        -- Используем MediaWiki-синтаксис файла — надёжно рендерится
        item:wikitext('[[File:Beaker.png|36px|class=chem-beaker]] ' .. it.name .. ' [' .. it.qty .. ']')
    end
    inner:node(inputsDiv)
 
    -- Действие
    if rec.action ~= "" then
        local actionDiv = inner:tag("div"):addClass("recipe-action")
        actionDiv:wikitext(rec.action)
    end
 
    -- OUTPUTS (правая колонка)
    local outputsDiv = html.create("div"):addClass("recipe-outputs")
    for _, it in ipairs(rec.outputs) do
        local item = outputsDiv:tag("div"):addClass("recipe-item")
        item:wikitext('[[File:Beaker.png|36px|class=chem-beaker]] ' .. it.name .. ' [' .. it.qty .. ']')
    end
    inner:node(outputsDiv)
 
    collapsible:node(inner)
    container:node(collapsible)
    return container
end
end


local function buildEffectsHtml(effects)
local function buildEffectsHtml(effects)
if not effects or effects=="" or effects=="Нет" then return nil end
    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;")
    local container = html.create("div"):addClass("chem-effects-block")
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")
    local heading = html.create("div"):addClass("chem-heading"):attr("data-kind", "effects")
for effect in mw.text.gsplit(effects, "[;\n]") do
    local hcontent = heading:tag("div"):addClass("chem-heading-content")
effect = mw.text.trim(effect)
    hcontent:tag("span"):addClass("heading-text"):wikitext("Эффекты")
if effect ~= "" then content:tag("div"):wikitext(effect) end
    hcontent:tag("span"):addClass("collapse-btn"):wikitext("развернуть")
end
    container:node(heading)
container:node(content)
 
return container
    local collapsible = html.create("div"):addClass("collapsible collapsed"):attr("data-kind", "effects")
    local inner = html.create("div"):addClass("chem-effects")
 
    for effect in mw.text.gsplit(effects, "[;\n]") do
        effect = mw.text.trim(effect)
        if effect ~= "" then
            inner:tag("div"):addClass("chem-effect-line"):wikitext(effect)
        end
    end
    collapsible:node(inner)
    container:node(collapsible)
    return container
end
end


function p.card(frame)
function p.card(frame)
local args = frame.args or {}
    local args = getArgs(frame)
local name = args.name or "—"
    local name = args.name or "—"
local color = args.color or "#ffffff"
    local color = args.color or "#8cf"
local border = args.border or "#000000"
    local border = args.border or "#444"
local bg = args.bg or "#101010"
    local desc = args.desc or ""
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 recipes = {}
local outer = html.create("div"):attr("style","border:2px solid black; border-radius:0 0 21px 21px")
    local i = 1
local middle = html.create("div"):attr("style","border:3px solid "..border.."; border-radius:0 0 20px 20px")
    while true do
local inner = html.create("div"):attr("style","border:2px solid black; border-radius:0 0 18px 18px")
        local key = i == 1 and "recipe" or ("recipe" .. i)
        local recipeStr = args[key]
        if not recipeStr then break end
        local parsedList = parseRecipeString(recipeStr)
        for _, rec in ipairs(parsedList) do
            table.insert(recipes, rec)
        end
        i = i + 1
    end


inner:tag("div"):addClass("quickboxhead"):attr("style","font-weight:500; background-color:"..color.."; color:#000000"):wikitext("'''&nbsp;"..name.."'''")
    local container = html.create("div"):addClass("chem-card")
    container:attr("style", "border-color:" .. border .. "; --card-accent:" .. color)


if parsed then
    container:tag("div"):addClass("chem-name-header"):wikitext(name)
local rhtml = buildRecipeHtml(parsed)
if rhtml then inner:node(rhtml) end
end


local ehtml = buildEffectsHtml(effects)
    if #recipes > 0 then
if ehtml then inner:node(ehtml) end
        for idx, rec in ipairs(recipes) do
            local block = buildRecipeHtml(rec, idx)
            if block then container:node(block) end
        end
    end


if desc and desc~="" then
    local effectsBlock = buildEffectsHtml(args.effects)
inner:tag("div"):attr("style","padding:0 0.5em"):wikitext(desc)
    if effectsBlock then container:node(effectsBlock) end
end


middle:node(inner)
    if desc ~= "" then
outer:node(middle)
        container:tag("div"):addClass("chem-desc"):wikitext(desc)
card:node(outer)
    end


return tostring(card)
    return tostring(container)
end
end


return p
return p

Текущая версия от 13:44, 2 апреля 2026

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

local p = {}
local mw = mw
local html = mw.html

local function getArgs(frame)
    if type(frame) == "table" and frame.args then return frame.args end
    return mw.getCurrentFrame():getParent().args or {}
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
    return { name = mw.text.trim(name), qty = qty or "1" }
end

local function parseRecipeString(recipeStr)
    if not recipeStr or recipeStr == "" then return {} end
    local lines = splitLines(recipeStr)
    local recipes = {}
    local currentInputs = {}
    local i = 1
    while i <= #lines do
        local line = lines[i]
        local lower = mw.ustring.lower(line)
        if lower:find("смеш") then
            if #currentInputs > 0 then
                local outputs = {}
                i = i + 1
                while i <= #lines do
                    local nextLine = lines[i]
                    local nextLower = mw.ustring.lower(nextLine)
                    if nextLower:find("смеш") then break end
                    table.insert(outputs, parseLineToItem(nextLine))
                    i = i + 1
                end
                table.insert(recipes, {
                    inputs = currentInputs,
                    outputs = outputs,
                    action = "Смешайте"
                })
                currentInputs = {}
            else
                i = i + 1
            end
        else
            table.insert(currentInputs, parseLineToItem(line))
            i = i + 1
        end
    end
    if #currentInputs > 0 then
        table.insert(recipes, { inputs = currentInputs, outputs = {}, action = "" })
    end
    return recipes
end

local function buildRecipeHtml(rec, index)
    if not rec or (#rec.inputs == 0 and #rec.outputs == 0) then return nil end
    local container = html.create("div"):addClass("chem-recipe-block")

    if index == 1 then
        local heading = html.create("div"):addClass("chem-heading"):attr("data-kind", "recipe")
        local hcontent = heading:tag("div"):addClass("chem-heading-content")
        hcontent:tag("span"):addClass("heading-text"):wikitext("Рецепты")
        hcontent:tag("span"):addClass("collapse-btn"):wikitext("развернуть")
        container:node(heading)
    end

    local collapsible = html.create("div"):addClass("collapsible collapsed"):attr("data-kind", "recipe")

    local inner = html.create("div"):addClass("chem-recipe")

    -- INPUTS (левая колонка)
    local inputsDiv = html.create("div"):addClass("recipe-inputs")
    for _, it in ipairs(rec.inputs) do
        local item = inputsDiv:tag("div"):addClass("recipe-item")
        -- Используем MediaWiki-синтаксис файла — надёжно рендерится
        item:wikitext('[[File:Beaker.png|36px|class=chem-beaker]] ' .. it.name .. ' [' .. it.qty .. ']')
    end
    inner:node(inputsDiv)

    -- Действие
    if rec.action ~= "" then
        local actionDiv = inner:tag("div"):addClass("recipe-action")
        actionDiv:wikitext(rec.action)
    end

    -- OUTPUTS (правая колонка)
    local outputsDiv = html.create("div"):addClass("recipe-outputs")
    for _, it in ipairs(rec.outputs) do
        local item = outputsDiv:tag("div"):addClass("recipe-item")
        item:wikitext('[[File:Beaker.png|36px|class=chem-beaker]] ' .. 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")

    local heading = html.create("div"):addClass("chem-heading"):attr("data-kind", "effects")
    local hcontent = heading:tag("div"):addClass("chem-heading-content")
    hcontent:tag("span"):addClass("heading-text"):wikitext("Эффекты")
    hcontent:tag("span"):addClass("collapse-btn"):wikitext("развернуть")
    container:node(heading)

    local collapsible = html.create("div"):addClass("collapsible collapsed"):attr("data-kind", "effects")
    local inner = html.create("div"):addClass("chem-effects")

    for effect in mw.text.gsplit(effects, "[;\n]") do
        effect = mw.text.trim(effect)
        if effect ~= "" then
            inner:tag("div"):addClass("chem-effect-line"):wikitext(effect)
        end
    end
    collapsible:node(inner)
    container:node(collapsible)
    return container
end

function p.card(frame)
    local args = getArgs(frame)
    local name = args.name or "—"
    local color = args.color or "#8cf"
    local border = args.border or "#444"
    local desc = args.desc or ""

    local recipes = {}
    local i = 1
    while true do
        local key = i == 1 and "recipe" or ("recipe" .. i)
        local recipeStr = args[key]
        if not recipeStr then break end
        local parsedList = parseRecipeString(recipeStr)
        for _, rec in ipairs(parsedList) do
            table.insert(recipes, rec)
        end
        i = i + 1
    end

    local container = html.create("div"):addClass("chem-card")
    container:attr("style", "border-color:" .. border .. "; --card-accent:" .. color)

    container:tag("div"):addClass("chem-name-header"):wikitext(name)

    if #recipes > 0 then
        for idx, rec in ipairs(recipes) do
            local block = buildRecipeHtml(rec, idx)
            if block then container:node(block) end
        end
    end

    local effectsBlock = buildEffectsHtml(args.effects)
    if effectsBlock then container:node(effectsBlock) end

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

    return tostring(container)
end

return p