Модуль:Сущность: различия между версиями
Pok (обсуждение | вклад) мНет описания правки |
Pok (обсуждение | вклад) мНет описания правки |
||
| Строка 39: | Строка 39: | ||
local tplLabel = "Template:" .. s.tplPath | local tplLabel = "Template:" .. s.tplPath | ||
return "[[" .. tplLabel .. "|" .. className .. "]]" | return "[[" .. tplLabel .. "|" .. className .. "]]" | ||
end | |||
local switches = { "card", "title" } | |||
local switchConfigs = { | |||
card = { | |||
wrapper = function(key, tplCalls, sources) | |||
if not tplCalls or #tplCalls == 0 then return "" end | |||
local calls = table.concat(tplCalls, " ") | |||
local srcStr = "" | |||
if sources and #sources > 0 then | |||
local srcParts = {} | |||
for _, s in ipairs(sources) do table.insert(srcParts, makeSourceLink(s)) end | |||
srcStr = " " .. table.concat(srcParts, " ") | |||
end | |||
return "{{карточка/Сущность|" .. mw.text.encode(key) .. "|" .. calls .. srcStr .. "}}" | |||
end | |||
}, | |||
title = { | |||
wrapper = function(key, tplCalls, sources) | |||
local parts = {} | |||
table.insert(parts, "<h2>" .. mw.text.encode(key) .. "</h2>") | |||
if tplCalls and #tplCalls > 0 then | |||
for i, tpl in ipairs(tplCalls) do | |||
local line = tpl | |||
local src = sources and sources[i] | |||
if src then line = line .. " " .. makeSourceLink(src) end | |||
table.insert(parts, "<p>" .. line .. "</p>") | |||
end | |||
end | |||
return table.concat(parts, "\n") | |||
end | |||
} | |||
} | |||
local function getTemplateMeta(frame, tplPath) | |||
local expanded = frame:expandTemplate { | |||
title = tplPath, | |||
args = { "json" } | |||
} | |||
local ok, data = pcall(mw.text.jsonDecode, expanded) | |||
if ok and type(data) == "table" then | |||
return data | |||
end | |||
return "" | |||
end | |||
local function renderBlocks(frame, switchesTbl, configs, keyOrder, keyToTemplates, keySources) | |||
local outLocal = {} | |||
for _, sw in ipairs(switchesTbl) do | |||
local cfg = configs[sw] or {} | |||
for _, key in ipairs(keyOrder[sw] or {}) do | |||
local entries = keyToTemplates[sw][key] or {} | |||
local tplCalls = {} | |||
local sources = {} | |||
if #entries > 0 then | |||
table.sort(entries, function(a, b) | |||
if a.priority == b.priority then return a.idx < b.idx end | |||
return a.priority > b.priority | |||
end) | |||
for _, e in ipairs(entries) do | |||
table.insert(tplCalls, e.tpl) | |||
table.insert(sources, e.source) | |||
end | |||
end | |||
if cfg.wrapper then | |||
local outStr = cfg.wrapper(key, tplCalls, sources) | |||
if outStr and outStr ~= "" then table.insert(outLocal, outStr) end | |||
end | |||
end | |||
end | |||
return outLocal | |||
end | end | ||
| Строка 64: | Строка 137: | ||
end | end | ||
local switchKeyOrder, switchKeyToTemplates, switchKeySources = {}, {}, {} | local switchKeyOrder, switchKeyToTemplates, switchKeySources = {}, {}, {} | ||
for _, sw in ipairs(switches) do | for _, sw in ipairs(switches) do | ||
switchKeyOrder[sw] = {}; switchKeyToTemplates[sw] = {}; switchKeySources[sw] = {} | switchKeyOrder[sw] = {}; switchKeyToTemplates[sw] = {}; switchKeySources[sw] = {} | ||
end | end | ||
| Строка 169: | Строка 196: | ||
local out = {} | local out = {} | ||
local blocks = renderBlocks(frame, switches, switchConfigs, switchKeyOrder, switchKeyToTemplates, switchKeySources) | |||
for _, b in ipairs(blocks) do table.insert(out, b) end | |||
for _, e in ipairs(errors) do table.insert(out, e) end | |||
return frame:preprocess(table.concat(out, "\n\n")) | |||
end | |||
function p.preview(frame) | |||
local args = frame.args or {} | |||
local tplPath = args[1] or args.tpl or args.template or "" | |||
if tplPath == "" then return "" end | |||
local content = load_template_content(tplPath) | |||
if not content then | |||
return "" | |||
end | |||
local parsed = getTemplateMeta(frame, tplPath) or {} | |||
local switchKeyOrder, switchKeyToTemplates, switchKeySources = {}, {}, {} | |||
for _, sw in ipairs(switches) do | for _, sw in ipairs(switches) do | ||
switchKeyOrder[sw] = {}; switchKeyToTemplates[sw] = {}; switchKeySources[sw] = {} | |||
end | |||
for _, sw in ipairs(switches) do | |||
local keys = parsed[sw] or {} | |||
for idx, key in ipairs(keys) do | |||
if not switchKeyToTemplates[sw][key] then | |||
switchKeyToTemplates[sw][key] = {} | |||
table.insert(switchKeyOrder[sw], key) | |||
if | |||
end | end | ||
local tplStr = makeTplCall(tplPath, sw, key, "") | |||
local entry = { | |||
tpl = tplStr, | |||
source = { kind = "template", name = tplPath, pathName = tplPath, tplPath = tplPath }, | |||
priority = 1, | |||
idx = #switchKeyToTemplates[sw][key] + 1 | |||
} | |||
table.insert(switchKeyToTemplates[sw][key], entry) | |||
end | end | ||
end | end | ||
for _, | |||
local out = {} | |||
local blocks = renderBlocks(frame, switches, switchConfigs, switchKeyOrder, switchKeyToTemplates, switchKeySources) | |||
for _, b in ipairs(blocks) do table.insert(out, b) end | |||
return frame:preprocess(table.concat(out, "\n\n")) | return frame:preprocess(table.concat(out, "\n\n")) | ||