Открыть меню
Переключить меню настроек
Открыть персональное меню
Вы не представились системе
Ваш IP-адрес будет виден всем, если вы внесёте какие-либо изменения.

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

Материал из Space Stories Wiki
Нет описания правки
Нет описания правки
 
(не показано 59 промежуточных версий этого же участника)
Строка 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


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
  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 = {}} end
    if not recipeStr or recipeStr == "" then return {} end
  local lines = splitLines(recipeStr)
    local lines = splitLines(recipeStr)
  local splitIdx
    local steps = {}
  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
      break
        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(steps, {
                    inputs = currentInputs,
                    outputs = outputs,
                    action = mw.text.trim(line)
                })
                currentInputs = {}
            else
                i = i + 1
            end
        else
            table.insert(currentInputs, parseLineToItem(line))
            i = i + 1
        end
    end
    if #currentInputs > 0 then
        table.insert(steps, { inputs = currentInputs, outputs = {}, action = "" })
     end
     end
  end
     return steps
  local inputs = {}
  local 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}
end
end


local function parseRecipeN(args)
local FILE_PATTERN = "(%[%[File:[^]]+%]%])"
  local inputs = {}
 
  local outputs = {}
local function buildRecipeStepHtml(rec)
  for i = 1, 24 do
    if not rec or (#rec.inputs == 0 and #rec.outputs == 0) then return nil end
     local name = args["recipe"..i] or args["r"..i]
 
     local q = args["recipe"..i.."_q"] or args["r"..i.."_q"] or args["q"..i]
     local inner = html.create("div"):addClass("chem-recipe")
    local part = args["recipe"..i.."_part"] or args["r"..i.."_part"]
 
     if name and name ~= "" then
     local inputsDiv = html.create("div"):addClass("recipe-inputs")
      local qty = q and q ~= "" and tostring(q) or "1"
    for _, it in ipairs(rec.inputs) do
      local item = {name = name, qty = qty}
        local item = inputsDiv:tag("div"):addClass("recipe-item")
      if part and (part == "out" or part == "output" or part == "result") then
        item:wikitext('[[File:Beaker.png|36px|class=chem-reagent-icon]] ' .. it.name .. ' [' .. it.qty .. ']')
         table.insert(outputs, item)
    end
      else
    inner:node(inputsDiv)
        table.insert(inputs, item)
 
      end
     if rec.action and rec.action ~= "" then
        local actionDiv = inner:tag("div"):addClass("recipe-action")
        local content = actionDiv:tag("div"):addClass("recipe-action-content")
 
        local sprite = '[[File:Beaker.png|36px|class=chem-reagent-icon]]'
        local actionText = rec.action
 
         local customSprite = mw.ustring.match(actionText, FILE_PATTERN)
        if customSprite then
            sprite = customSprite
            actionText = mw.text.trim(mw.ustring.gsub(actionText, FILE_PATTERN, ""))
        end
 
        content:tag("div"):addClass("action-sprite"):wikitext(sprite)
        content:tag("div"):addClass("action-text"):wikitext(actionText)
     end
     end
  end
 
  return {inputs = inputs, outputs = 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-reagent-icon]] ' .. it.name .. ' [' .. it.qty .. ']')
    end
    inner:node(outputsDiv)
 
    return inner
end
end


local function mergeRecipes(recipeTable1, recipeTable2)
local function buildEffectsHtml(effects)
  local inputs = {}
    if not effects or effects == "" or effects == "Нет" then return nil end
  for _,v in ipairs(recipeTable1.inputs or {}) do table.insert(inputs, v) end
    local container = html.create("div"):addClass("chem-effects-block")
  for _,v in ipairs(recipeTable2.inputs or {}) do table.insert(inputs, v) end
    local heading = html.create("div"):addClass("chem-heading"):attr("data-kind", "effects")
  local outputs = {}
    local hcontent = heading:tag("div"):addClass("chem-heading-content")
  for _,v in ipairs(recipeTable1.outputs or {}) do table.insert(outputs, v) end
    hcontent:tag("span"):addClass("heading-text"):wikitext("Эффекты")
  for _,v in ipairs(recipeTable2.outputs or {}) do table.insert(outputs, v) end
    hcontent:tag("span"):addClass("collapse-btn"):wikitext("развернуть")
  return {inputs = inputs, outputs = outputs}
    container:node(heading)
end


local function buildRecipeHtml(rec)
    local collapsible = html.create("div"):addClass("collapsible collapsed"):attr("data-kind", "effects")
  if not rec then return nil end
     local inner = html.create("div"):addClass("chem-effects")
  local inputs = rec.inputs or {}
     for effect in mw.text.gsplit(effects, "[;\n]") do
  local outputs = rec.outputs or {}
        effect = mw.text.trim(effect)
  if #inputs == 0 and #outputs == 0 then return nil end
        if effect ~= "" then
  local container = html.create("div")
            inner:tag("div"):addClass("chem-effect-line"):wikitext(effect)
  container:addClass("chem-recipe")
        end
  local inGroup = container:tag("div")
  inGroup:addClass("recipe-group recipe-inputs")
  inGroup:tag("b"):wikitext("Ингредиенты")
  for _,it in ipairs(inputs) do
     local row = inGroup:tag("div")
    row:addClass("recipe-item")
     row:tag("span"):addClass("name"):wikitext(mw.text.escape(it.name))
    row:tag("span"):addClass("qty"):wikitext("×"..mw.text.escape(tostring(it.qty)))
  end
  if #outputs > 0 then
    local outGroup = container:tag("div")
    outGroup:addClass("recipe-group recipe-outputs")
    outGroup:tag("b"):wikitext("Результат")
    for _,it in ipairs(outputs) do
      local row = outGroup:tag("div")
      row:addClass("recipe-item")
      row:tag("span"):addClass("name"):wikitext(mw.text.escape(it.name))
      row:tag("span"):addClass("qty"):wikitext("×"..mw.text.escape(tostring(it.qty)))
     end
     end
  end
    collapsible:node(inner)
  return tostring(container)
    container:node(collapsible)
    return container
end
end


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 formula = args.formula or args.formulae or ""
    local color = args.color or "#8cf"
  local color = args.color or args.colour or ""
    local border = args.border or "#444"
  local border = args.border or args.bordercolor or ""
    local desc = args.desc or ""
  local bg = args.bg or args.bgcolor or ""
 
  local recipeStr = args.recipe or args.rezept or ""
    local variants = {}
  local parsedStr = parseRecipeString(recipeStr)
    local i = 1
  local parsedN = parseRecipeN(args)
    while true do
  local recipeMerged = mergeRecipes(parsedStr, parsedN)
        local key = i == 1 and "recipe" or ("recipe" .. i)
  local effects = args.effects or args.effect or ""
        local recipeStr = args[key]
  local desc = args.description or args.desc or ""
        if not recipeStr then break end
  local container = html.create("div")
        local steps = parseRecipeString(recipeStr)
  container:addClass("chem-card")
        if #steps > 0 then
  local styleParts = {}
            table.insert(variants, {id = i, steps = steps})
  if color and color ~= "" then table.insert(styleParts, "--card-accent:"..color) end
        end
  if border and border ~= "" then table.insert(styleParts, "--card-border:"..border) end
        i = i + 1
  if bg and bg ~= "" then table.insert(styleParts, "--card-bg:"..bg) end
    end
  if #styleParts > 0 then container:attr("style", table.concat(styleParts, ";")) end
 
  local header = container:tag("div")
    local container = html.create("div"):addClass("chem-card")
  header:addClass("chem-header")
    container:attr("style", "border-color:" .. border .. "; --card-accent:" .. color)
  header:tag("h3"):wikitext(mw.text.escape(name))
    container:tag("div"):addClass("chem-name-header"):wikitext(name)
  if formula and formula ~= "" then header:tag("span"):addClass("formula"):wikitext(mw.text.escape(formula)) end
 
  local recipeHtml = buildRecipeHtml(recipeMerged)
    if #variants > 0 then
  if recipeHtml then container:tag("div"):addClass("chem-section"):wikitext("<b>Рецепт</b>") container:wikitext(recipeHtml) end
        local recipeBlock = html.create("div"):addClass("chem-recipe-block")
  if effects and effects ~= "" then
        local heading = html.create("div"):addClass("chem-heading"):attr("data-kind", "recipe")
    local effHtml = html.create("div")
        local hcontent = heading:tag("div"):addClass("chem-heading-content")
    effHtml:addClass("chem-section")
        hcontent:tag("span"):addClass("heading-text"):wikitext("Рецепты")
    effHtml:tag("b"):wikitext("Эффекты")
        hcontent:tag("span"):addClass("collapse-btn"):wikitext("развернуть")
    local ul = html.create("ul")
        recipeBlock:node(heading)
    ul:addClass("chem-effects")
 
    for effect in mw.text.gsplit(effects, "[;\n]") do
        local collapsible = html.create("div"):addClass("collapsible collapsed"):attr("data-kind", "recipe")
      effect = mw.text.trim(effect)
        local stepsContainer = html.create("div"):addClass("chem-recipe-steps")
      if effect ~= "" then ul:tag("li"):wikitext(effect) end
 
        for vidx, variant in ipairs(variants) do
            if #variants > 1 then
                local varHeader = stepsContainer:tag("div"):addClass("recipe-variant-header")
                varHeader:wikitext("Вариант " .. vidx)
            end
 
            for _, rec in ipairs(variant.steps) do
                local step = buildRecipeStepHtml(rec)
                if step then stepsContainer:node(step) end
            end
        end
 
        collapsible:node(stepsContainer)
        recipeBlock:node(collapsible)
        container:node(recipeBlock)
    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
     end
    effHtml:node(ul)
 
     container:node(effHtml)
     return tostring(container)
  end
  if desc and desc ~= "" then container:tag("div"):addClass("chem-desc"):wikitext(desc) end
  return tostring(container)
end
end


return p
return p

Текущая версия от 13:20, 4 апреля 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 steps = {}
    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(steps, {
                    inputs = currentInputs,
                    outputs = outputs,
                    action = mw.text.trim(line)
                })
                currentInputs = {}
            else
                i = i + 1
            end
        else
            table.insert(currentInputs, parseLineToItem(line))
            i = i + 1
        end
    end
    if #currentInputs > 0 then
        table.insert(steps, { inputs = currentInputs, outputs = {}, action = "" })
    end
    return steps
end

local FILE_PATTERN = "(%[%[File:[^]]+%]%])"

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

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

    local inputsDiv = html.create("div"):addClass("recipe-inputs")
    for _, it in ipairs(rec.inputs) do
        local item = inputsDiv:tag("div"):addClass("recipe-item")
        item:wikitext('[[File:Beaker.png|36px|class=chem-reagent-icon]] ' .. it.name .. ' [' .. it.qty .. ']')
    end
    inner:node(inputsDiv)

    if rec.action and rec.action ~= "" then
        local actionDiv = inner:tag("div"):addClass("recipe-action")
        local content = actionDiv:tag("div"):addClass("recipe-action-content")

        local sprite = '[[File:Beaker.png|36px|class=chem-reagent-icon]]'
        local actionText = rec.action

        local customSprite = mw.ustring.match(actionText, FILE_PATTERN)
        if customSprite then
            sprite = customSprite
            actionText = mw.text.trim(mw.ustring.gsub(actionText, FILE_PATTERN, ""))
        end

        content:tag("div"):addClass("action-sprite"):wikitext(sprite)
        content:tag("div"):addClass("action-text"):wikitext(actionText)
    end

    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-reagent-icon]] ' .. it.name .. ' [' .. it.qty .. ']')
    end
    inner:node(outputsDiv)

    return inner
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 variants = {}
    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 steps = parseRecipeString(recipeStr)
        if #steps > 0 then
            table.insert(variants, {id = i, steps = steps})
        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 #variants > 0 then
        local recipeBlock = html.create("div"):addClass("chem-recipe-block")
        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("развернуть")
        recipeBlock:node(heading)

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

        for vidx, variant in ipairs(variants) do
            if #variants > 1 then
                local varHeader = stepsContainer:tag("div"):addClass("recipe-variant-header")
                varHeader:wikitext("Вариант " .. vidx)
            end

            for _, rec in ipairs(variant.steps) do
                local step = buildRecipeStepHtml(rec)
                if step then stepsContainer:node(step) end
            end
        end

        collapsible:node(stepsContainer)
        recipeBlock:node(collapsible)
        container:node(recipeBlock)
    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