Dantes (обсуждение | вклад) (Отмена правки 23407, сделанной Dantes (обсуждение)) Метки: отмена отменено |
Dantes (обсуждение | вклад) Нет описания правки Метка: отменено |
||
| Строка 56: | Строка 56: | ||
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("chem-recipe") | local container = html.create("div"):addClass("chem-recipe") | ||
container:attr("style","display:flex; flex-wrap:wrap; align-items:flex-start; gap:0.5em; margin-bottom:0.5em;") | |||
if #parsed.inputs > 0 then | if #parsed.inputs > 0 then | ||
for _, it in ipairs(parsed.inputs) do | for _, it in ipairs(parsed.inputs) do | ||
local div = html.create("div"):attr("style","white-space:nowrap;") | |||
div:wikitext(it.name.." ["..it.qty.."]") | |||
container:node(div) | |||
end | end | ||
end | end | ||
if parsed.action and parsed.action~="" then | if parsed.action and parsed.action~="" then | ||
local actionDiv = html.create("div"):attr("style"," | local actionDiv = html.create("div") | ||
actionDiv:attr("style","white-space:nowrap; font-weight:bold; margin:0 0.3em;") | |||
actionDiv:wikitext(parsed.action) | actionDiv:wikitext(parsed.action) | ||
container:node(actionDiv) | container:node(actionDiv) | ||
end | end | ||
if #parsed.outputs > 0 then | if #parsed.outputs > 0 then | ||
for _, it in ipairs(parsed.outputs) do | for _, it in ipairs(parsed.outputs) do | ||
local div = html.create("div"):attr("style","white-space:nowrap;") | |||
div:wikitext(it.name.." ["..it.qty.."]") | |||
container:node(div) | |||
end | end | ||
end | end | ||
return container | return container | ||
end | end | ||
| Строка 103: | Строка 109: | ||
local container = html.create("div"):addClass("chem-card") | local container = html.create("div"):addClass("chem-card") | ||
:attr("style","border:2px solid "..border.."; border-radius:8px; background-color:"..bg.."; padding:0.5em;") | :attr("style","border:2px solid "..border.."; border-radius:8px; background-color:"..bg.."; padding:0.5em; margin:0.5em; box-sizing:border-box;") | ||
container:tag("h3"):attr("style","margin:0 0 0.5em 0; color:"..color.."; font-size:16px;"):wikitext(name) | container:tag("h3"):attr("style","margin:0 0 0.5em 0; color:"..color.."; font-size:16px;"):wikitext(name) | ||
Версия от 07:30, 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
return {name = mw.text.trim(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("chem-recipe")
container:attr("style","display:flex; flex-wrap:wrap; align-items:flex-start; gap:0.5em; margin-bottom:0.5em;")
if #parsed.inputs > 0 then
for _, it in ipairs(parsed.inputs) do
local div = html.create("div"):attr("style","white-space:nowrap;")
div:wikitext(it.name.." ["..it.qty.."]")
container:node(div)
end
end
if parsed.action and parsed.action~="" then
local actionDiv = html.create("div")
actionDiv:attr("style","white-space:nowrap; font-weight:bold; margin:0 0.3em;")
actionDiv:wikitext(parsed.action)
container:node(actionDiv)
end
if #parsed.outputs > 0 then
for _, it in ipairs(parsed.outputs) do
local div = html.create("div"):attr("style","white-space:nowrap;")
div:wikitext(it.name.." ["..it.qty.."]")
container:node(div)
end
end
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")
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
container:node(ul)
return container
end
function p.card(frame)
local args = getArgs(frame)
local name = args.name or "—"
local color = args.color or "#fff"
local border = args.border or "#555"
local bg = args.bg or "#101010"
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")
:attr("style","border:2px solid "..border.."; border-radius:8px; background-color:"..bg.."; padding:0.5em; margin:0.5em; box-sizing:border-box;")
container:tag("h3"):attr("style","margin:0 0 0.5em 0; color:"..color.."; font-size:16px;"):wikitext(name)
local recBlock = buildRecipeHtml(parsed)
if recBlock then container:node(recBlock) 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.5em; font-size:14px; color:#ccc;"):wikitext(desc)
end
return tostring(container)
end
return p