Модуль:GetField: различия между версиями

Нет описания правки
мНет описания правки
 
(не показано 6 промежуточных версий этого же участника)
Строка 70: Строка 70:
         return tostring(v)
         return tostring(v)
     end
     end
end
local function to_nowiki(v)
    local s = tostring(v)
    return "<nowiki>" .. s .. "</nowiki>"
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


Строка 196: Строка 214:
             local key = (prefix == "" and tostring(k) or prefix .. "." .. tostring(k))
             local key = (prefix == "" and tostring(k) or prefix .. "." .. tostring(k))
             if type(v) == "table" then
             if type(v) == "table" then
                 walk(v, key)
                 if is_array(v) then
                    local ok, json = pcall(mw.text.jsonEncode, v)
                    if ok and json then
                        parts[#parts + 1] = key .. "=" .. to_nowiki(json)
                    end
                else
                    local ok, json = pcall(mw.text.jsonEncode, v)
                    if ok and json then
                        parts[#parts + 1] = key .. "=" .. to_nowiki(json)
                    end
                    walk(v, key)
                end
             else
             else
                 parts[#parts + 1] = key .. "=" .. tostring(v)
                 parts[#parts + 1] = key .. "=" .. to_nowiki(v)
             end
             end
         end
         end
Строка 204: Строка 233:


     walk(entry, "")
     walk(entry, "")
     return table.concat(parts, "|")
     return table.concat(parts, "|")
end
end
Строка 259: Строка 289:
     end
     end
     tplStr = tplStr .. "}}"
     tplStr = tplStr .. "}}"
    if type(frame.preprocess) == "function" then
        return frame:preprocess(tplStr)
    end


     return tplStr
     return tplStr
Строка 288: Строка 322:
     end
     end


     return table.concat(out, "\n")
     local result = table.concat(out, "\n")
    if type(frame.preprocess) == "function" then
        return frame:preprocess(result)
    end
 
    return result
end
end


return p
return p