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

Материал из Space Stories Wiki
Нет описания правки
Нет описания правки
 
(не показана 1 промежуточная версия этого же участника)
Строка 36: Строка 36:
local function parseRecipeString(recipeStr)
local function parseRecipeString(recipeStr)
     if not recipeStr or recipeStr == "" then
     if not recipeStr or recipeStr == "" then
         return {inputs = {}, outputs = {}, action = ""}
         return nil
     end
     end


     local lines = splitLines(recipeStr)
     local lines = splitLines(recipeStr)
    if #lines == 0 then
        return nil
    end
     local splitIdx, action
     local splitIdx, action
     for i, line in ipairs(lines) do
     for i, line in ipairs(lines) do
         if mw.ustring.lower(line):find("смеш") then
         if mw.ustring.lower(line):find("смеш") then
Строка 63: Строка 66:
             table.insert(outputs, parseLineToItem(lines[i]))
             table.insert(outputs, parseLineToItem(lines[i]))
         end
         end
    end
    if #inputs == 0 and #outputs == 0 then
        return nil
     end
     end


Строка 68: Строка 75:
         inputs = inputs,
         inputs = inputs,
         outputs = outputs,
         outputs = outputs,
         action = action
         action = action or ""
     }
     }
end
end


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


     local heading = html.create("div"):addClass("chem-heading")
     local heading = html.create("div")
        :addClass("chem-heading")
        :attr("data-kind", "recipe")
     heading:tag("span"):wikitext("Рецепт")
     heading:tag("span"):wikitext("Рецепт")
     container:node(heading)
     container:node(heading)
    local collapsible = html.create("div")
        :addClass("collapsible collapsed")
        :attr("data-kind", "recipe")


     local inner = html.create("div"):addClass("chem-recipe")
     local inner = html.create("div"):addClass("chem-recipe")
Строка 83: Строка 100:
     local inputsDiv = html.create("div"):addClass("recipe-inputs")
     local inputsDiv = html.create("div"):addClass("recipe-inputs")
     for _, it in ipairs(rec.inputs) do
     for _, it in ipairs(rec.inputs) do
         inputsDiv:tag("div"):addClass("recipe-item")
         inputsDiv:tag("div")
            :addClass("recipe-item")
             :wikitext(it.name .. " [" .. it.qty .. "]")
             :wikitext(it.name .. " [" .. it.qty .. "]")
     end
     end
     inner:node(inputsDiv)
     inner:node(inputsDiv)


     if rec.action and rec.action ~= "" then
     if rec.action ~= "" then
         inner:tag("div"):addClass("recipe-action"):wikitext(rec.action)
         inner:tag("div")
            :addClass("recipe-action")
            :wikitext(rec.action)
     end
     end


     local outputsDiv = html.create("div"):addClass("recipe-outputs")
     local outputsDiv = html.create("div"):addClass("recipe-outputs")
     for _, it in ipairs(rec.outputs) do
     for _, it in ipairs(rec.outputs) do
         outputsDiv:tag("div"):addClass("recipe-item")
         outputsDiv:tag("div")
            :addClass("recipe-item")
             :wikitext(it.name .. " [" .. it.qty .. "]")
             :wikitext(it.name .. " [" .. it.qty .. "]")
     end
     end
     inner:node(outputsDiv)
     inner:node(outputsDiv)


     container:node(inner)
    collapsible:node(inner)
     container:node(collapsible)
     return container
     return container
end
end
Строка 110: Строка 132:
     local container = html.create("div"):addClass("chem-effects-block")
     local container = html.create("div"):addClass("chem-effects-block")


     local heading = html.create("div"):addClass("chem-heading")
     local heading = html.create("div")
        :addClass("chem-heading")
        :attr("data-kind", "effects")
     heading:tag("span"):wikitext("Эффекты")
     heading:tag("span"):wikitext("Эффекты")
     container:node(heading)
     container:node(heading)
    local collapsible = html.create("div")
        :addClass("collapsible collapsed")
        :attr("data-kind", "effects")


     local inner = html.create("div"):addClass("chem-effects")
     local inner = html.create("div"):addClass("chem-effects")
Строка 127: Строка 155:


     inner:node(ul)
     inner:node(ul)
     container:node(inner)
    collapsible:node(inner)
     container:node(collapsible)
     return container
     return container
end
end
Строка 142: Строка 171:
     local recipes = {}
     local recipes = {}
     local i = 1
     local i = 1
     while true do
     while true do
         local key = i == 1 and "recipe" or ("recipe" .. i)
         local key = i == 1 and "recipe" or ("recipe" .. i)
Строка 149: Строка 177:


         local parsed = parseRecipeString(recipeStr)
         local parsed = parseRecipeString(recipeStr)
         if #parsed.inputs > 0 or #parsed.outputs > 0 then
         if parsed then
             table.insert(recipes, parsed)
             table.insert(recipes, parsed)
         end
         end
Строка 161: Строка 189:


     for _, rec in ipairs(recipes) do
     for _, rec in ipairs(recipes) do
         container:node(buildRecipeHtml(rec))
         local block = buildRecipeHtml(rec)
        if block then
            container:node(block)
        end
     end
     end



Текущая версия от 07:05, 10 января 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 nil
    end

    local lines = splitLines(recipeStr)
    if #lines == 0 then
        return nil
    end

    local splitIdx, action
    for i, line in ipairs(lines) do
        if mw.ustring.lower(line):find("смеш") then
            splitIdx = i
            action = "смешайте"
            break
        end
    end

    local inputs, outputs = {}, {}

    if splitIdx then
        for i = 1, splitIdx - 1 do
            table.insert(outputs, parseLineToItem(lines[i]))
        end
        for i = splitIdx + 1, #lines do
            table.insert(inputs, parseLineToItem(lines[i]))
        end
    else
        for i = 1, #lines do
            table.insert(outputs, parseLineToItem(lines[i]))
        end
    end

    if #inputs == 0 and #outputs == 0 then
        return nil
    end

    return {
        inputs = inputs,
        outputs = outputs,
        action = action or ""
    }
end

local function buildRecipeHtml(rec)
    if not rec or (#rec.inputs == 0 and #rec.outputs == 0) then
        return nil
    end

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

    local heading = html.create("div")
        :addClass("chem-heading")
        :attr("data-kind", "recipe")
    heading:tag("span"):wikitext("Рецепт")
    container:node(heading)

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

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

    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)

    if rec.action ~= "" then
        inner:tag("div")
            :addClass("recipe-action")
            :wikitext(rec.action)
    end

    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")

    local heading = html.create("div")
        :addClass("chem-heading")
        :attr("data-kind", "effects")
    heading:tag("span"):wikitext("Эффекты")
    container:node(heading)

    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")

    local seen = {}
    for effect in mw.text.gsplit(effects, "[;\n]") do
        effect = mw.text.trim(effect)
        if effect ~= "" and not seen[effect] then
            seen[effect] = true
            ul:tag("li"):wikitext(effect)
        end
    end

    inner:node(ul)
    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 effects = args.effects or ""
    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 parsed = parseRecipeString(recipeStr)
        if parsed then
            table.insert(recipes, parsed)
        end
        i = i + 1
    end

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

    container:tag("h3"):wikitext(name)

    for _, rec in ipairs(recipes) do
        local block = buildRecipeHtml(rec)
        if block then
            container:node(block)
        end
    end

    local effectsBlock = buildEffectsHtml(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