Модуль:Сущность: различия между версиями
Pok (обсуждение | вклад) Нет описания правки |
Pok (обсуждение | вклад) Нет описания правки |
||
| Строка 300: | Строка 300: | ||
return frame:preprocess(table.concat(out, "\n")) | return frame:preprocess(table.concat(out, "\n")) | ||
end | |||
local function is_array(tbl) | |||
local max = 0 | |||
local count = 0 | |||
for k in pairs(tbl) do | |||
if type(k) ~= "number" then | |||
return false | |||
end | |||
if k > max then max = k end | |||
count = count + 1 | |||
end | |||
return count > 0 and max == count | |||
end | end | ||
| Строка 315: | Строка 328: | ||
local sep = args.sep or ": " | local sep = args.sep or ": " | ||
local | local out = {} | ||
if is_array(data) then | |||
for _, v in ipairs(data) do | |||
local text = "" | |||
if type(v) == "table" then | |||
if is_array(v) then | |||
if #v == 1 then | |||
text = tostring(v[1]) | |||
elseif #v > 1 then | |||
local parts = {} | |||
for i = 1, #v do | |||
parts[#parts + 1] = tostring(v[i]) | |||
end | |||
text = table.concat(parts, ", ") | |||
end | |||
else | |||
local okJson, jsonVal = pcall(mw.text.jsonEncode, v) | |||
if okJson and jsonVal then | |||
text = jsonVal | |||
end | |||
end | |||
else | else | ||
text = tostring(v) | |||
end | |||
if text ~= "" then | |||
table.insert(out, bullet .. text) | |||
end | end | ||
end | end | ||
if vStr ~= "" then | else | ||
local keys = {} | |||
for k in pairs(data) do | |||
keys[#keys + 1] = k | |||
end | |||
table.sort(keys, function(a, b) return tostring(a) < tostring(b) end) | |||
for _, k in ipairs(keys) do | |||
local v = data[k] | |||
local vStr | |||
if type(v) == "table" then | |||
local okJson, jsonVal = pcall(mw.text.jsonEncode, v) | |||
if okJson and jsonVal then | |||
vStr = jsonVal | |||
else | |||
vStr = "" | |||
end | |||
else | |||
vStr = tostring(v) | |||
end | |||
if vStr ~= "" then | |||
table.insert(out, bullet .. tostring(k) .. sep .. vStr) | |||
end | |||
end | end | ||
end | end | ||