Dantes (обсуждение | вклад) Нет описания правки |
Dantes (обсуждение | вклад) Нет описания правки |
||
| Строка 24: | Строка 24: | ||
local function parseLineToItem(l) | local function parseLineToItem(l) | ||
local name, qty = mw.ustring.match(l, "^(.*)%s*%[%s*([%d%. | local name, qty = mw.ustring.match(l, "^(.*)%s*%[%s*([%d,%.]+)%s*%]%s*$") | ||
if not name then | if not name then | ||
name = l | name = l | ||
| Строка 30: | Строка 30: | ||
end | end | ||
name = mw.text.trim(name) | name = mw.text.trim(name) | ||
return {name = name, qty = qty or "1"} | |||
return {name = name, qty = qty} | |||
end | end | ||
local function parseRecipeString(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.lower(line):find("смеш") then | if mw.ustring.lower(line):find("смеш") then | ||
splitIdx = i | splitIdx = i | ||
| Строка 47: | Строка 44: | ||
end | end | ||
end | end | ||
local inputs = {} | 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 | local function buildRecipeHtml(rec, sprite) | ||
local | 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") | local inputsDiv = html.create("div"):addClass("recipe-inputs") | ||
for _, it in ipairs(rec.inputs | for _,it in ipairs(rec.inputs) do | ||
inputsDiv:tag("div"):addClass("recipe-item"):wikitext(it.name .. " [" .. | inputsDiv:tag("div"):addClass("recipe-item"):wikitext(it.name.." ["..it.qty.."]") | ||
end | end | ||
inner:node(inputsDiv) | |||
if rec.action and #rec.inputs > 0 and #rec.outputs > 0 then | |||
local | if rec.action and #rec.inputs>0 and #rec.outputs>0 then | ||
local actionDiv = html.create("div"):addClass("recipe-action") | |||
if | if sprite and sprite~="" then | ||
actionDiv:tag("div"):addClass("action-sprite"):wikitext(sprite) | |||
end | end | ||
actionDiv:wikitext(rec.action) | |||
inner:node(actionDiv) | |||
end | end | ||
local outputsDiv = html.create("div"):addClass("recipe-outputs") | local outputsDiv = html.create("div"):addClass("recipe-outputs") | ||
for _, it in ipairs(rec.outputs | for _,it in ipairs(rec.outputs) do | ||
outputsDiv:tag("div"):addClass("recipe-item"):wikitext(it.name .. " [" .. | 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") | |||
inner:tag("b"):wikitext("Эффекты:") | |||
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 | end | ||
inner:node(ul) | |||
collapsible:node(inner) | |||
container:node(collapsible) | |||
return | return container | ||
end | end | ||
| Строка 129: | Строка 111: | ||
local bg = args.bg or args.bgcolor or "" | local bg = args.bg or args.bgcolor or "" | ||
local recipeStr = args.recipe or args.rezept or "" | local recipeStr = args.recipe or args.rezept or "" | ||
local | local sprite = args.recipe_sprite or "" | ||
local effects = args.effects or args.effect or "" | local effects = args.effects or args.effect or "" | ||
local desc = args.description or args.desc or "" | local desc = args.description or args.desc or "" | ||
local parsed = parseRecipeString(recipeStr) | |||
local container = html.create("div"):addClass("chem-card") | local container = html.create("div"):addClass("chem-card") | ||
local styleParts = {} | local styleParts = {} | ||
if color ~= "" then table.insert(styleParts, "--card-accent:" .. color) end | if color~="" then table.insert(styleParts,"--card-accent:"..color) end | ||
if border ~= "" then table.insert(styleParts, "--card-border:" .. border) end | if border~="" then table.insert(styleParts,"--card-border:"..border) end | ||
if bg ~= "" then table.insert(styleParts, "--card-bg:" .. bg) end | if bg~="" then table.insert(styleParts,"--card-bg:"..bg) end | ||
if #styleParts > 0 then container:attr("style", table.concat(styleParts, ";")) end | if #styleParts>0 then container:attr("style", table.concat(styleParts,";")) end | ||
container:tag("h3"):wikitext(name) | container:tag("h3"):wikitext(name) | ||
container: | container:node(buildRecipeHtml(parsed, sprite)) | ||
container:node(buildEffectsHtml(effects)) | |||
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:19, 31 октября 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, 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(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")
if sprite and sprite~="" then
actionDiv:tag("div"):addClass("action-sprite"):wikitext(sprite)
end
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")
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")
inner:tag("b"):wikitext("Эффекты:")
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 = getArgs(frame)
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 = parseRecipeString(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)
container:node(buildRecipeHtml(parsed, sprite))
container:node(buildEffectsHtml(effects))
if desc and desc~="" then
container:tag("div"):addClass("chem-desc"):wikitext(desc)
end
return tostring(container)
end
return p