Dantes (обсуждение | вклад) (Отмена правки 23212, сделанной Dantes (обсуждение)) Метка: отмена |
Dantes (обсуждение | вклад) Нет описания правки Метка: отменено |
||
| Строка 1: | Строка 1: | ||
local p = {} | local p = {} | ||
local html = mw.html | local html = mw.html | ||
local function splitLines(s) | 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 | end | ||
local function parseLineToItem(l) | 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 | end | ||
local function | local function parseRecipe(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.find(mw.ustring.lower(line), "смеш") 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 buildRecipeHtml( | local function buildRecipeHtml(parsed, sprite) | ||
local container = html.create("div"):addClass("chem-recipe-block") | |||
container:tag("span"):addClass("chem-block-heading chem-heading"):attr("data-kind","recipe"):wikitext("Рецепт") | |||
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(parsed.inputs) do | |||
inputsDiv:tag("div"):addClass("recipe-item"):wikitext(it.name.." ["..it.qty.."]") | |||
end | |||
inner:node(inputsDiv) | |||
if parsed.action and #parsed.inputs > 0 and #parsed.outputs > 0 then | |||
local actionDiv = html.create("div"):addClass("recipe-action") | |||
if sprite and sprite ~= "" then | |||
actionDiv:tag("div"):addClass("action-sprite"):wikitext(sprite) | |||
end | |||
actionDiv:wikitext(parsed.action) | |||
inner:node(actionDiv) | |||
end | |||
local outputsDiv = html.create("div"):addClass("recipe-outputs") | |||
for _, it in ipairs(parsed.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 | end | ||
local function buildEffectsHtml(effects) | local function buildEffectsHtml(effects) | ||
if not effects or effects == "" or effects == "Нет" then return nil end | |||
local container = html.create("div"):addClass("chem-effects-block") | |||
container:tag("span"):addClass("chem-block-heading chem-heading"):attr("data-kind","effects"):wikitext("Эффекты") | |||
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") | |||
for effect in mw.text.gsplit(effects, "[;\n]") do | |||
effect = mw.text.trim(effect) | |||
if effect ~= "" then ul:tag("li"):wikitext(effect) end | |||
end | |||
inner:node(ul) | |||
collapsible:node(inner) | |||
container:node(collapsible) | |||
return container | |||
end | end | ||
function p.card(frame) | function p.card(frame) | ||
local args = frame.args or {} | |||
local name = args.name or args.title 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 sprite = args.recipe_sprite or "" | |||
local effects = args.effects or args.effect or "" | |||
local desc = args.description or args.desc or "" | |||
local parsed = parseRecipe(recipeStr) | |||
local container = html.create("div"):addClass("chem-card") | |||
local styleParts = {} | |||
if color ~= "" then table.insert(styleParts,"--card-accent:"..color) end | |||
if border ~= "" then table.insert(styleParts,"--card-border:"..border) end | |||
if bg ~= "" then table.insert(styleParts,"--card-bg:"..bg) end | |||
if #styleParts > 0 then container:attr("style", table.concat(styleParts,";")) end | |||
container:tag("h3"):wikitext(name) | |||
if #parsed.inputs > 0 or #parsed.outputs > 0 or (parsed.action and parsed.action ~= "") then | |||
container:node(buildRecipeHtml(parsed, sprite)) | |||
end | |||
container:node(buildEffectsHtml(effects)) | |||
if desc and desc ~= "" then | |||
container:tag("div"):addClass("chem-desc"):wikitext(desc) | |||
end | |||
return tostring(container) | |||
end | end | ||
return p | return p | ||
Версия от 06:04, 11 ноября 2025
Для документации этого модуля может быть создана страница Модуль:ChemCard/doc
local p = {}
local html = mw.html
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 parseRecipe(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.find(mw.ustring.lower(line), "смеш") 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, sprite)
local container = html.create("div"):addClass("chem-recipe-block")
container:tag("span"):addClass("chem-block-heading chem-heading"):attr("data-kind","recipe"):wikitext("Рецепт")
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(parsed.inputs) do
inputsDiv:tag("div"):addClass("recipe-item"):wikitext(it.name.." ["..it.qty.."]")
end
inner:node(inputsDiv)
if parsed.action and #parsed.inputs > 0 and #parsed.outputs > 0 then
local actionDiv = html.create("div"):addClass("recipe-action")
if sprite and sprite ~= "" then
actionDiv:tag("div"):addClass("action-sprite"):wikitext(sprite)
end
actionDiv:wikitext(parsed.action)
inner:node(actionDiv)
end
local outputsDiv = html.create("div"):addClass("recipe-outputs")
for _, it in ipairs(parsed.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")
container:tag("span"):addClass("chem-block-heading chem-heading"):attr("data-kind","effects"):wikitext("Эффекты")
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")
for effect in mw.text.gsplit(effects, "[;\n]") do
effect = mw.text.trim(effect)
if effect ~= "" then 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 = frame.args or {}
local name = args.name or args.title 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 sprite = args.recipe_sprite or ""
local effects = args.effects or args.effect or ""
local desc = args.description or args.desc or ""
local parsed = parseRecipe(recipeStr)
local container = html.create("div"):addClass("chem-card")
local styleParts = {}
if color ~= "" then table.insert(styleParts,"--card-accent:"..color) end
if border ~= "" then table.insert(styleParts,"--card-border:"..border) end
if bg ~= "" then table.insert(styleParts,"--card-bg:"..bg) end
if #styleParts > 0 then container:attr("style", table.concat(styleParts,";")) end
container:tag("h3"):wikitext(name)
if #parsed.inputs > 0 or #parsed.outputs > 0 or (parsed.action and parsed.action ~= "") then
container:node(buildRecipeHtml(parsed, sprite))
end
container:node(buildEffectsHtml(effects))
if desc and desc ~= "" then
container:tag("div"):addClass("chem-desc"):wikitext(desc)
end
return tostring(container)
end
return p