Модуль:Песочница/Pok: различия между версиями
Pok (обсуждение | вклад) Нет описания правки |
Pok (обсуждение | вклад) Нет описания правки |
||
| Строка 6: | Строка 6: | ||
end | end | ||
local function parse_keys_from_template(content) | |||
local function parse_keys_from_template(content, switches) | |||
if not content then return {} end | if not content then return {} end | ||
local lower = content:lower() | local lower = content:lower() | ||
local | local result = {} | ||
for _, sw in ipairs(switches) do | |||
result[sw] = {} | |||
local swLower = sw:lower() | |||
local pos = lower:find("|%s*" .. swLower .. "%s*=") | |||
if not pos then | |||
-- no keys for this switch | |||
else | |||
local startBrace = content:find("{{", pos) | |||
local region = nil | |||
if startBrace then | |||
local len = #content | |||
local i = startBrace | |||
local depth = 0 | |||
while i <= len - 1 do | |||
local two = content:sub(i, i+1) | |||
if two == "{{" then | |||
depth = depth + 1 | |||
i = i + 2 | |||
elseif two == "}}" then | |||
depth = depth - 1 | |||
i = i + 2 | |||
if depth == 0 then | |||
region = content:sub(startBrace, i-1) | |||
break | |||
end | |||
else | |||
i = i + 1 | |||
end | |||
end | |||
end | |||
if not region then | |||
local substr = content:sub(pos) | |||
local endPos = substr:find("}}") | |||
if endPos then | |||
region = substr:sub(1, endPos) | |||
else | |||
region = substr | |||
end | |||
end | |||
for key in string.gmatch(region, "|%s*([^=|%}]-)%s*=") do | |||
local k = trim(key) | |||
if k ~= "" then | |||
table.insert(result[sw], k) | |||
end | end | ||
end | end | ||
end | end | ||
end | end | ||
return result | |||
return | |||
end | end | ||
| Строка 130: | Строка 126: | ||
local out = {} | local out = {} | ||
local errors = {} | local errors = {} | ||
local | local switches = { "card", "title" } | ||
local | local switchKeyOrder = {} | ||
local switchKeyToTemplates = {} | |||
for _, sw in ipairs(switches) do | |||
switchKeyOrder[sw] = {} | |||
switchKeyToTemplates[sw] = {} | |||
end | |||
local switchConfigs = { | |||
card = { | |||
wrapper = function(key, tplCalls) | |||
if not tplCalls or #tplCalls == 0 then return "" end | |||
return "{{card|" .. mw.text.encode(key) .. "|" .. table.concat(tplCalls, " ") .. "}}" | |||
end | |||
}, | |||
title = { | |||
wrapper = function(key, tplCalls) | |||
local parts = {} | |||
table.insert(parts, "<h2>" .. mw.text.encode(key) .. "</h2>") | |||
if tplCalls and #tplCalls > 0 then | |||
for _, tpl in ipairs(tplCalls) do | |||
table.insert(parts, tpl) | |||
end | |||
end | |||
return table.concat(parts, "\n") | |||
end | |||
} | |||
} | |||
local function lcfirst(s) | local function lcfirst(s) | ||
| Строка 138: | Строка 159: | ||
end | end | ||
local items = {} | |||
for compName,_ in pairs(foundComponents) do | for compName,_ in pairs(foundComponents) do | ||
table.insert(items, { kind = "component", name = compName }) | |||
end | |||
for protoName,_ in pairs(foundPrototypes) do | |||
table.insert(items, { kind = "prototype", name = protoName }) | |||
end | end | ||
for | for _, item in ipairs(items) do | ||
local | local kind = item.kind | ||
local tplPath = " | local name = item.name | ||
local pathName = lcfirst(name) | |||
local tplPath = kind .. "/" .. pathName | |||
local content = load_template_content(tplPath) | local content = load_template_content(tplPath) | ||
if not content then | if not content then | ||
table.insert(errors, "Ошибка: не найден шаблон | table.insert(errors, "Ошибка: не найден шаблон " .. kind .. "/" .. pathName) | ||
else | else | ||
local | local parsed = parse_keys_from_template(content, switches) | ||
for _, key in ipairs(keys) do | for _, sw in ipairs(switches) do | ||
local keys = parsed[sw] or {} | |||
for _, key in ipairs(keys) do | |||
table.insert( | if not switchKeyToTemplates[sw][key] then | ||
switchKeyToTemplates[sw][key] = {} | |||
table.insert(switchKeyOrder[sw], key) | |||
end | |||
table.insert(switchKeyToTemplates[sw][key], "{{" .. tplPath .. "|title|" .. key .. "}}") | |||
end | end | ||
end | end | ||
end | end | ||
| Строка 178: | Строка 194: | ||
end | end | ||
for _, | for _, sw in ipairs(switches) do | ||
local cfg = switchConfigs[sw] or {} | |||
for _, | for _, key in ipairs(switchKeyOrder[sw]) do | ||
table.insert(out, | local tplCalls = switchKeyToTemplates[sw][key] or {} | ||
if cfg.wrapper then | |||
local outStr = cfg.wrapper(key, tplCalls) | |||
if outStr and outStr ~= "" then | |||
table.insert(out, outStr) | |||
end | |||
end | |||
end | end | ||
end | end | ||