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

Материал из Space Stories Wiki
(Новая страница: «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 buildRecipeTable(args) local tbl = html.create('table') tbl:addClass('chem-recipe') local has = false for i = 1, 12 do local name = args['recipe'..i] or args['r'..i] local q = a...»)
 
Нет описания правки
Строка 5: Строка 5:
local function getArgs(frame)
local function getArgs(frame)
   local args = {}
   local args = {}
   if type(frame) == 'table' and frame.args then
   if type(frame) == "table" and frame.args then
     args = frame.args
     args = frame.args
   else
   else
Строка 13: Строка 13:
end
end


local function buildRecipeTable(args)
local function splitLines(s)
   local tbl = html.create('table')
   local t = {}
   tbl:addClass('chem-recipe')
   if not s then return t end
  local has = false
   for line in mw.text.gsplit(s, "\n") do
   for i = 1, 12 do
     line = mw.text.trim(line)
     local name = args['recipe'..i] or args['r'..i]
    if line ~= "" then table.insert(t, line) end
    local q = args['recipe'..i..'_q'] or args['r'..i..'_q'] or args['q'..i]
  end
    if name and name ~= '' then
  return t
      has = true
end
      local tr = html.create('tr')
 
      tr:tag('td'):wikitext(name)
local function parseLineToItem(l)
      tr:tag('td'):wikitext(q and q ~= '' and tostring(q) or '1')
  local name, qty = mw.ustring.match(l, "^(.*)%s*%[%s*(%d+)%s*%]%s*$")
       tbl:node(tr)
  if not name then
    name = l
    qty = "1"
  end
  name = mw.text.trim(name)
  return {name = name, qty = qty or "1"}
end
 
local function parseRecipeString(recipeStr)
  if not recipeStr or recipeStr == "" then return {inputs = {}, outputs = {}} end
  local lines = splitLines(recipeStr)
  local splitIdx
  for i,line in ipairs(lines) do
    if mw.ustring.lower(line):find("смеш") then
      splitIdx = i
       break
     end
     end
   end
   end
   return has and tostring(tbl) or nil
   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 buildEffectsList(effects)
local function parseRecipeN(args)
   if not effects or effects == '' then return nil end
   local inputs = {}
   local ul = html.create('ul')
  local outputs = {}
  ul:addClass('chem-effects')
   for i = 1, 24 do
  for effect in mw.text.gsplit(effects, '[;\n]') do
    local name = args["recipe"..i] or args["r"..i]
     effect = mw.text.trim(effect)
    local q = args["recipe"..i.."_q"] or args["r"..i.."_q"] or args["q"..i]
     if effect ~= '' then
     local part = args["recipe"..i.."_part"] or args["r"..i.."_part"]
       ul:tag('li'):wikitext(effect)
     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
   end
   return tostring(ul)
   return {inputs = inputs, outputs = outputs}
end
end


function p.card(frame)
local function mergeRecipes(recipeTable1, recipeTable2)
   local args = getArgs(frame)
   local inputs = {}
   local name = args.name or args.title or '—'
   for _,v in ipairs(recipeTable1.inputs or {}) do table.insert(inputs, v) end
  local color = args.color or args.colour or '#cfe8ff'
   for _,v in ipairs(recipeTable2.inputs or {}) do table.insert(inputs, v) end
  local recipeHtml = buildRecipeTable(args)
   local outputs = {}
   local effectsHtml = buildEffectsList(args.effects or args.effect)
   for _,v in ipairs(recipeTable1.outputs or {}) do table.insert(outputs, v) end
   local desc = args.description or args.desc or ''
   for _,v in ipairs(recipeTable2.outputs or {}) do table.insert(outputs, v) end
 
   return {inputs = inputs, outputs = outputs}
  local container = html.create('div')
end
   container:addClass('chem-card')
  container:css('border','1px solid #888')
   container:css('border-radius','8px')
  container:css('padding','8px')
   container:css('background','linear-gradient(90deg, white 0%, '..color..'33 100%)')


   local header = container:tag('div')
local function buildRecipeHtml(rec)
   header:addClass('chem-header')
  if not rec then return nil end
   header:css('display','flex')
  local inputs = rec.inputs or {}
   header:css('justify-content','space-between')
   local outputs = rec.outputs or {}
   header:css('align-items','center')
  if #inputs == 0 and #outputs == 0 then return nil end
 
  local container = html.create("div")
  header:tag('h3'):wikitext(name)
   container:addClass("chem-recipe")
  if args.formula and args.formula ~= '' then
   local inGroup = container:tag("div")
     header:tag('span'):wikitext(args.formula)
   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
   end
 
   if #outputs > 0 then
   if recipeHtml then
     local outGroup = container:tag("div")
     container:tag('div'):addClass('chem-section'):wikitext('<b>Рецепт</b>')
    outGroup:addClass("recipe-group recipe-outputs")
     container:wikitext(recipeHtml)
    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
  return tostring(container)
end


   if effectsHtml then
function p.card(frame)
    container:tag('div'):addClass('chem-section'):wikitext('<b>Эффекты</b>')
  local args = getArgs(frame)
     container:wikitext(effectsHtml)
  local name = args.name or args.title or "—"
  local formula = args.formula or args.formulae 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")
  container:addClass("chem-card")
  local styleParts = {}
   if color and color ~= "" then table.insert(styleParts, "--card-accent:"..color) end
  if border and border ~= "" then table.insert(styleParts, "--card-border:"..border) end
  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")
    ul:addClass("chem-effects")
    for effect in mw.text.gsplit(effects, "[;\n]") do
      effect = mw.text.trim(effect)
      if effect ~= "" then ul:tag("li"):wikitext(effect) end
    end
    effHtml:node(ul)
     container:node(effHtml)
   end
   end
 
   if desc and desc ~= "" then container:tag("div"):addClass("chem-desc"):wikitext(desc) end
   if desc and desc ~= '' then
    container:tag('div'):addClass('chem-desc'):wikitext(desc)
  end
 
   return tostring(container)
   return tostring(container)
end
end


return p
return p

Версия от 12:22, 18 октября 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)
  return {name = name, qty = qty or "1"}
end

local function parseRecipeString(recipeStr)
  if not recipeStr or recipeStr == "" then return {inputs = {}, outputs = {}} end
  local lines = splitLines(recipeStr)
  local splitIdx
  for i,line in ipairs(lines) do
    if mw.ustring.lower(line):find("смеш") then
      splitIdx = i
      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}
end

local function parseRecipeN(args)
  local inputs = {}
  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
  return {inputs = inputs, outputs = outputs}
end

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

local function buildRecipeHtml(rec)
  if not rec then return nil end
  local inputs = rec.inputs or {}
  local outputs = rec.outputs or {}
  if #inputs == 0 and #outputs == 0 then return nil end
  local container = html.create("div")
  container:addClass("chem-recipe")
  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
  return tostring(container)
end

function p.card(frame)
  local args = getArgs(frame)
  local name = args.name or args.title or "—"
  local formula = args.formula or args.formulae 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")
  container:addClass("chem-card")
  local styleParts = {}
  if color and color ~= "" then table.insert(styleParts, "--card-accent:"..color) end
  if border and border ~= "" then table.insert(styleParts, "--card-border:"..border) end
  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")
    ul:addClass("chem-effects")
    for effect in mw.text.gsplit(effects, "[;\n]") do
      effect = mw.text.trim(effect)
      if effect ~= "" then ul:tag("li"):wikitext(effect) end
    end
    effHtml:node(ul)
    container:node(effHtml)
  end
  if desc and desc ~= "" then container:tag("div"):addClass("chem-desc"):wikitext(desc) end
  return tostring(container)
end

return p