Модуль:chemcard
| Строка 36: | Строка 36: | ||
if #currentInputs > 0 then | if #currentInputs > 0 then | ||
local outputs = {} | local outputs = {} | ||
i = i + 1 | i = i + 1 -- переходим сразу после «смешайте» | ||
while i <= #lines do | while i <= #lines do | ||
local nextLine = lines[i] | local nextLine = lines[i] | ||
| Строка 44: | Строка 44: | ||
i = i + 1 | i = i + 1 | ||
end | end | ||
local action = mw.text.trim(line) | |||
table.insert(recipes, { | table.insert(recipes, { | ||
inputs = currentInputs, | inputs = currentInputs, | ||
outputs = outputs, | outputs = outputs, | ||
action = | action = action | ||
}) | }) | ||
currentInputs = {} | currentInputs = {} | ||
else | else | ||
i = i + 1 | i = i + 1 -- пропускаем лишний «смешайте» | ||
end | end | ||
else | else | ||
| Строка 64: | Строка 65: | ||
end | end | ||
local function | local function buildRecipeStepHtml(rec) | ||
if not rec or (#rec.inputs == 0 and #rec.outputs == 0) | if not rec or (#rec.inputs == 0 and #rec.outputs == 0) or (rec.action == "" and #rec.outputs == 0) then | ||
return nil | |||
end | end | ||
local inner = html.create("div"):addClass("chem-recipe") | local inner = html.create("div"):addClass("chem-recipe") | ||
-- INPUTS | -- INPUTS | ||
local inputsDiv = html.create("div"):addClass("recipe-inputs") | local inputsDiv = html.create("div"):addClass("recipe-inputs") | ||
for _, it in ipairs(rec.inputs) do | for _, it in ipairs(rec.inputs) do | ||
local item = inputsDiv:tag("div"):addClass("recipe-item") | local item = inputsDiv:tag("div"):addClass("recipe-item") | ||
item:wikitext('[[File:Beaker.png|36px|class=chem-beaker]] ' .. it.name .. ' [' .. it.qty .. ']') | item:wikitext('[[File:Beaker.png|36px|class=chem-beaker]] ' .. it.name .. ' [' .. it.qty .. ']') | ||
end | end | ||
inner:node(inputsDiv) | inner:node(inputsDiv) | ||
-- | -- ACTION + КОЛБА | ||
if rec.action ~= "" then | if rec.action and rec.action ~= "" then | ||
local actionDiv = inner:tag("div"):addClass("recipe-action") | local actionDiv = inner:tag("div"):addClass("recipe-action") | ||
actionDiv:wikitext(rec.action) | local hasSprite = mw.ustring.find(rec.action, "%[%[File:") | ||
if hasSprite then | |||
actionDiv:wikitext(rec.action) | |||
else | |||
actionDiv:wikitext('[[File:Beaker.png|36px|class=chem-beaker]]<br>' .. rec.action) | |||
end | |||
end | end | ||
-- OUTPUTS | -- OUTPUTS | ||
local outputsDiv = html.create("div"):addClass("recipe-outputs") | local outputsDiv = html.create("div"):addClass("recipe-outputs") | ||
for _, it in ipairs(rec.outputs) do | for _, it in ipairs(rec.outputs) do | ||
| Строка 102: | Строка 97: | ||
end | end | ||
inner:node(outputsDiv) | inner:node(outputsDiv) | ||
return inner | |||
return | |||
end | end | ||
| Строка 111: | Строка 104: | ||
if not effects or effects == "" or effects == "Нет" then return nil end | if not effects or effects == "" or effects == "Нет" then return nil end | ||
local container = html.create("div"):addClass("chem-effects-block") | local container = html.create("div"):addClass("chem-effects-block") | ||
local heading = html.create("div"):addClass("chem-heading"):attr("data-kind", "effects") | local heading = html.create("div"):addClass("chem-heading"):attr("data-kind", "effects") | ||
local hcontent = heading:tag("div"):addClass("chem-heading-content") | local hcontent = heading:tag("div"):addClass("chem-heading-content") | ||
| Строка 117: | Строка 109: | ||
hcontent:tag("span"):addClass("collapse-btn"):wikitext("развернуть") | hcontent:tag("span"):addClass("collapse-btn"):wikitext("развернуть") | ||
container:node(heading) | container:node(heading) | ||
local collapsible = html.create("div"):addClass("collapsible collapsed"):attr("data-kind", "effects") | local collapsible = html.create("div"):addClass("collapsible collapsed"):attr("data-kind", "effects") | ||
local inner = html.create("div"):addClass("chem-effects") | local inner = html.create("div"):addClass("chem-effects") | ||
for effect in mw.text.gsplit(effects, "[;\n]") do | for effect in mw.text.gsplit(effects, "[;\n]") do | ||
effect = mw.text.trim(effect) | effect = mw.text.trim(effect) | ||
| Строка 138: | Строка 128: | ||
local border = args.border or "#444" | local border = args.border or "#444" | ||
local desc = args.desc or "" | local desc = args.desc or "" | ||
local recipes = {} | local recipes = {} | ||
local i = 1 | local i = 1 | ||
| Строка 151: | Строка 141: | ||
i = i + 1 | i = i + 1 | ||
end | end | ||
local container = html.create("div"):addClass("chem-card") | local container = html.create("div"):addClass("chem-card") | ||
container:attr("style", "border-color:" .. border .. "; --card-accent:" .. color) | container:attr("style", "border-color:" .. border .. "; --card-accent:" .. color) | ||
container:tag("div"):addClass("chem-name-header"):wikitext(name) | container:tag("div"):addClass("chem-name-header"):wikitext(name) | ||
if #recipes > 0 then | if #recipes > 0 then | ||
for | local recipeBlock = html.create("div"):addClass("chem-recipe-block") | ||
local | local heading = html.create("div"):addClass("chem-heading"):attr("data-kind", "recipe") | ||
if | local hcontent = heading:tag("div"):addClass("chem-heading-content") | ||
hcontent:tag("span"):addClass("heading-text"):wikitext("Рецепты") | |||
hcontent:tag("span"):addClass("collapse-btn"):wikitext("развернуть") | |||
recipeBlock:node(heading) | |||
local collapsible = html.create("div"):addClass("collapsible collapsed"):attr("data-kind", "recipe") | |||
local stepsContainer = html.create("div"):addClass("chem-recipe-steps") | |||
for _, rec in ipairs(recipes) do | |||
local step = buildRecipeStepHtml(rec) | |||
if step then stepsContainer:node(step) end | |||
end | end | ||
collapsible:node(stepsContainer) | |||
recipeBlock:node(collapsible) | |||
container:node(recipeBlock) | |||
end | end | ||
local effectsBlock = buildEffectsHtml(args.effects) | local effectsBlock = buildEffectsHtml(args.effects) | ||
if effectsBlock then container:node(effectsBlock) end | if effectsBlock then container:node(effectsBlock) end | ||
if desc ~= "" then | if 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 | ||