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

Материал из Space Stories Wiki
Нет описания правки
Метка: отменено
Нет описания правки
 
(не показана 31 промежуточная версия этого же участника)
Строка 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
end
            table.insert(t, line)
return t
        end
    end
    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
        name = l
qty = "1"
        qty = "1"
end
    end
return {name = mw.text.trim(name), qty = qty or "1"}
    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
local lines = splitLines(recipeStr)
        return nil
local splitIdx, action
    end
for i, line in ipairs(lines) do
 
if mw.ustring.find(mw.ustring.lower(line), "смеш") then
    local lines = splitLines(recipeStr)
splitIdx = i
    if #lines == 0 then
action = line
        return nil
break
    end
end
 
end
    local splitIdx, action
local inputs, outputs = {}, {}
    for i, line in ipairs(lines) do
if splitIdx then
        if mw.ustring.lower(line):find("смеш") then
for i = 1, splitIdx - 1 do table.insert(inputs, parseLineToItem(lines[i])) end
            splitIdx = i
for i = splitIdx + 1, #lines do table.insert(outputs, parseLineToItem(lines[i])) end
            action = "смешайте"
else
            break
for i = 1, #lines do table.insert(inputs, parseLineToItem(lines[i])) end
        end
end
    end
return {inputs = inputs, outputs = outputs, action = action}
 
    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
end


local function buildRecipeHtml(parsed)
local function buildRecipeHtml(rec)
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
local container = html.create("div"):addClass("mw-collapsible mw-collapsed"):attr("style","width:100%; overflow:auto; border-bottom: 1px solid #303038;")
        return nil
container:tag("div"):attr("style","font-weight:bold;line-height:1.6; padding:1px"):wikitext("Рецепты")
    end
local content = html.create("div"):addClass("mw-collapsible-content")
 
for _, it in ipairs(parsed.inputs) do
    local container = html.create("div"):addClass("chem-recipe-block")
content:tag("div"):wikitext(it.name.." ["..it.qty.."]")
 
end
    local heading = html.create("div")
if parsed.action and parsed.action~="" then
        :addClass("chem-heading")
content:tag("div"):wikitext(parsed.action)
        :attr("data-kind", "recipe")
end
    heading:tag("span"):wikitext("Рецепт")
for _, it in ipairs(parsed.outputs) do
    container:node(heading)
content:tag("div"):wikitext(it.name.." ["..it.qty.."]")
 
end
    local collapsible = html.create("div")
container:node(content)
        :addClass("collapsible collapsed")
return container
        :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
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
local container = html.create("div"):addClass("mw-collapsible mw-collapsed"):attr("style","width:100%; overflow:auto; border-bottom: 1px solid #303038;")
        return nil
container:tag("div"):attr("style","font-weight:bold;line-height:1.6; padding:1px"):wikitext("Эффекты")
    end
local content = html.create("div"):addClass("mw-collapsible-content"):attr("style","padding:0 0.5em")
 
for effect in mw.text.gsplit(effects, "[;\n]") do
    local container = html.create("div"):addClass("chem-effects-block")
effect = mw.text.trim(effect)
 
if effect ~= "" then content:tag("div"):wikitext(effect) end
    local heading = html.create("div")
end
        :addClass("chem-heading")
container:node(content)
        :attr("data-kind", "effects")
return container
    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
end


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


local parsed = parseRecipe(recipeStr)
    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 card = html.create("div"):addClass("quickbox"):attr("style","width:25em;")
        local parsed = parseRecipeString(recipeStr)
local outer = html.create("div"):attr("style","border:2px solid black; border-radius:0 0 21px 21px")
        if parsed then
local middle = html.create("div"):attr("style","border:3px solid "..border.."; border-radius:0 0 20px 20px")
            table.insert(recipes, parsed)
local inner = html.create("div"):attr("style","border:2px solid black; border-radius:0 0 18px 18px")
        end
        i = i + 1
    end


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


if parsed then
    container:tag("h3"):wikitext(name)
local rhtml = buildRecipeHtml(parsed)
if rhtml then inner:node(rhtml) end
end


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


if desc and desc~="" then
    local effectsBlock = buildEffectsHtml(effects)
inner:tag("div"):attr("style","padding:0 0.5em"):wikitext(desc)
    if effectsBlock then
end
        container:node(effectsBlock)
    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

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