Модуль:ChemCard

Материал из Space Stories Wiki
Версия от 12:08, 18 октября 2025; Dantes (обсуждение | вклад) (Новая страница: «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...»)
(разн.) ← Предыдущая версия | Текущая версия (разн.) | Следующая версия → (разн.)

Для документации этого модуля может быть создана страница Модуль: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 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 = args['recipe'..i..'_q'] or args['r'..i..'_q'] or args['q'..i]
    if name and name ~= '' then
      has = true
      local tr = html.create('tr')
      tr:tag('td'):wikitext(name)
      tr:tag('td'):wikitext(q and q ~= '' and tostring(q) or '1')
      tbl:node(tr)
    end
  end
  return has and tostring(tbl) or nil
end

local function buildEffectsList(effects)
  if not effects or effects == '' then return nil end
  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
  return tostring(ul)
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 '#cfe8ff'
  local recipeHtml = buildRecipeTable(args)
  local effectsHtml = buildEffectsList(args.effects or args.effect)
  local desc = args.description or args.desc or ''

  local container = html.create('div')
  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')
  header:addClass('chem-header')
  header:css('display','flex')
  header:css('justify-content','space-between')
  header:css('align-items','center')

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

  if recipeHtml then
    container:tag('div'):addClass('chem-section'):wikitext('<b>Рецепт</b>')
    container:wikitext(recipeHtml)
  end

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

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

  return tostring(container)
end

return p