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

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


local function getArgs(frame)
local function getArgs(frame)
  local args = {}
    if type(frame) == "table" and frame.args then
  if type(frame) == "table" and frame.args then
        return frame.args
    args = frame.args
    end
  else
     return mw.getCurrentFrame():getParent().args or {}
     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
  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
  name = mw.text.trim(name)
    return {
  return {name = name, qty = qty or "1"}
        name = mw.text.trim(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
  local lines = splitLines(recipeStr)
        return nil
  local splitIdx
    end
  for i,line in ipairs(lines) do
 
    if mw.ustring.lower(line):find("смеш") then
    local lines = splitLines(recipeStr)
      splitIdx = i
    if #lines == 0 then
      break
        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
     end
  end
  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


local function parseRecipeN(args)
     if #inputs == 0 and #outputs == 0 then
  local inputs = {}
         return nil
  local outputs = {}
  for i = 1, 24 do
    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 part = args["recipe"..i.."_part"] or args["r"..i.."_part"]
     if name and name ~= "" then
      local qty = q and q ~= "" and tostring(q) or "1"
      local item = {name = name, qty = qty}
      if part and (part == "out" or part == "output" or part == "result") then
         table.insert(outputs, item)
      else
        table.insert(inputs, item)
      end
     end
     end
  end
  return {inputs = inputs, outputs = outputs}
end


local function mergeRecipes(recipeTable1, recipeTable2)
    return {
  local inputs = {}
        inputs = inputs,
  for _,v in ipairs(recipeTable1.inputs or {}) do table.insert(inputs, v) end
        outputs = outputs,
  for _,v in ipairs(recipeTable2.inputs or {}) do table.insert(inputs, v) end
        action = action or ""
  local outputs = {}
    }
  for _,v in ipairs(recipeTable1.outputs or {}) do table.insert(outputs, v) end
  for _,v in ipairs(recipeTable2.outputs or {}) do table.insert(outputs, v) end
  return {inputs = inputs, outputs = outputs}
end
end


local function buildRecipeHtml(rec)
local function buildRecipeHtml(rec)
  if not rec then return nil end
    if not rec or (#rec.inputs == 0 and #rec.outputs == 0) then
  local inputs = rec.inputs or {}
        return nil
  local outputs = rec.outputs or {}
    end
  if #inputs == 0 and #outputs == 0 then return nil end
 
  local container = html.create("div")
    local container = html.create("div"):addClass("chem-recipe-block")
  container:addClass("chem-recipe")
 
  local inGroup = container:tag("div")
    local heading = html.create("div")
  inGroup:addClass("recipe-group recipe-inputs")
        :addClass("chem-heading")
  inGroup:tag("b"):wikitext("Ингредиенты")
        :attr("data-kind", "recipe")
  for _,it in ipairs(inputs) do
    heading:tag("span"):wikitext("Рецепт")
     local row = inGroup:tag("div")
    container:node(heading)
    row:addClass("recipe-item")
 
     row:tag("span"):addClass("name"):wikitext(mw.text.escape(it.name))
     local collapsible = html.create("div")
    row:tag("span"):addClass("qty"):wikitext("×"..mw.text.escape(tostring(it.qty)))
        :addClass("collapsible collapsed")
  end
        :attr("data-kind", "recipe")
  if #outputs > 0 then
 
    local outGroup = container:tag("div")
     local inner = html.create("div"):addClass("chem-recipe")
    outGroup:addClass("recipe-group recipe-outputs")
 
     outGroup:tag("b"):wikitext("Результат")
    local inputsDiv = html.create("div"):addClass("recipe-inputs")
     for _,it in ipairs(outputs) do
    for _, it in ipairs(rec.inputs) do
      local row = outGroup:tag("div")
        inputsDiv:tag("div")
      row:addClass("recipe-item")
            :addClass("recipe-item")
      row:tag("span"):addClass("name"):wikitext(mw.text.escape(it.name))
            :wikitext(it.name .. " [" .. it.qty .. "]")
      row:tag("span"):addClass("qty"):wikitext("×"..mw.text.escape(tostring(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
     end
  end
    inner:node(outputsDiv)
  return tostring(container)
 
    collapsible:node(inner)
    container:node(collapsible)
    return container
end
end


function p.card(frame)
local function buildEffectsHtml(effects)
  local args = getArgs(frame)
    if not effects or effects == "" or effects == "Нет" then
  local name = args.name or args.title or "—"
        return nil
  local formula = args.formula or args.formulae or ""
    end
  local color = args.color or args.colour or ""
 
  local border = args.border or args.bordercolor or ""
    local container = html.create("div"):addClass("chem-effects-block")
  local bg = args.bg or args.bgcolor or ""
 
  local recipeStr = args.recipe or args.rezept or ""
    local heading = html.create("div")
  local parsedStr = parseRecipeString(recipeStr)
        :addClass("chem-heading")
  local parsedN = parseRecipeN(args)
        :attr("data-kind", "effects")
  local recipeMerged = mergeRecipes(parsedStr, parsedN)
    heading:tag("span"):wikitext("Эффекты")
  local effects = args.effects or args.effect or ""
    container:node(heading)
  local desc = args.description or args.desc or ""
 
  local container = html.create("div")
    local collapsible = html.create("div")
  container:addClass("chem-card")
        :addClass("collapsible collapsed")
  local styleParts = {}
        :attr("data-kind", "effects")
  if color and color ~= "" then table.insert(styleParts, "--card-accent:"..color) end
 
  if border and border ~= "" then table.insert(styleParts, "--card-border:"..border) end
     local inner = html.create("div"):addClass("chem-effects")
  if bg and bg ~= "" then table.insert(styleParts, "--card-bg:"..bg) end
  if #styleParts > 0 then container:attr("style", table.concat(styleParts, ";")) end
  local header = container:tag("div")
  header:addClass("chem-header")
  header:tag("h3"):wikitext(mw.text.escape(name))
  if formula and formula ~= "" then header:tag("span"):addClass("formula"):wikitext(mw.text.escape(formula)) end
  local recipeHtml = buildRecipeHtml(recipeMerged)
  if recipeHtml then container:tag("div"):addClass("chem-section"):wikitext("<b>Рецепт</b>") container:wikitext(recipeHtml) end
  if effects and effects ~= "" then
     local effHtml = html.create("div")
    effHtml:addClass("chem-section")
    effHtml:tag("b"):wikitext("Эффекты")
     local ul = html.create("ul")
     local ul = html.create("ul")
     ul:addClass("chem-effects")
 
     local seen = {}
     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 ~= "" then ul:tag("li"):wikitext(effect) end
        if effect ~= "" and not seen[effect] then
            seen[effect] = true
            ul:tag("li"):wikitext(effect)
        end
     end
     end
     effHtml:node(ul)
 
     container:node(effHtml)
     inner:node(ul)
  end
    collapsible:node(inner)
  if desc and desc ~= "" then container:tag("div"):addClass("chem-desc"):wikitext(desc) end
     container:node(collapsible)
  return tostring(container)
    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
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