Модуль: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...»)
 
Нет описания правки
 
(не показано 45 промежуточных версий этого же участника)
Строка 4: Строка 4:


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
    args = mw.getCurrentFrame():getParent().args or {}
        args = mw.getCurrentFrame():getParent().args or {}
  end
    end
  return args
    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
end


local function buildRecipeTable(args)
local function buildRecipeHtml(rec)
  local tbl = html.create('table')
    local container = html.create("div"):addClass("chem-recipe-block")
  tbl:addClass('chem-recipe')
    local heading = html.create("div"):addClass("chem-heading"):attr("data-kind","recipe")
  local has = false
    heading:tag("span"):wikitext("Рецепт")
  for i = 1, 12 do
    container:node(heading)
     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 collapsible = html.create("div"):addClass("collapsible collapsed"):attr("data-kind","recipe")
     if name and name ~= '' then
     local inner = html.create("div"):addClass("chem-recipe")
      has = true
 
      local tr = html.create('tr')
     local inputsDiv = html.create("div"):addClass("recipe-inputs")
      tr:tag('td'):wikitext(name)
    for _,it in ipairs(rec.inputs) do
      tr:tag('td'):wikitext(q and q ~= '' and tostring(q) or '1')
        inputsDiv:tag("div"):addClass("recipe-item"):wikitext(it.name.." ["..it.qty.."]")
      tbl:node(tr)
    end
    inner:node(inputsDiv)
 
     if rec.action and #rec.inputs>0 and #rec.outputs>0 then
        local actionDiv = html.create("div"):addClass("recipe-action")
        actionDiv:wikitext(rec.action)
        inner:node(actionDiv)
    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 has and tostring(tbl) or nil
 
    collapsible:node(inner)
    container:node(collapsible)
    return container
end
end


local function buildEffectsList(effects)
local function buildEffectsHtml(effects)
  if not effects or effects == '' then return nil end
    if not effects or effects=="" or effects=="Нет" then return nil end
  local ul = html.create('ul')
    local container = html.create("div"):addClass("chem-effects-block")
  ul:addClass('chem-effects')
    local heading = html.create("div"):addClass("chem-heading"):attr("data-kind","effects")
  for effect in mw.text.gsplit(effects, '[;\n]') do
    heading:tag("span"):wikitext("Эффекты")
    effect = mw.text.trim(effect)
    container:node(heading)
    if effect ~= '' then
   
      ul:tag('li'):wikitext(effect)
    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 seenEffects = {}
    for effect in mw.text.gsplit(effects, "[;\n]") do
        effect = mw.text.trim(effect)
        if effect~="" and not seenEffects[effect] then
            seenEffects[effect] = true
            ul:tag("li"):wikitext(effect)
        end
     end
     end
  end
    inner:node(ul)
  return tostring(ul)
    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 '#cfe8ff'
    local color = args.color or "#8cf"
  local recipeHtml = buildRecipeTable(args)
    local border = args.border or "#444"
  local effectsHtml = buildEffectsList(args.effects or args.effect)
    local recipeStr = args.recipe or ""
  local desc = args.description or args.desc or ''
    local effects = args.effects or ""
    local desc = args.desc or ""


  local container = html.create('div')
    local parsed = parseRecipeString(recipeStr)
  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 container = html.create("div"):addClass("chem-card")
  header:addClass('chem-header')
    container:attr("style", "border-color:"..border.."; --card-accent:"..color)
  header:css('display','flex')
  header:css('justify-content','space-between')
  header:css('align-items','center')


  header:tag('h3'):wikitext(name)
    container:tag("h3"):wikitext(name)
  if args.formula and args.formula ~= '' then
    header:tag('span'):wikitext(args.formula)
  end


  if recipeHtml then
    if #parsed.inputs > 0 or #parsed.outputs > 0 or (parsed.action and parsed.action~="") then
    container:tag('div'):addClass('chem-section'):wikitext('<b>Рецепт</b>')
        container:node(buildRecipeHtml(parsed))
     container:wikitext(recipeHtml)
     end
  end


  if effectsHtml then
     local effectsBlock = buildEffectsHtml(effects)
     container:tag('div'):addClass('chem-section'):wikitext('<b>Эффекты</b>')
     if effectsBlock then
     container:wikitext(effectsHtml)
        container:node(effectsBlock)
  end
    end


  if desc and desc ~= '' then
    if desc and desc~="" then
    container:tag('div'):addClass('chem-desc'):wikitext(desc)
        container:tag("div"):addClass("chem-desc"):wikitext(desc)
  end
    end


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


return p
return p

Текущая версия от 16:56, 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(rec)
    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 and #rec.inputs>0 and #rec.outputs>0 then
        local actionDiv = html.create("div"):addClass("recipe-action")
        actionDiv:wikitext(rec.action)
        inner:node(actionDiv)
    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 seenEffects = {}
    for effect in mw.text.gsplit(effects, "[;\n]") do
        effect = mw.text.trim(effect)
        if effect~="" and not seenEffects[effect] then
            seenEffects[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 recipeStr = args.recipe or ""
    local effects = args.effects or ""
    local desc = args.desc or ""

    local parsed = parseRecipeString(recipeStr)

    local container = html.create("div"):addClass("chem-card")
    container:attr("style", "border-color:"..border.."; --card-accent:"..color)

    container:tag("h3"):wikitext(name)

    if #parsed.inputs > 0 or #parsed.outputs > 0 or (parsed.action and parsed.action~="") then
        container:node(buildRecipeHtml(parsed))
    end

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

    if desc and desc~="" then
        container:tag("div"):addClass("chem-desc"):wikitext(desc)
    end

    return tostring(container)
end

return p