Module:RecipeLoader: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
No edit summary |
||
Line 5: | Line 5: | ||
local tableNode = mw.html.create("table") | local tableNode = mw.html.create("table") | ||
:addClass("wikitable recipe-table") | :addClass("wikitable recipe-table") | ||
:css("text-align", " | :css("text-align", "left") | ||
:css("margin-bottom", " | :css("margin-bottom", "15px") | ||
:css("min-width", "300px") | |||
-- Заголовок | |||
tableNode:tag("tr") | tableNode:tag("tr") | ||
:tag("th"): | :tag("th"):attr("colspan", 3):wikitext("Recipe"):done() | ||
:done() | :done() | ||
-- ===== Ingredients ===== | |||
tableNode:tag("tr") | |||
:tag("th"):attr("colspan", 3):wikitext("Ingredients"):done() | |||
:done() | |||
if recipe.ingredients and #recipe.ingredients > 0 then | |||
for _, ing in ipairs(recipe.ingredients) do | |||
local tr = tableNode:tag("tr") | |||
tr:tag("td"):wikitext(string.format("[[%s]] | tr:tag("td"):wikitext(string.format("[[File:T_%s.png|32px]]", ing[1])):done() | ||
tr:tag("td"):wikitext(string.format("[[%s]]", ing[1])):done() | |||
tr:tag("td"):wikitext(""):done() | tr:tag("td"):wikitext("× " .. tostring(ing[2])):done() | ||
end | end | ||
else | |||
tableNode:tag("tr") | |||
:tag("td"):attr("colspan", 3):wikitext("—"):done() | |||
end | |||
-- ===== Results ===== | |||
tableNode:tag("tr") | |||
:tag("th"):attr("colspan", 3):wikitext("Results"):done() | |||
:done() | |||
if recipe.results and #recipe.results > 0 then | |||
for _, res in ipairs(recipe.results) do | |||
tr:tag("td"): | local tr = tableNode:tag("tr") | ||
tr:tag("td"):wikitext(string.format("[[File:T_%s.png|32px]]", res[1])):done() | |||
tr:tag("td"):wikitext(string.format("[[%s]]", res[1])):done() | |||
tr:tag("td"):wikitext("× " .. tostring(res[2])):done() | |||
end | end | ||
tr:done() | else | ||
tableNode:tag("tr") | |||
:tag("td"):attr("colspan", 3):wikitext("—"):done() | |||
end | end | ||
-- ===== Time ===== | |||
tableNode:tag("tr") | |||
:tag("th"):attr("colspan", 3):wikitext("Time"):done() | |||
:done() | |||
tableNode:tag("tr") | |||
:tag("td"):attr("colspan", 3):wikitext(tostring(recipe.time or "") .. " ticks"):done() | |||
return tableNode | return tableNode | ||
end | end | ||
-- Вывод одного конкретного рецепта | -- Вывод одного конкретного рецепта |
Revision as of 12:33, 31 July 2025
Documentation for this module may be created at Module:RecipeLoader/doc
local p = {}
-- Функция вывода одной таблицы для рецепта
local function renderRecipe(recipe)
local tableNode = mw.html.create("table")
:addClass("wikitable recipe-table")
:css("text-align", "left")
:css("margin-bottom", "15px")
:css("min-width", "300px")
-- Заголовок
tableNode:tag("tr")
:tag("th"):attr("colspan", 3):wikitext("Recipe"):done()
:done()
-- ===== Ingredients =====
tableNode:tag("tr")
:tag("th"):attr("colspan", 3):wikitext("Ingredients"):done()
:done()
if recipe.ingredients and #recipe.ingredients > 0 then
for _, ing in ipairs(recipe.ingredients) do
local tr = tableNode:tag("tr")
tr:tag("td"):wikitext(string.format("[[File:T_%s.png|32px]]", ing[1])):done()
tr:tag("td"):wikitext(string.format("[[%s]]", ing[1])):done()
tr:tag("td"):wikitext("× " .. tostring(ing[2])):done()
end
else
tableNode:tag("tr")
:tag("td"):attr("colspan", 3):wikitext("—"):done()
end
-- ===== Results =====
tableNode:tag("tr")
:tag("th"):attr("colspan", 3):wikitext("Results"):done()
:done()
if recipe.results and #recipe.results > 0 then
for _, res in ipairs(recipe.results) do
local tr = tableNode:tag("tr")
tr:tag("td"):wikitext(string.format("[[File:T_%s.png|32px]]", res[1])):done()
tr:tag("td"):wikitext(string.format("[[%s]]", res[1])):done()
tr:tag("td"):wikitext("× " .. tostring(res[2])):done()
end
else
tableNode:tag("tr")
:tag("td"):attr("colspan", 3):wikitext("—"):done()
end
-- ===== Time =====
tableNode:tag("tr")
:tag("th"):attr("colspan", 3):wikitext("Time"):done()
:done()
tableNode:tag("tr")
:tag("td"):attr("colspan", 3):wikitext(tostring(recipe.time or "") .. " ticks"):done()
return tableNode
end
-- Вывод одного конкретного рецепта
function p.get(frame)
local dictName = frame.args[1]
local recipeName = frame.args[2]
if not dictName or not recipeName then
return "❌ Missing parameters: {{Recipe|DictionaryName|RecipeName}}"
end
local success, recipes = pcall(mw.loadData, "Module:" .. dictName)
if not success or type(recipes) ~= "table" then
return "❌ Recipe dictionary not found: " .. tostring(dictName)
end
local html = mw.html.create()
for _, recipe in ipairs(recipes) do
if recipe.name == recipeName then
html:node(renderRecipe(recipe))
end
end
if tostring(html) == "" then
return "⚠️ Recipe not found: " .. tostring(recipeName)
end
return tostring(html)
end
-- Вывод всех рецептов из словаря
function p.getAll(frame)
local dictName = frame.args[1]
if not dictName then
return "❌ Missing parameter: {{AllRecipes|DictionaryName}}"
end
local success, recipes = pcall(mw.loadData, "Module:" .. dictName)
if not success or type(recipes) ~= "table" then
return "❌ Recipe dictionary not found: " .. tostring(dictName)
end
local html = mw.html.create()
for _, recipe in ipairs(recipes) do
html:tag("h4"):wikitext(recipe.name or "(no name)"):done()
html:node(renderRecipe(recipe))
end
return tostring(html)
end
return p