Модуль:Сущность: различия между версиями

Нет описания правки
Метка: ручная отмена
мНет описания правки
Строка 299: Строка 299:
     for _, b in ipairs(blocks) do table.insert(out, b) end
     for _, b in ipairs(blocks) do table.insert(out, b) end


     return frame:preprocess(table.concat(out, "\n"))
     return frame:preprocess(table.concat(out, "\n\n"))
end
 
function p.jsonList(frame)
    local args = getArgs(frame, { removeBlanks = false })
    local jsonStr = trim(args[1] or args.json or "")
    if jsonStr == "" then return "" end
 
    local ok, data = pcall(mw.text.jsonDecode, jsonStr)
    if not ok or type(data) ~= "table" then
        return ""
    end
 
    local bullet = args.prefix or "* "
    local sep = args.sep or ": "
 
    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)
 
    local out = {}
    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
 
    return table.concat(out, "\n")
end
end


return p
return p