Модуль:Песочница/Pok: различия между версиями

мНет описания правки
Нет описания правки
Строка 12: Строка 12:


local function parse_indexed_part(part)
local function parse_indexed_part(part)
-- supports "key", "key[1]" and numeric "1" forms
local key, idx = string.match(part, "^(.-)%[(%d+)%]$")
local key, idx = string.match(part, "^(.-)%[(%d+)%]$")
if key then
if key then
return key, tonumber(idx)
return key, tonumber(idx)
end
end
-- numeric-only part (array index)
local num = tonumber(part)
local num = tonumber(part)
if num then
if num then
Строка 32: Строка 30:
local key, idx = parse_indexed_part(part)
local key, idx = parse_indexed_part(part)
if key and key ~= "" then
if key and key ~= "" then
cur = cur[key]
local nextCur = nil
if type(cur) == "table" then
nextCur = cur[key]
if nextCur == nil then
nextCur = cur["!type:" .. key]
end
end
cur = nextCur
end
end
if idx then
if idx then
-- Lua tables from mw.loadData are 1-based for arrays, so we use the index directly.
if type(cur) ~= "table" then
if type(cur) ~= "table" then
return nil
return nil
Строка 53: Строка 57:
for _ in pairs(t) do
for _ in pairs(t) do
i = i + 1
i = i + 1
-- if any key is not a number then not a pure array
end
end
-- We'll treat table as array if all keys are numeric 1..n
local n = 0
local n = 0
for k in pairs(t) do
for k in pairs(t) do
Строка 72: Строка 74:
return tostring(v)
return tostring(v)
elseif t == "table" then
elseif t == "table" then
-- array-like: join with commas
if is_array(v) then
if is_array(v) then
local out = {}
local out = {}
Строка 82: Строка 83:
return table.concat(out, ", ")
return table.concat(out, ", ")
else
else
-- associative table: "k: v" pairs joined by ", "
local out = {}
local out = {}
for k, val in pairs(v) do
for k, val in pairs(v) do
Строка 94: Строка 94:
end
end


-- Public entry point for invocation
function p.get(frame)
function p.get(frame)
local args = frame.args or {}
local args = frame.args or {}
Строка 101: Строка 100:
local keyPath = args[3] or ""
local keyPath = args[3] or ""


-- Build module load path. The user requested appending to IanComradeBot/
local baseUser = "User:IanComradeBot/"
local baseUser = "IanComradeBot/"
local moduleName = "Module:" .. baseUser .. pagePath .. "/data"
local moduleName = "Module:" .. baseUser .. pagePath .. "/data"


-- Try load
local ok, data = pcall(mw.loadData, moduleName)
local ok, data = pcall(mw.loadData, moduleName)
if not ok or not data then
if not ok or not data then
-- Fallback: try using pagePath directly as a Module name (in case the caller passed full path)
return ""
local altModuleName = "Module:" .. pagePath .. "/data"
ok, data = pcall(mw.loadData, altModuleName)
if not ok or not data then
return ""
end
end
end


-- Find by id, fallback to default
local entry = nil
local entry = nil
if id ~= "" then
if id ~= "" then
Строка 129: Строка 120:


if keyPath == "" then
if keyPath == "" then
-- If no keyPath provided, return formatted entire entry
return format_value(entry)
return format_value(entry)
end
end