Модуль:Песочница/Pok: различия между версиями

мНет описания правки
Нет описания правки
Строка 47: Строка 47:


local function load_template_content(path)
local function load_template_content(path)
local title = mw.title.new("Шаблон:" .. path)
local title = mw.title.new("Template:" .. path)
if not title then return nil end
if not title then return nil end
local ok, content = pcall(function() return title:getContent() end)
local ok, content = pcall(function() return title:getContent() end)
Строка 86: Строка 86:


local out = {}
local out = {}
local errors = {}
local keyOrder = {}
local keyToTemplates = {}
local function lcfirst(s)
if not s or s == "" then return s end
return string.lower(s:sub(1,1)) .. (s:sub(2) or "")
end


for compName,_ in pairs(foundComponents) do
for compName,_ in pairs(foundComponents) do
local tplPath = "Component/" .. compName
local compPathName = lcfirst(compName)
local tplPath = "component/" .. compPathName
local content = load_template_content(tplPath)
local content = load_template_content(tplPath)
if not content then
if not content then
table.insert(out, "<h2>" .. mw.text.encode(compName) .. "</h2>")
table.insert(errors, "Ошибка: не найден шаблон component/" .. compPathName)
table.insert(out, "Template: " .. tplPath)
else
else
local keys = parse_keys_from_template(content)
local keys = parse_keys_from_template(content)
for _, key in ipairs(keys) do
for _, key in ipairs(keys) do
table.insert(out, "<h2>" .. key .. "</h2>")
if not keyToTemplates[key] then
table.insert(out, "{{" .. tplPath .. "|title|" .. key .. "}}")
keyToTemplates[key] = {}
table.insert(keyOrder, key)
end
table.insert(keyToTemplates[key], "{{" .. tplPath .. "|title|" .. key .. "}}")
end
end
end
end
Строка 103: Строка 114:


for protoName,_ in pairs(foundPrototypes) do
for protoName,_ in pairs(foundPrototypes) do
local tplPath = "prototype/" .. protoName
local protoPathName = lcfirst(protoName)
local tplPath = "prototype/" .. protoPathName
local content = load_template_content(tplPath)
local content = load_template_content(tplPath)
if not content then
if not content then
table.insert(out, "<h2>" .. mw.text.encode(protoName) .. "</h2>")
table.insert(errors, "Ошибка: не найден шаблон prototype/" .. protoPathName)
table.insert(out, "Template: " .. tplPath)
else
else
local keys = parse_keys_from_template(content)
local keys = parse_keys_from_template(content)
for _, key in ipairs(keys) do
for _, key in ipairs(keys) do
table.insert(out, "<h2>" .. key .. "</h2>")
if not keyToTemplates[key] then
table.insert(out, "{{" .. tplPath .. "|title|" .. key .. "}}")
keyToTemplates[key] = {}
table.insert(keyOrder, key)
end
table.insert(keyToTemplates[key], "{{" .. tplPath .. "|title|" .. key .. "}}")
end
end
end
end
-- Append errors first
for _, e in ipairs(errors) do
table.insert(out, "<div class=\"error\">" .. mw.text.encode(e) .. "</div>")
end
-- Then for each key output header and all template invocations for that key
for _, key in ipairs(keyOrder) do
table.insert(out, "<h2>" .. mw.text.encode(key) .. "</h2>")
for _, tplCall in ipairs(keyToTemplates[key]) do
table.insert(out, tplCall)
end
end
end
end