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

Материал из Space Stories Wiki
Нет описания правки
Метка: отменено
Нет описания правки
Метка: отменено
Строка 1: Строка 1:
local p = {}
local p = {}
local mw = mw
local html = mw.html
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 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
    name = l
qty = "1"
    qty = "1"
end
  end
return {name = mw.text.trim(name), qty = qty or "1"}
  name = mw.text.trim(name)
  return {name = name, qty = qty or "1"}
end
end


local function parseRecipe(recipeStr)
local function parseRecipeString(recipeStr)
if not recipeStr or recipeStr == "" then return {inputs={}, outputs={}, action=""} end
  if not recipeStr or recipeStr == "" then return {inputs={}, outputs={}, action=""} end
local lines = splitLines(recipeStr)
  local lines = splitLines(recipeStr)
local splitIdx, action
  local splitIdx, action
for i, line in ipairs(lines) do
  for i,line in ipairs(lines) do
if mw.ustring.find(mw.ustring.lower(line), "смеш") then
    if mw.ustring.lower(line):find("смеш") then
splitIdx = i
      splitIdx = i
action = line
      action = line
break
      break
end
    end
end
  end
local inputs, outputs = {}, {}
  local inputs, outputs = {}, {}
if splitIdx then
  if splitIdx then
for i = 1, splitIdx - 1 do table.insert(inputs, parseLineToItem(lines[i])) end
    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
    for i=splitIdx+1,#lines do table.insert(outputs, parseLineToItem(lines[i])) end
else
  else
for i = 1, #lines do table.insert(inputs, parseLineToItem(lines[i])) end
    for i=1,#lines do table.insert(inputs, parseLineToItem(lines[i])) end
end
  end
return {inputs = inputs, outputs = outputs, action = action}
  return {inputs=inputs, outputs=outputs, action=action}
end
end


local function buildRecipeHtml(parsed)
local function buildRecipeHtml(parsed)
if #parsed.inputs == 0 and #parsed.outputs == 0 and (not parsed.action or parsed.action=="") then return nil end
  if #parsed.inputs == 0 and #parsed.outputs == 0 and (not parsed.action or parsed.action=="") then return nil end
local container = html.create("div"):addClass("mw-collapsible mw-collapsed"):attr("style","width:100%; overflow:auto; border-bottom: 1px solid #303038;")
  local container = html.create("div"):addClass("mw-collapsible mw-collapsed"):attr("style","width:100%; overflow:auto; border-bottom: 1px solid #303038;")
container:tag("div"):attr("style","font-weight:bold;line-height:1.6; padding:1px"):wikitext("Рецепты")
  container:tag("div"):attr("style","font-weight:bold;line-height:1.6; padding:1px"):wikitext("Рецепты")
local content = html.create("div"):addClass("mw-collapsible-content")
  local content = html.create("div"):addClass("mw-collapsible-content"):attr("style","display:flex; justify-content:space-between; padding:0.3em 0; flex-wrap:wrap")
for _, it in ipairs(parsed.inputs) do
 
content:tag("div"):wikitext(it.name.." ["..it.qty.."]")
  local inputsDiv = html.create("div"):attr("style","flex:1; min-width:8em; margin-right:1em")
end
  for _, it in ipairs(parsed.inputs) do
if parsed.action and parsed.action~="" then
    inputsDiv:tag("div"):wikitext(it.name.." ["..it.qty.."]")
content:tag("div"):wikitext(parsed.action)
  end
end
 
for _, it in ipairs(parsed.outputs) do
  local actionDiv = html.create("div"):attr("style","flex:0 0 auto; text-align:center; min-width:5em; margin:0 0.5em")
content:tag("div"):wikitext(it.name.." ["..it.qty.."]")
  if parsed.action and parsed.action~="" then
end
    actionDiv:wikitext(parsed.action)
container:node(content)
  end
return container
 
  local outputsDiv = html.create("div"):attr("style","flex:1; min-width:8em; margin-left:1em")
  for _, it in ipairs(parsed.outputs) do
    outputsDiv:tag("div"):wikitext(it.name.." ["..it.qty.."]")
  end
 
  content:node(inputsDiv)
  content:node(actionDiv)
  content:node(outputsDiv)
  container:node(content)
  return container
end
end


local function buildEffectsHtml(effects)
local function buildEffectsHtml(effects)
if not effects or effects=="" or effects=="Нет" then return nil end
  if not effects or effects=="" or effects=="Нет" then return nil end
local container = html.create("div"):addClass("mw-collapsible mw-collapsed"):attr("style","width:100%; overflow:auto; border-bottom: 1px solid #303038;")
  local container = html.create("div"):addClass("mw-collapsible mw-collapsed"):attr("style","width:100%; overflow:auto; border-bottom: 1px solid #303038; margin-top:0.3em")
container:tag("div"):attr("style","font-weight:bold;line-height:1.6; padding:1px"):wikitext("Эффекты")
  container:tag("div"):attr("style","font-weight:bold;line-height:1.6; padding:1px"):wikitext("Эффекты")
local content = html.create("div"):addClass("mw-collapsible-content"):attr("style","padding:0 0.5em")
  local content = html.create("div"):addClass("mw-collapsible-content"):attr("style","padding:0.3em")
for effect in mw.text.gsplit(effects, "[;\n]") do
  local ul = html.create("ul")
effect = mw.text.trim(effect)
  for effect in mw.text.gsplit(effects, "[;\n]") do
if effect ~= "" then content:tag("div"):wikitext(effect) end
    effect = mw.text.trim(effect)
end
    if effect~="" then ul:tag("li"):wikitext(effect) end
container:node(content)
  end
return container
  content:node(ul)
  container:node(content)
  return container
end
end


function p.card(frame)
function p.card(frame)
local args = frame.args or {}
  local args = getArgs(frame)
local name = args.name or "—"
  local name = args.name or args.title or "—"
local color = args.color or "#ffffff"
  local color = args.color or args.colour or "#000000"
local border = args.border or "#000000"
  local border = args.border or args.bordercolor or "#000000"
local bg = args.bg or "#101010"
  local bg = args.bg or args.bgcolor or "#101010"
local recipeStr = args.recipe or ""
  local recipeStr = args.recipe or args.rezept or ""
local effects = args.effects or ""
  local effects = args.effects or args.effect or ""
local desc = args.desc or ""
  local desc = args.description or args.desc or ""
 
local parsed = parseRecipe(recipeStr)


local card = html.create("div"):addClass("quickbox"):attr("style","width:25em;")
  local parsed = parseRecipeString(recipeStr)
local outer = html.create("div"):attr("style","border:2px solid black; border-radius:0 0 21px 21px")
local middle = html.create("div"):attr("style","border:3px solid "..border.."; border-radius:0 0 20px 20px")
local inner = html.create("div"):attr("style","border:2px solid black; border-radius:0 0 18px 18px")


inner:tag("div"):addClass("quickboxhead"):attr("style","font-weight:500; background-color:"..color.."; color:#000000"):wikitext("''' "..name.."'''")
  local container = html.create("div"):addClass("chem-card"):attr("style",
    "border:2px solid "..border.."; border-radius:10px; background-color:"..bg.."; margin:0.5em; padding:0.3em;"
  )


if parsed then
  container:tag("h3"):attr("style","margin:0; padding:0.3em; color:"..color..";"):wikitext(name)
local rhtml = buildRecipeHtml(parsed)
if rhtml then inner:node(rhtml) end
end


local ehtml = buildEffectsHtml(effects)
  if parsed then
if ehtml then inner:node(ehtml) end
    local recBlock = buildRecipeHtml(parsed)
    if recBlock then container:node(recBlock) end
  end


if desc and desc~="" then
  local effectsBlock = buildEffectsHtml(effects)
inner:tag("div"):attr("style","padding:0 0.5em"):wikitext(desc)
  if effectsBlock then container:node(effectsBlock) end
end


middle:node(inner)
  if desc and desc~="" then
outer:node(middle)
    container:tag("div"):attr("style","margin-top:0.3em; padding:0 0.3em;"):wikitext(desc)
card:node(outer)
  end


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


return p
return p

Версия от 06:14, 11 ноября 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={}, 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, 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}
end

local function buildRecipeHtml(parsed)
  if #parsed.inputs == 0 and #parsed.outputs == 0 and (not parsed.action or parsed.action=="") then return nil end
  local container = html.create("div"):addClass("mw-collapsible mw-collapsed"):attr("style","width:100%; overflow:auto; border-bottom: 1px solid #303038;")
  container:tag("div"):attr("style","font-weight:bold;line-height:1.6; padding:1px"):wikitext("Рецепты")
  local content = html.create("div"):addClass("mw-collapsible-content"):attr("style","display:flex; justify-content:space-between; padding:0.3em 0; flex-wrap:wrap")

  local inputsDiv = html.create("div"):attr("style","flex:1; min-width:8em; margin-right:1em")
  for _, it in ipairs(parsed.inputs) do
    inputsDiv:tag("div"):wikitext(it.name.." ["..it.qty.."]")
  end

  local actionDiv = html.create("div"):attr("style","flex:0 0 auto; text-align:center; min-width:5em; margin:0 0.5em")
  if parsed.action and parsed.action~="" then
    actionDiv:wikitext(parsed.action)
  end

  local outputsDiv = html.create("div"):attr("style","flex:1; min-width:8em; margin-left:1em")
  for _, it in ipairs(parsed.outputs) do
    outputsDiv:tag("div"):wikitext(it.name.." ["..it.qty.."]")
  end

  content:node(inputsDiv)
  content:node(actionDiv)
  content:node(outputsDiv)
  container:node(content)
  return container
end

local function buildEffectsHtml(effects)
  if not effects or effects=="" or effects=="Нет" then return nil end
  local container = html.create("div"):addClass("mw-collapsible mw-collapsed"):attr("style","width:100%; overflow:auto; border-bottom: 1px solid #303038; margin-top:0.3em")
  container:tag("div"):attr("style","font-weight:bold;line-height:1.6; padding:1px"):wikitext("Эффекты")
  local content = html.create("div"):addClass("mw-collapsible-content"):attr("style","padding:0.3em")
  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
  content:node(ul)
  container:node(content)
  return container
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 "#000000"
  local border = args.border or args.bordercolor or "#000000"
  local bg = args.bg or args.bgcolor or "#101010"
  local recipeStr = args.recipe or args.rezept or ""
  local effects = args.effects or args.effect or ""
  local desc = args.description or args.desc or ""

  local parsed = parseRecipeString(recipeStr)

  local container = html.create("div"):addClass("chem-card"):attr("style",
    "border:2px solid "..border.."; border-radius:10px; background-color:"..bg.."; margin:0.5em; padding:0.3em;"
  )

  container:tag("h3"):attr("style","margin:0; padding:0.3em; color:"..color..";"):wikitext(name)

  if parsed then
    local recBlock = buildRecipeHtml(parsed)
    if recBlock then container:node(recBlock) end
  end

  local effectsBlock = buildEffectsHtml(effects)
  if effectsBlock then container:node(effectsBlock) end

  if desc and desc~="" then
    container:tag("div"):attr("style","margin-top:0.3em; padding:0 0.3em;"):wikitext(desc)
  end

  return tostring(container)
end

return p