Dantes (обсуждение | вклад) Нет описания правки |
Dantes (обсуждение | вклад) Нет описания правки |
||
| Строка 1: | Строка 1: | ||
local p = {} | local p = {} | ||
local mw_text = mw.text | |||
-- Функция для безопасного экранирования HTML | |||
local function escape_html(text) | |||
return mw_text.encode(text or '', true) | |||
end | |||
function p.render(frame) | function p.render(frame) | ||
-- Безопасное получение аргументов | |||
local parent = frame:getParent() | |||
local args | |||
if parent then | |||
args = parent.args | |||
else | |||
args = frame.args | |||
end | |||
-- Обработка данных с экранированием | |||
local title = args['название'] and escape_html(args['название']) or 'Без названия' | |||
local result = args['результат'] and escape_html(args['результат']) or 'Неизвестно' | |||
local description = args['описание'] or 'Нет описания.' -- HTML разрешён | |||
-- Сбор ингредиентов | |||
local recipe = {} | |||
local i = 1 | |||
while args['ингредиент' .. i] do | |||
table.insert(recipe, escape_html(args['ингредиент' .. i])) | |||
i = i + 1 | |||
end | |||
-- Сбор эффектов | |||
local effects = {} | |||
i = 1 | |||
while args['эффект' .. i] do | |||
table.insert(effects, escape_html(args['эффект' .. i])) | |||
i = i + 1 | |||
end | |||
-- Генерация HTML | |||
local out = { | |||
'<div class="alchemy-recipe-card" style="border:1px solid #555;padding:1em;margin:1em 0;background:#1a1a1a;color:#eee;border-radius:12px;">', | |||
'<div style="font-size:1.5em;font-weight:bold;margin-bottom:0.5em;">' .. title .. '</div>' | |||
} | |||
-- Секция рецепта | |||
if #recipe > 0 then | |||
table.insert(out, '<div style="margin-bottom:0.5em;"><b>Рецепт</b><ul>') | |||
for _, r in ipairs(recipe) do | |||
table.insert(out, '<li>' .. r .. '</li>') | |||
end | |||
table.insert(out, '<li><i>Смешайте</i></li>') | |||
table.insert(out, '<li><b>' .. result .. '</b></li></ul></div>') | |||
end | |||
-- Секция эффектов | |||
if #effects > 0 then | |||
table.insert(out, '<div style="margin-bottom:0.5em;"><b>Эффекты</b><ul>') | |||
for _, e in ipairs(effects) do | |||
table.insert(out, '<li>' .. e .. '</li>') | |||
end | |||
table.insert(out, '</ul></div>') | |||
end | |||
-- Секция описания | |||
table.insert(out, '<div><b>Описание</b><br>' .. description .. '</div>') | |||
table.insert(out, '</div>') | |||
return table.concat(out, '\n') | |||
end | end | ||
return p | return p | ||
Версия от 17:20, 20 июня 2025
Для документации этого модуля может быть создана страница Модуль:AlchemyRecipe/doc
local p = {}
local mw_text = mw.text
-- Функция для безопасного экранирования HTML
local function escape_html(text)
return mw_text.encode(text or '', true)
end
function p.render(frame)
-- Безопасное получение аргументов
local parent = frame:getParent()
local args
if parent then
args = parent.args
else
args = frame.args
end
-- Обработка данных с экранированием
local title = args['название'] and escape_html(args['название']) or 'Без названия'
local result = args['результат'] and escape_html(args['результат']) or 'Неизвестно'
local description = args['описание'] or 'Нет описания.' -- HTML разрешён
-- Сбор ингредиентов
local recipe = {}
local i = 1
while args['ингредиент' .. i] do
table.insert(recipe, escape_html(args['ингредиент' .. i]))
i = i + 1
end
-- Сбор эффектов
local effects = {}
i = 1
while args['эффект' .. i] do
table.insert(effects, escape_html(args['эффект' .. i]))
i = i + 1
end
-- Генерация HTML
local out = {
'<div class="alchemy-recipe-card" style="border:1px solid #555;padding:1em;margin:1em 0;background:#1a1a1a;color:#eee;border-radius:12px;">',
'<div style="font-size:1.5em;font-weight:bold;margin-bottom:0.5em;">' .. title .. '</div>'
}
-- Секция рецепта
if #recipe > 0 then
table.insert(out, '<div style="margin-bottom:0.5em;"><b>Рецепт</b><ul>')
for _, r in ipairs(recipe) do
table.insert(out, '<li>' .. r .. '</li>')
end
table.insert(out, '<li><i>Смешайте</i></li>')
table.insert(out, '<li><b>' .. result .. '</b></li></ul></div>')
end
-- Секция эффектов
if #effects > 0 then
table.insert(out, '<div style="margin-bottom:0.5em;"><b>Эффекты</b><ul>')
for _, e in ipairs(effects) do
table.insert(out, '<li>' .. e .. '</li>')
end
table.insert(out, '</ul></div>')
end
-- Секция описания
table.insert(out, '<div><b>Описание</b><br>' .. description .. '</div>')
table.insert(out, '</div>')
return table.concat(out, '\n')
end
return p