Module:RecipeLoader: Difference between revisions
Jump to navigation
Jump to search
Created page with "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 -- Находим все с..." |
No edit summary |
||
Line 1: | Line 1: | ||
local p = {} | local p = {} | ||
-- Функция вывода одной таблицы для рецепта | |||
local function renderRecipe(recipe) | |||
local tableNode = mw.html.create("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 | |||
return tableNode | |||
end | |||
-- Вывод одного конкретного рецепта | |||
function p.get(frame) | function p.get(frame) | ||
local dictName = frame.args[1] | local dictName = frame.args[1] | ||
Line 14: | Line 60: | ||
end | end | ||
local html = mw.html.create() | |||
for _, recipe in ipairs(recipes) do | for _, recipe in ipairs(recipes) do | ||
if recipe.name == recipeName then | if recipe.name == recipeName then | ||
html:node(renderRecipe(recipe)) | |||
end | end | ||
end | end | ||
if | if tostring(html) == "" then | ||
return "⚠️ Recipe not found: " .. tostring(recipeName) | return "⚠️ Recipe not found: " .. tostring(recipeName) | ||
end | 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 | end | ||
Revision as of 12:27, 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", "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
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