Module:RecipeLoader
Documentation for this module may be created at Module:RecipeLoader/doc
local p = {}
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 matches = {}
for _, recipe in ipairs(recipes) do
if recipe.name == recipeName then
table.insert(matches, recipe)
end
end
if #matches == 0 then
return "⚠️ Recipe not found: " .. tostring(recipeName)
end
local html = mw.html.create()
for _, recipe in ipairs(matches) do
local tableNode = html:tag("table")
:addClass("wikitable recipe-table")
:css("text-align", "center")
:css("margin-bottom", "10px")
tableNode:tag("tr")
:tag("th"):wikitext("Ingredients"):done()
:tag("th"):wikitext("Results"):done()
:tag("th"):wikitext("Time"):done()
:done()
local maxRows = math.max(#(recipe.ingredients or {}), #(recipe.results or {}))
if maxRows < 1 then maxRows = 1 end
for i = 1, maxRows do
local tr = tableNode:tag("tr")
-- Ingredient
if recipe.ingredients and recipe.ingredients[i] then
local ing = recipe.ingredients[i]
tr:tag("td"):wikitext(string.format("[[%s]] × %s", ing[1], ing[2])):done()
else
tr:tag("td"):wikitext(""):done()
end
-- Result
if recipe.results and recipe.results[i] then
local res = recipe.results[i]
tr:tag("td"):wikitext(string.format("[[%s]] × %s", res[1], res[2])):done()
else
tr:tag("td"):wikitext(""):done()
end
-- Time (одна ячейка на таблицу)
if i == 1 then
tr:tag("td"):attr("rowspan", maxRows):wikitext(tostring(recipe.time or "")):done()
end
tr:done()
end
end
return tostring(html)
end
return p