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

Материал из Space Stories Wiki
Нет описания правки
Нет описания правки
Метка: ручная отмена
 
(не показано 46 промежуточных версий этого же участника)
Строка 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 = {}, 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 = line
        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
    if #currentInputs > 0 then
  local inputs = {}
        table.insert(recipes, { inputs = currentInputs, outputs = {}, action = "" })
  local outputs = {}
     end
  if splitIdx then
    return recipes
    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
end


local function parseRecipeN(args)
local function buildRecipeHtml(rec, index)
  local inputs, outputs, action = {}, {}, ""
    if not rec or (#rec.inputs == 0 and #rec.outputs == 0) then return nil end
  for i = 1, 24 do
    local container = html.create("div"):addClass("chem-recipe-block")
    local name = args["recipe"..i] or args["r"..i]
 
     local q = args["recipe"..i.."_q"] or args["r"..i.."_q"] or args["q"..i]
    if index == 1 then
    local part = args["recipe"..i.."_part"] or args["r"..i.."_part"]
        local heading = html.create("div"):addClass("chem-heading"):attr("data-kind", "recipe")
     if name and name ~= "" then
        local hcontent = heading:tag("div"):addClass("chem-heading-content")
      local qty = q and q ~= "" and tostring(q) or "1"
        hcontent:tag("span"):addClass("heading-text"):wikitext("Рецепты")
      local item = {name = name, qty = qty}
        hcontent:tag("span"):addClass("collapse-btn"):wikitext("развернуть")
      if part and (part == "out" or part == "output" or part == "result") then
        container:node(heading)
        table.insert(outputs, item)
    end
      else
 
         table.insert(inputs, item)
     local collapsible = html.create("div"):addClass("collapsible collapsed"):attr("data-kind", "recipe")
      end
 
    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
     end
     local act = args["recipe_action"]
     inner:node(outputsDiv)
    if act and act ~= "" then action = act end
  end
  return {inputs=inputs, outputs=outputs, action=action}
end


local function mergeRecipes(r1, r2)
    collapsible:node(inner)
  local inputs, outputs, action = {}, {}, r1.action or r2.action
    container:node(collapsible)
  for _,v in ipairs(r1.inputs or {}) do table.insert(inputs, v) end
    return container
  for _,v in ipairs(r2.inputs or {}) do table.insert(inputs, v) end
  for _,v in ipairs(r1.outputs or {}) do table.insert(outputs, v) end
  for _,v in ipairs(r2.outputs or {}) do table.insert(outputs, v) end
  return {inputs=inputs, outputs=outputs, action=action}
end
end


local function buildRecipeHtml(rec)
local function buildEffectsHtml(effects)
  if not rec then return nil end
    if not effects or effects == "" or effects == "Нет" then return nil end
  local container = html.create("div"):addClass("chem-recipe")
    local container = html.create("div"):addClass("chem-effects-block")


  local inputsDiv = html.create("div"):addClass("recipe-inputs")
    local heading = html.create("div"):addClass("chem-heading"):attr("data-kind", "effects")
  for _,it in ipairs(rec.inputs) do
    local hcontent = heading:tag("div"):addClass("chem-heading-content")
     inputsDiv:tag("div"):addClass("recipe-item"):wikitext(it.name.." ["..it.qty.."]")
     hcontent:tag("span"):addClass("heading-text"):wikitext("Эффекты")
  end
    hcontent:tag("span"):addClass("collapse-btn"):wikitext("развернуть")
  container:node(inputsDiv)
    container:node(heading)


  if rec.action and #rec.inputs>0 and #rec.outputs>0 then
    local collapsible = html.create("div"):addClass("collapsible collapsed"):attr("data-kind", "effects")
    container:tag("div"):addClass("recipe-action"):wikitext(rec.action)
    local inner = html.create("div"):addClass("chem-effects")
  end


  local outputsDiv = html.create("div"):addClass("recipe-outputs")
    for effect in mw.text.gsplit(effects, "[;\n]") do
  for _,it in ipairs(rec.outputs) do
        effect = mw.text.trim(effect)
    outputsDiv:tag("div"):addClass("recipe-item"):wikitext(it.name.." ["..it.qty.."]")
        if effect ~= "" then
  end
            inner:tag("div"):addClass("chem-effect-line"):wikitext(effect)
  container:node(outputsDiv)
        end
 
    end
  return tostring(container)
    collapsible:node(inner)
    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 color = args.color or args.colour or ""
    local color = args.color or "#8cf"
  local border = args.border or args.bordercolor or ""
    local border = args.border or "#444"
  local bg = args.bg or args.bgcolor or ""
    local desc = args.desc or ""
  local recipeStr = args.recipe or args.rezept or ""
 
  local parsedStr = parseRecipeString(recipeStr)
    local recipes = {}
  local parsedN = parseRecipeN(args)
    local i = 1
  local recipeMerged = mergeRecipes(parsedStr, parsedN)
    while true do
  local effects = args.effects or args.effect or ""
        local key = i == 1 and "recipe" or ("recipe" .. i)
  local desc = args.description or args.desc or ""
        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")
    local container = html.create("div"):addClass("chem-card")
  local styleParts = {}
    container:attr("style", "border-color:" .. border .. "; --card-accent:" .. color)
  if color ~= "" then table.insert(styleParts, "--card-accent:"..color) end
  if border ~= "" then table.insert(styleParts, "--card-border:"..border) end
  if bg ~= "" then table.insert(styleParts, "--card-bg:"..bg) end
  if #styleParts > 0 then container:attr("style", table.concat(styleParts, ";")) end


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


  if effects and effects ~= "" and effects ~= "Нет" then
    if #recipes > 0 then
    local effDiv = html.create("div"):addClass("chem-effects")
        for idx, rec in ipairs(recipes) do
    effDiv:tag("b"):wikitext("Эффекты:")
            local block = buildRecipeHtml(rec, idx)
    local ul = html.create("ul")
            if block then container:node(block) end
    for effect in mw.text.gsplit(effects, "[;\n]") do
        end
      effect = mw.text.trim(effect)
      if effect ~= "" then ul:tag("li"):wikitext(effect) end
     end
     end
    effDiv:node(ul)
    container:node(effDiv)
  end


  if desc and desc ~= "" then
    local effectsBlock = buildEffectsHtml(args.effects)
    container:tag("div"):addClass("chem-desc"):wikitext(desc)
    if effectsBlock then container:node(effectsBlock) end
  end
 
    if desc ~= "" then
        container:tag("div"):addClass("chem-desc"):wikitext(desc)
    end


  return tostring(container)
    return tostring(container)
end
end


return p
return p

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