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

Материал из Space Stories Wiki
Нет описания правки
Метка: отменено
Нет описания правки
Метка: отменено
Строка 29: Строка 29:
         qty = "1"
         qty = "1"
     end
     end
     name = mw.text.trim(name)
     return {name = mw.text.trim(name), qty = qty or "1"}
    return {name = name, qty = qty or "1"}
end
end


Строка 56: Строка 55:
local function buildRecipeHtml(parsed)
local function buildRecipeHtml(parsed)
     if #parsed.inputs == 0 and #parsed.outputs == 0 and (not parsed.action or parsed.action=="") then return nil end
     if #parsed.inputs == 0 and #parsed.outputs == 0 and (not parsed.action or parsed.action=="") then return nil end
     local container = html.create("div"):addClass("mw-collapsible mw-collapsed")
     local container = html.create("div"):addClass("chem-recipe")
        :attr("style","width:100%; overflow:auto; border-bottom: 1px solid #303038; margin-top:0.3em;")
    if #parsed.inputs > 0 then
    container:tag("div"):attr("style","font-weight:bold;line-height:1.6; padding:1px"):wikitext("Рецепт")
         local inputsDiv = html.create("div")
    local content = html.create("div")
        for _, it in ipairs(parsed.inputs) do
         :attr("style","display:grid; grid-template-columns: repeat(auto-fit, minmax(100px, 1fr)); gap:0.5em; padding:0.3em 0;")
            inputsDiv:tag("div"):wikitext(it.name.." ["..it.qty.."]")
 
        end
    local inputsDiv = html.create("div")
        container:node(inputsDiv)
    for _, it in ipairs(parsed.inputs) do
        inputsDiv:tag("div"):wikitext(it.name.." ["..it.qty.."]")
     end
     end
    local actionDiv = html.create("div"):attr("style","text-align:center")
     if parsed.action and parsed.action~="" then
     if parsed.action and parsed.action~="" then
        local actionDiv = html.create("div"):attr("style","text-align:center")
         actionDiv:wikitext(parsed.action)
         actionDiv:wikitext(parsed.action)
        container:node(actionDiv)
     end
     end
 
    if #parsed.outputs > 0 then
    local outputsDiv = html.create("div")
        local outputsDiv = html.create("div")
    for _, it in ipairs(parsed.outputs) do
        for _, it in ipairs(parsed.outputs) do
        outputsDiv:tag("div"):wikitext(it.name.." ["..it.qty.."]")
            outputsDiv:tag("div"):wikitext(it.name.." ["..it.qty.."]")
        end
        container:node(outputsDiv)
     end
     end
    content:node(inputsDiv)
    content:node(actionDiv)
    content:node(outputsDiv)
    container:node(content)
     return container
     return container
end
end
Строка 86: Строка 80:
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")
     local container = html.create("div"):addClass("chem-effects")
        :attr("style","width:100%; overflow:auto; border-bottom: 1px solid #303038; margin-top:0.3em;")
    container:tag("div"):attr("style","font-weight:bold;line-height:1.6; padding:1px"):wikitext("Эффекты")
    local content = html.create("div"):attr("style","padding:0.3em")
     local ul = html.create("ul")
     local ul = html.create("ul")
     for effect in mw.text.gsplit(effects, "[;\n]") do
     for effect in mw.text.gsplit(effects, "[;\n]") do
Строка 95: Строка 86:
         if effect~="" then ul:tag("li"):wikitext(effect) end
         if effect~="" then ul:tag("li"):wikitext(effect) end
     end
     end
     content:node(ul)
     container:node(ul)
    container:node(content)
     return container
     return container
end
end
Строка 102: Строка 92:
function p.card(frame)
function p.card(frame)
     local args = getArgs(frame)
     local args = getArgs(frame)
     local name = args.name or args.title or "—"
     local name = args.name or "—"
     local color = args.color or "#000000"
     local color = args.color or "#fff"
     local border = args.border or "#000000"
     local border = args.border or "#555"
     local bg = args.bg or "#101010"
     local bg = args.bg or "#101010"
     local recipeStr = args.recipe or ""
     local recipeStr = args.recipe or ""
Строка 113: Строка 103:


     local container = html.create("div"):addClass("chem-card")
     local container = html.create("div"):addClass("chem-card")
         :attr("style","border:2px solid "..border.."; border-radius:10px; background-color:"..bg.."; margin:0.5em; padding:0.3em;")
         :attr("style","border:2px solid "..border.."; border-radius:8px; background-color:"..bg.."; padding:0.5em;")


     container:tag("h3"):attr("style","margin:0; padding:0.3em; color:"..color..";"):wikitext(name)
     container:tag("h3"):attr("style","margin:0 0 0.5em 0; color:"..color.."; font-size:16px;"):wikitext(name)


     local recBlock = buildRecipeHtml(parsed)
     local recBlock = buildRecipeHtml(parsed)
Строка 124: Строка 114:


     if desc and desc~="" then
     if desc and desc~="" then
         container:tag("div"):attr("style","margin-top:0.3em; padding:0 0.3em;"):wikitext(desc)
         container:tag("div"):attr("style","margin-top:0.5em; font-size:14px; color:#ccc;"):wikitext(desc)
     end
     end



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

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

local p = {}
local mw = mw
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
    return {name = mw.text.trim(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(parsed)
    if #parsed.inputs == 0 and #parsed.outputs == 0 and (not parsed.action or parsed.action=="") then return nil end
    local container = html.create("div"):addClass("chem-recipe")
    if #parsed.inputs > 0 then
        local inputsDiv = html.create("div")
        for _, it in ipairs(parsed.inputs) do
            inputsDiv:tag("div"):wikitext(it.name.." ["..it.qty.."]")
        end
        container:node(inputsDiv)
    end
    if parsed.action and parsed.action~="" then
        local actionDiv = html.create("div"):attr("style","text-align:center")
        actionDiv:wikitext(parsed.action)
        container:node(actionDiv)
    end
    if #parsed.outputs > 0 then
        local outputsDiv = html.create("div")
        for _, it in ipairs(parsed.outputs) do
            outputsDiv:tag("div"):wikitext(it.name.." ["..it.qty.."]")
        end
        container:node(outputsDiv)
    end
    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")
    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
    container:node(ul)
    return container
end

function p.card(frame)
    local args = getArgs(frame)
    local name = args.name or "—"
    local color = args.color or "#fff"
    local border = args.border or "#555"
    local bg = args.bg or "#101010"
    local recipeStr = args.recipe or ""
    local effects = args.effects or ""
    local desc = args.desc or ""

    local parsed = parseRecipeString(recipeStr)

    local container = html.create("div"):addClass("chem-card")
        :attr("style","border:2px solid "..border.."; border-radius:8px; background-color:"..bg.."; padding:0.5em;")

    container:tag("h3"):attr("style","margin:0 0 0.5em 0; color:"..color.."; font-size:16px;"):wikitext(name)

    local recBlock = buildRecipeHtml(parsed)
    if recBlock then container:node(recBlock) end

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

    if desc and desc~="" then
        container:tag("div"):attr("style","margin-top:0.5em; font-size:14px; color:#ccc;"):wikitext(desc)
    end

    return tostring(container)
end

return p