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

Нет описания правки
Нет описания правки
Строка 1050: Строка 1050:
end
end


local function append_table_tree(parts, prefix, value)
local function encode_nowiki_json(value)
if type(value) ~= "table" or next(value) == nil then
return
end
 
local ok, json = pcall(mw.text.jsonEncode, value)
local ok, json = pcall(mw.text.jsonEncode, value)
if ok and json then
if ok and json then
parts[#parts + 1] = prefix .. "=" .. to_nowiki(json)
return to_nowiki(json)
end
end
return nil
end


local function collect_sorted_keys(tbl, stringOnly)
local keys = {}
local keys = {}
for k in pairs(value) do
for k in pairs(tbl) do
keys[#keys + 1] = k
if not stringOnly or type(k) == "string" then
keys[#keys + 1] = k
end
end
end


Строка 1069: Строка 1070:
end)
end)


for _, k in ipairs(keys) do
return keys
local v = value[k]
local key = prefix .. "." .. tostring(k)
 
if type(v) == "table" then
if next(v) ~= nil then
append_table_tree(parts, key, v)
end
else
parts[#parts + 1] = key .. "=" .. tostring(v)
end
end
end
end


Строка 1126: Строка 1116:
end
end


local function append_table_tree_raw(parts, value)
local function append_table_fields(parts, value, options, prefix)
if type(value) ~= "table" or next(value) == nil then
if type(value) ~= "table" or next(value) == nil then
return
return
end
end


if is_array_of_primitives(value) then
if options.skipPrimitiveRoot and is_array_of_primitives(value) then
return
return
end
end


local keys = {}
if prefix and options.includeJsonAtPrefix then
for k in pairs(value) do
local json = encode_nowiki_json(value)
keys[#keys + 1] = k
if json then
parts[#parts + 1] = prefix .. "=" .. tostring(json)
end
end
end


table.sort(keys, function(a, b)
local keys = collect_sorted_keys(value, false)
return tostring(a) < tostring(b)
end)


for _, k in ipairs(keys) do
for _, k in ipairs(keys) do
local v = value[k]
local v = value[k]
local key = tostring(k)
local key
if prefix and options.nestedKeyMode == "prefixed" then
key = prefix .. "." .. tostring(k)
else
key = tostring(k)
end


if type(v) == "table" then
if type(v) == "table" then
if is_array_of_primitives(v) then
if is_array_of_primitives(v) then
local okJson, jsonVal = pcall(mw.text.jsonEncode, v)
local json = encode_nowiki_json(v)
if okJson and jsonVal then
if json then
parts[#parts + 1] = key .. "=" .. to_nowiki(jsonVal)
parts[#parts + 1] = key .. "=" .. tostring(json)
end
end
end
end


if next(v) ~= nil then
if next(v) ~= nil then
append_table_tree_raw(parts, v)
local childPrefix = (options.nestedKeyMode == "prefixed") and key or nil
append_table_fields(parts, v, options, childPrefix)
end
end
else
else
Строка 1182: Строка 1178:


local calls = {}
local calls = {}
 
local nestedOptions = {
local function makeTemplatePrefix()
includeJsonAtPrefix = true,
return "{{Шаблон:" .. resolve_template_path(tplPath)
nestedKeyMode = "prefixed",
end
skipPrimitiveRoot = false,
}
local rawTypeOptions = {
includeJsonAtPrefix = false,
nestedKeyMode = "raw",
skipPrimitiveRoot = true,
}


local function makeCall(obj)
local function makeCall(obj)
Строка 1197: Строка 1199:
end
end


local parts = { makeTemplatePrefix() }
local parts = { "{{Шаблон:" .. resolve_template_path(tplPath) }


if tplArgs ~= "" then
if tplArgs ~= "" then
Строка 1204: Строка 1206:


parts[#parts + 1] = tostring(idKey)
parts[#parts + 1] = tostring(idKey)
 
local keys = collect_sorted_keys(obj, true)
local keys = {}
for k in pairs(obj) do
if type(k) == "string" then
keys[#keys + 1] = k
end
end
 
table.sort(keys, function(a, b)
return tostring(a) < tostring(b)
end)


for _, k in ipairs(keys) do
for _, k in ipairs(keys) do
Строка 1222: Строка 1214:
if type(k) == "string" and string.sub(k, 1, 6) == "!type:" then
if type(k) == "string" and string.sub(k, 1, 6) == "!type:" then
if type(v) == "table" then
if type(v) == "table" then
append_table_tree_raw(parts, v)
append_table_fields(parts, v, rawTypeOptions, nil)
elseif v ~= nil then
elseif v ~= nil then
parts[#parts + 1] = "value=" .. tostring(v)
parts[#parts + 1] = "value=" .. tostring(v)
Строка 1228: Строка 1220:
elseif type(v) == "table" then
elseif type(v) == "table" then
if is_array_of_primitives(v) then
if is_array_of_primitives(v) then
local okJson, jsonVal = pcall(mw.text.jsonEncode, v)
local json = encode_nowiki_json(v)
if okJson and jsonVal then
if json then
parts[#parts + 1] = "value=" .. to_nowiki(jsonVal)
parts[#parts + 1] = "value=" .. tostring(json)
end
end
end
end


if next(v) ~= nil then
if next(v) ~= nil then
append_table_tree(parts, k, v)
append_table_fields(parts, v, nestedOptions, k)
end
end
elseif v ~= nil then
elseif v ~= nil then
Строка 1243: Строка 1235:
if type(v) == "table" then
if type(v) == "table" then
if next(v) ~= nil then
if next(v) ~= nil then
append_table_tree(parts, k, v)
append_table_fields(parts, v, nestedOptions, k)
end
end
elseif v ~= nil then
elseif v ~= nil then