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

Материал из Space Stories Wiki
Нет описания правки
Нет описания правки
Строка 91: Строка 91:
local function buildRecipeHtml(rec)
local function buildRecipeHtml(rec)
   if not rec then return nil end
   if not rec then return nil end
  local block = html.create("div"):addClass("chem-recipe-block")
  local header = html.create("div"):addClass("chem-header chem-toggle-label"):wikitext("Рецепт")
  block:node(header)
  local wrapper = html.create("div"):addClass("collapsible collapsed")
   local container = html.create("div"):addClass("chem-recipe")
   local container = html.create("div"):addClass("chem-recipe")
   local inputsDiv = html.create("div"):addClass("recipe-inputs")
   local inputsDiv = html.create("div"):addClass("recipe-inputs")
Строка 98: Строка 102:
   container:node(inputsDiv)
   container:node(inputsDiv)
   if rec.action and #rec.inputs > 0 and #rec.outputs > 0 then
   if rec.action and #rec.inputs > 0 and #rec.outputs > 0 then
     container:tag("div"):addClass("recipe-action"):wikitext(rec.action)
     local actionLine = tostring(rec.action or "")
    local fileMatch = mw.ustring.match(actionLine, "%[%[%s*File:([^%]|]+)")
    if fileMatch then
      local fileWikitext = "[[File:" .. mw.text.trim(fileMatch) .. "]]"
      container:tag("div"):addClass("recipe-sprite"):wikitext(fileWikitext)
      actionLine = mw.ustring.gsub(actionLine, "%[%[%s*File:([^%]]+)%]%]", "")
      actionLine = mw.text.trim(actionLine)
    end
    container:tag("div"):addClass("recipe-action"):wikitext(actionLine ~= "" and actionLine or "смешайте")
   end
   end
   local outputsDiv = html.create("div"):addClass("recipe-outputs")
   local outputsDiv = html.create("div"):addClass("recipe-outputs")
Строка 105: Строка 117:
   end
   end
   container:node(outputsDiv)
   container:node(outputsDiv)
   return tostring(container)
  wrapper:node(container)
  block:node(wrapper)
   return tostring(block)
end
end


Строка 129: Строка 143:
   container:wikitext(buildRecipeHtml(recipeMerged))
   container:wikitext(buildRecipeHtml(recipeMerged))
   if effects and effects ~= "" and effects ~= "Нет" then
   if effects and effects ~= "" and effects ~= "Нет" then
    local effBlock = html.create("div"):addClass("chem-effects-block")
    local effHeader = html.create("div"):addClass("chem-header chem-toggle-label"):wikitext("Эффекты")
    effBlock:node(effHeader)
    local effWrapper = html.create("div"):addClass("collapsible collapsed")
     local effDiv = html.create("div"):addClass("chem-effects")
     local effDiv = html.create("div"):addClass("chem-effects")
     effDiv:tag("b"):wikitext("Эффекты:")
     effDiv:tag("b"):wikitext("Эффекты:")
Строка 137: Строка 155:
     end
     end
     effDiv:node(ul)
     effDiv:node(ul)
     container:node(effDiv)
    effWrapper:node(effDiv)
    effBlock:node(effWrapper)
     container:node(effBlock)
   end
   end
   if desc and desc ~= "" then
   if desc and desc ~= "" then

Версия от 16:07, 31 октября 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
  name = mw.text.trim(name)
  qty = qty or "1"
  qty = mw.text.trim(qty)
  qty = mw.ustring.gsub(qty, ",", ".")
  return {name = name, qty = qty}
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 = {}
  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, action = action or ""}
end

local function parseRecipeN(args)
  local inputs, outputs, action = {}, {}, ""
  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"
      qty = mw.ustring.gsub(mw.text.trim(qty), ",", ".")
      local item = {name = mw.text.trim(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
  local act = args["recipe_action"] or args["action"]
  if act and act ~= "" then action = act end
  return {inputs = inputs, outputs = outputs, action = action}
end

local function mergeRecipes(r1, r2)
  local inputs, outputs, action = {}, {}, r1.action or r2.action or ""
  for _, v in ipairs(r1.inputs or {}) do table.insert(inputs, v) end
  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

local function buildRecipeHtml(rec)
  if not rec then return nil end
  local block = html.create("div"):addClass("chem-recipe-block")
  local header = html.create("div"):addClass("chem-header chem-toggle-label"):wikitext("Рецепт")
  block:node(header)
  local wrapper = html.create("div"):addClass("collapsible collapsed")
  local container = html.create("div"):addClass("chem-recipe")
  local inputsDiv = html.create("div"):addClass("recipe-inputs")
  for _, it in ipairs(rec.inputs or {}) do
    inputsDiv:tag("div"):addClass("recipe-item"):wikitext(it.name .. " [" .. tostring(it.qty) .. "]")
  end
  container:node(inputsDiv)
  if rec.action and #rec.inputs > 0 and #rec.outputs > 0 then
    local actionLine = tostring(rec.action or "")
    local fileMatch = mw.ustring.match(actionLine, "%[%[%s*File:([^%]|]+)")
    if fileMatch then
      local fileWikitext = "[[File:" .. mw.text.trim(fileMatch) .. "]]"
      container:tag("div"):addClass("recipe-sprite"):wikitext(fileWikitext)
      actionLine = mw.ustring.gsub(actionLine, "%[%[%s*File:([^%]]+)%]%]", "")
      actionLine = mw.text.trim(actionLine)
    end
    container:tag("div"):addClass("recipe-action"):wikitext(actionLine ~= "" and actionLine or "смешайте")
  end
  local outputsDiv = html.create("div"):addClass("recipe-outputs")
  for _, it in ipairs(rec.outputs or {}) do
    outputsDiv:tag("div"):addClass("recipe-item"):wikitext(it.name .. " [" .. tostring(it.qty) .. "]")
  end
  container:node(outputsDiv)
  wrapper:node(container)
  block:node(wrapper)
  return tostring(block)
end

function p.card(frame)
  local args = getArgs(frame)
  local name = args.name or args.title or "—"
  local color = args.color or args.colour or ""
  local border = args.border or args.bordercolor or ""
  local bg = args.bg or args.bgcolor or ""
  local recipeStr = args.recipe or args.rezept or ""
  local parsedStr = parseRecipeString(recipeStr)
  local parsedN = parseRecipeN(args)
  local recipeMerged = mergeRecipes(parsedStr, parsedN)
  local effects = args.effects or args.effect or ""
  local desc = args.description or args.desc or ""
  local container = html.create("div"):addClass("chem-card")
  local styleParts = {}
  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:wikitext(buildRecipeHtml(recipeMerged))
  if effects and effects ~= "" and effects ~= "Нет" then
    local effBlock = html.create("div"):addClass("chem-effects-block")
    local effHeader = html.create("div"):addClass("chem-header chem-toggle-label"):wikitext("Эффекты")
    effBlock:node(effHeader)
    local effWrapper = html.create("div"):addClass("collapsible collapsed")
    local effDiv = html.create("div"):addClass("chem-effects")
    effDiv:tag("b"):wikitext("Эффекты:")
    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
    effDiv:node(ul)
    effWrapper:node(effDiv)
    effBlock:node(effWrapper)
    container:node(effBlock)
  end
  if desc and desc ~= "" then
    container:tag("div"):addClass("chem-desc"):wikitext(desc)
  end
  return tostring(container)
end

return p