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

Материал из Space Stories Wiki
Нет описания правки
Нет описания правки
Метка: ручная отмена
 
(не показано 6 промежуточных версий этого же участника)
Строка 4: Строка 4:


local function getArgs(frame)
local function getArgs(frame)
    local args = {}
     if type(frame) == "table" and frame.args then return frame.args end
     if type(frame) == "table" and frame.args then
     return mw.getCurrentFrame():getParent().args or {}
        args = frame.args
     else
        args = mw.getCurrentFrame():getParent().args or {}
    end
    return args
end
end


Строка 25: Строка 20:
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
     name = mw.text.trim(name)
    return {name = name, qty = qty or "1"}
end
end


local function parseRecipeString(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.lower(line):find("смеш") then
    local i = 1
            splitIdx = i
     while i <= #lines do
            action = "смешайте"
        local line = lines[i]
             break
        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
     end
     end
    local inputs, outputs = {}, {}
     if #currentInputs > 0 then
     if splitIdx then
         table.insert(recipes, { inputs = currentInputs, outputs = {}, action = "" })
         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
     end
     return {inputs=inputs, outputs=outputs, action=action}
     return recipes
end
end


local function buildRecipeHtml(rec)
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")
     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("Рецепт")
     if index == 1 then
    container:node(heading)
        local heading = html.create("div"):addClass("chem-heading"):attr("data-kind", "recipe")
      
        local hcontent = heading:tag("div"):addClass("chem-heading-content")
     local collapsible = html.create("div"):addClass("collapsible collapsed"):attr("data-kind","recipe")
        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")
     local inner = html.create("div"):addClass("chem-recipe")


    -- INPUTS (левая колонка)
     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"):wikitext(it.name.." ["..it.qty.."]")
         local item = inputsDiv:tag("div"):addClass("recipe-item")
        -- Используем MediaWiki-синтаксис файла — надёжно рендерится
        item:wikitext('[[File:Beaker.png|36px|class=chem-beaker]] ' .. it.name .. ' [' .. it.qty .. ']')
     end
     end
     inner:node(inputsDiv)
     inner:node(inputsDiv)


     if rec.action and (#rec.inputs>0 or #rec.outputs>0) then
    -- Действие
         local actionDiv = html.create("div"):addClass("recipe-action")
     if rec.action ~= "" then
         local actionDiv = inner:tag("div"):addClass("recipe-action")
         actionDiv:wikitext(rec.action)
         actionDiv:wikitext(rec.action)
        inner:node(actionDiv)
     end
     end


    -- OUTPUTS (правая колонка)
     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"):wikitext(it.name.." ["..it.qty.."]")
         local item = outputsDiv:tag("div"):addClass("recipe-item")
        item:wikitext('[[File:Beaker.png|36px|class=chem-beaker]] ' .. it.name .. ' [' .. it.qty .. ']')
     end
     end
     inner:node(outputsDiv)
     inner:node(outputsDiv)
Строка 87: Строка 109:


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("chem-effects-block")
     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("Эффекты")
     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)
     container:node(heading)
   
 
     local collapsible = html.create("div"):addClass("collapsible collapsed"):attr("data-kind","effects")
     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")
    local ul = html.create("ul")
 
   
    local seenEffects = {}
     for effect in mw.text.gsplit(effects, "[;\n]") do
     for effect in mw.text.gsplit(effects, "[;\n]") do
         effect = mw.text.trim(effect)
         effect = mw.text.trim(effect)
         if effect~="" and not seenEffects[effect] then
         if effect ~= "" then
             seenEffects[effect] = true
             inner:tag("div"):addClass("chem-effect-line"):wikitext(effect)
            ul:tag("li"):wikitext(effect)
         end
         end
     end
     end
    inner:node(ul)
     collapsible:node(inner)
     collapsible:node(inner)
     container:node(collapsible)
     container:node(collapsible)
Строка 116: Строка 137:
     local color = args.color or "#8cf"
     local color = args.color or "#8cf"
     local border = args.border or "#444"
     local border = args.border or "#444"
    local effects = args.effects or ""
     local desc = args.desc or ""
     local desc = args.desc or ""


Строка 122: Строка 142:
     local i = 1
     local i = 1
     while true do
     while true do
         local key = "recipe"
         local key = i == 1 and "recipe" or ("recipe" .. i)
        if i > 1 then key = key .. i end
         local recipeStr = args[key]
         local recipeStr = args[key]
         if not recipeStr then break end
         if not recipeStr then break end
         local parsed = parseRecipeString(recipeStr)
         local parsedList = parseRecipeString(recipeStr)
         if #parsed.inputs > 0 or #parsed.outputs > 0 or (parsed.action and parsed.action ~= "") then
         for _, rec in ipairs(parsedList) do
             table.insert(recipes, parsed)
             table.insert(recipes, rec)
         end
         end
         i = i + 1
         i = i + 1
Строка 134: Строка 153:


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


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


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


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


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

Текущая версия от 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