Модуль:Песочница/Pok: различия между версиями
Pok (обсуждение | вклад) Нет описания правки |
Pok (обсуждение | вклад) Нет описания правки |
||
| Строка 3: | Строка 3: | ||
local CURRENT_PROJECT | local CURRENT_PROJECT | ||
local function try_call_expand(node) | |||
local function gather_text(x, out) | if type(node) ~= "table" then return nil end | ||
local f = node.expand | |||
if type(f) ~= "function" then return nil end | |||
local ok, res = pcall(function() return f(node) end) | |||
if ok and res and res ~= "" then return res end | |||
ok, res = pcall(function() return f() end) | |||
if ok and res and res ~= "" then return res end | |||
ok, res = pcall(f, node) | |||
if ok and res and res ~= "" then return res end | |||
return nil | |||
end | |||
local function gather_text(x, out, seen) | |||
out = out or {} | out = out or {} | ||
seen = seen or {} | |||
if type(x) == "string" then | |||
table.insert(out, x) | table.insert(out, x) | ||
return out | return out | ||
elseif | elseif type(x) == "number" or type(x) == "boolean" then | ||
table.insert(out, tostring(x)) | table.insert(out, tostring(x)) | ||
return out | return out | ||
elseif | elseif type(x) == "table" then | ||
if seen[x] then return out end | |||
seen[x] = true | |||
local s = try_call_expand(x) | |||
if s and s ~= "" then | |||
table.insert(out, s) | |||
return out | |||
end | |||
if x.text and type(x.text) == "string" and x.text ~= "" then | |||
table.insert(out, x.text) | |||
end | |||
local i = 1 | local i = 1 | ||
while x[i] ~= nil do | while x[i] ~= nil do | ||
gather_text(x[i], out) | gather_text(x[i], out, seen) | ||
i = i + 1 | i = i + 1 | ||
end | end | ||
for k, v in pairs(x) do | for k, v in pairs(x) do | ||
if type(k) ~= "number" then | if type(k) ~= "number" then | ||
if type(v) == "string" or type(v) == "number" or type(v) == "boolean" then | |||
table.insert(out, tostring(v)) | |||
elseif type(v) == "table" then | |||
gather_text(v, out, seen) | |||
end | end | ||
end | end | ||
end | end | ||
return out | return out | ||
end | end | ||
return out | |||
end | end | ||
local function extract_string(v) | local function extract_string(v) | ||
if v == nil then return nil end | if v == nil then return nil end | ||
if type(v) == "string" then if v == "" then return nil end; return v end | |||
if type(v) == "number" or type(v) == "boolean" then return tostring(v) end | |||
if type(v) == "table" then | |||
if | |||
if | |||
local parts = gather_text(v) | local parts = gather_text(v) | ||
if #parts == 0 then return nil end | if #parts == 0 then return nil end | ||
| Строка 61: | Строка 69: | ||
end | end | ||
local function short_serialize(obj, depth, seen) | local function short_serialize(obj, depth, seen) | ||
depth = depth or 0 | depth = depth or 0 | ||
| Строка 67: | Строка 74: | ||
if type(obj) ~= "table" then return tostring(obj) end | if type(obj) ~= "table" then return tostring(obj) end | ||
if seen[obj] then return "<cycle>" end | if seen[obj] then return "<cycle>" end | ||
if depth > | if depth > 2 then return "{...}" end | ||
seen[obj] = true | seen[obj] = true | ||
local parts = {} | local parts = {} | ||
| Строка 85: | Строка 92: | ||
function p.set_path(frame) | function p.set_path(frame) | ||
local raw | local raw | ||
if type(frame) == "table" and frame.args then | if type(frame) == "table" and frame.args then | ||
raw = frame.args[1] or nil | raw = frame.args[1] or nil | ||
if not raw then | if not raw then | ||
local ok, a = pcall(function() return frame:getArgument(1) end) | local ok, a = pcall(function() return frame:getArgument(1) end) | ||
| Строка 107: | Строка 112: | ||
end | end | ||
local diag = "NOT_SET" | local diag = "NOT_SET" | ||
if type(frame) == "table" then | if type(frame) == "table" then | ||