Модуль:GetField: различия между версиями
Pok (обсуждение | вклад) Нет описания правки |
Pok (обсуждение | вклад) Нет описания правки |
||
| Строка 1050: | Строка 1050: | ||
end | end | ||
local function | local function encode_nowiki_json(value) | ||
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 | ||
return to_nowiki(json) | |||
end | end | ||
return nil | |||
end | |||
local function collect_sorted_keys(tbl, stringOnly) | |||
local keys = {} | local keys = {} | ||
for k in pairs( | 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) | ||
return keys | |||
end | end | ||
| Строка 1126: | Строка 1116: | ||
end | end | ||
local function | 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 | if prefix and options.includeJsonAtPrefix then | ||
local json = encode_nowiki_json(value) | |||
if json then | |||
parts[#parts + 1] = prefix .. "=" .. tostring(json) | |||
end | |||
end | end | ||
local keys = collect_sorted_keys(value, false) | |||
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 | local json = encode_nowiki_json(v) | ||
if | if json then | ||
parts[#parts + 1] = key .. "=" .. | parts[#parts + 1] = key .. "=" .. tostring(json) | ||
end | end | ||
end | end | ||
if next(v) ~= nil then | if next(v) ~= nil then | ||
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 | includeJsonAtPrefix = true, | ||
nestedKeyMode = "prefixed", | |||
skipPrimitiveRoot = false, | |||
} | |||
local rawTypeOptions = { | |||
includeJsonAtPrefix = false, | |||
nestedKeyMode = "raw", | |||
skipPrimitiveRoot = true, | |||
} | |||
local function makeCall(obj) | local function makeCall(obj) | ||
| Строка 1197: | Строка 1199: | ||
end | end | ||
local parts = { | 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 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_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 | local json = encode_nowiki_json(v) | ||
if | if json then | ||
parts[#parts + 1] = "value=" .. | parts[#parts + 1] = "value=" .. tostring(json) | ||
end | end | ||
end | end | ||
if next(v) ~= nil then | if next(v) ~= nil then | ||
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_fields(parts, v, nestedOptions, k) | |||
end | end | ||
elseif v ~= nil then | elseif v ~= nil then | ||