Модуль:Сущность: различия между версиями
Pok (обсуждение | вклад) мНет описания правки |
Pok (обсуждение | вклад) Нет описания правки |
||
| Строка 505: | Строка 505: | ||
return (text:gsub(patt, replacement)) | return (text:gsub(patt, replacement)) | ||
end | |||
function p.json(frame) | |||
local args = getArgs(frame, { removeBlanks = false }) | |||
local jsonStr = mw.text.unstripNoWiki(trim(args[1] or args.json or "")) | |||
local tplPath = trim(args[2] or args.template or "") | |||
if jsonStr == "" or tplPath == "" then return "" end | |||
local ok, data = pcall(mw.text.jsonDecode, jsonStr) | |||
if not ok or type(data) ~= "table" then | |||
return "" | |||
end | |||
local calls = {} | |||
local function makeCall(id, obj) | |||
if type(id) ~= "string" then return end | |||
local parts = { "{{" .. tplPath, "id=" .. id } | |||
if type(obj) == "table" then | |||
for k, v in pairs(obj) do | |||
if v ~= nil then | |||
parts[#parts + 1] = tostring(k) .. "=" .. tostring(v) | |||
end | |||
end | |||
end | |||
parts[#parts + 1] = "}}" | |||
calls[#calls + 1] = table.concat(parts, "|") | |||
end | |||
if is_array(data) then | |||
for _, item in ipairs(data) do | |||
if type(item) == "table" then | |||
for k, v in pairs(item) do | |||
makeCall(k, v) | |||
end | |||
end | |||
end | |||
else | |||
for k, v in pairs(data) do | |||
makeCall(k, v) | |||
end | |||
end | |||
if #calls == 0 then | |||
return "" | |||
end | |||
local rendered = table.concat(calls, "\n") | |||
return frame:preprocess(rendered) | |||
end | end | ||