Модуль:JsonPaths: различия между версиями
Материал из Space Station 14 Вики
Pok (обсуждение | вклад) Нет описания правки |
Pok (обсуждение | вклад) Нет описания правки |
||
| (не показано 7 промежуточных версий этого же участника) | |||
| Строка 1: | Строка 1: | ||
local p = {} | local p = {} | ||
local getArgs = require('Module:Arguments').getArgs | local getArgs = require('Module:Arguments').getArgs | ||
local entityCache = {} | |||
local function trim(value) | |||
return mw.text.trim(value or "") | |||
end | |||
local function normalizePath(path) | |||
path = trim(path) | |||
if path == "Corvax" then | |||
return "" | |||
end | |||
return path | |||
end | |||
local function resolvePath(path) | local function resolvePath(path) | ||
if | path = trim(path) | ||
return path | if path ~= "" then | ||
return normalizePath(path) | |||
end | end | ||
| Строка 13: | Строка 30: | ||
local args = getArgs(frame) | local args = getArgs(frame) | ||
path = args.path or "" | path = trim(args.path or "") | ||
if path == "" then | |||
path = trim(frame:preprocess("{{#var:JsonPath}}") or "") | |||
end | |||
return normalizePath(path) | |||
end | |||
local function isFrameLike(value) | |||
return type(value) == "table" and type(value.getParent) == "function" | |||
end | |||
local function getPathFromCall(call) | |||
if type(call) == "string" then | |||
return resolvePath(call) | |||
end | |||
if isFrameLike(call) then | |||
local args = getArgs(call) | |||
return resolvePath(args.path or args[1]) | |||
end | |||
if type(call) == "table" then | |||
return resolvePath(call.path or call[1]) | |||
end | |||
return resolvePath("") | |||
end | |||
function p.project(call) | |||
return getPathFromCall(call) | |||
end | |||
if path == " | function p.prefix(call) | ||
path = "" | local path = getPathFromCall(call) | ||
if path ~= "" then | |||
return path .. ":" | |||
end | |||
return "" | |||
end | |||
local function getEntityDataTitle(path) | |||
path = resolvePath(path) | |||
if path == "" then | |||
return "" | |||
end | |||
return "Модуль:IanComradeBot/" .. path .. "/entity project.json/data" | |||
end | |||
local function loadEntityIds(path) | |||
path = resolvePath(path) | |||
if path == "" then | |||
return nil | |||
end | |||
if entityCache[path] ~= nil then | |||
return entityCache[path] | |||
end | |||
local title = getEntityDataTitle(path) | |||
local ok, data = pcall(mw.loadData, title) | |||
if not ok or type(data) ~= "table" then | |||
entityCache[path] = {} | |||
return entityCache[path] | |||
end | |||
entityCache[path] = data | |||
return data | |||
end | |||
local function arrayContains(list, value) | |||
if type(list) ~= "table" then | |||
return false | |||
end | |||
for _, item in ipairs(list) do | |||
if item == value then | |||
return true | |||
end | |||
end | |||
return false | |||
end | |||
function p.has(id, path) | |||
if isFrameLike(id) then | |||
local args = getArgs(id) | |||
id = args[1] or args.id | |||
path = args.path or args[2] | |||
end | |||
id = trim(id) | |||
if id == "" then | |||
return "" | |||
end | |||
path = resolvePath(path) | |||
if path == "" then | |||
return "" | |||
end | |||
local ids = loadEntityIds(path) | |||
if arrayContains(ids, id) then | |||
return true | |||
end | end | ||
return | return false | ||
end | end | ||
function p.get(rel, path) | function p.get(rel, path) | ||
rel = | if isFrameLike(rel) then | ||
local args = getArgs(rel) | |||
rel = args[1] or args.rel | |||
path = args.path or args[2] | |||
end | |||
rel = trim(rel) | |||
if rel == "" then | if rel == "" then | ||
return "" | return "" | ||
end | end | ||
path = | path = resolvePath(path) | ||
local prefix = "Module:IanComradeBot/" | local prefix = "Module:IanComradeBot/" | ||
| Строка 36: | Строка 164: | ||
return prefix .. rel .. "/data" | return prefix .. rel .. "/data" | ||
end | |||
function p.git(call) | |||
local path = getPathFromCall(call) | |||
if path == "Goob" then | |||
return "https://github.com/space-syndicate/Goob-Station/tree/master" | |||
elseif path == "CM" then | |||
return "???" | |||
elseif path == "Fallout" then | |||
return "https://github.com/Forge-Station/nuclear-14/tree/master" | |||
elseif path == "Frontier" then | |||
return "https://github.com/Forge-Station/Frontier/tree/master" | |||
elseif path == "SW" then | |||
return "???" | |||
elseif path == "Wega" then | |||
return "https://github.com/wega-team/ss14-weg/tree/master" | |||
elseif path == "WL" then | |||
return "https://github.com/corvax-team/ss14-wl/tree/master" | |||
end | |||
return "https://github.com/space-syndicate/space-station-14/tree/master" | |||
end | end | ||
return p | return p | ||
Текущая версия от 01:43, 22 марта 2026
Для документации этого модуля может быть создана страница Модуль:JsonPaths/doc
local p = {}
local getArgs = require('Module:Arguments').getArgs
local entityCache = {}
local function trim(value)
return mw.text.trim(value or "")
end
local function normalizePath(path)
path = trim(path)
if path == "Corvax" then
return ""
end
return path
end
local function resolvePath(path)
path = trim(path)
if path ~= "" then
return normalizePath(path)
end
local frame = mw.getCurrentFrame()
if not frame then
return ""
end
local args = getArgs(frame)
path = trim(args.path or "")
if path == "" then
path = trim(frame:preprocess("{{#var:JsonPath}}") or "")
end
return normalizePath(path)
end
local function isFrameLike(value)
return type(value) == "table" and type(value.getParent) == "function"
end
local function getPathFromCall(call)
if type(call) == "string" then
return resolvePath(call)
end
if isFrameLike(call) then
local args = getArgs(call)
return resolvePath(args.path or args[1])
end
if type(call) == "table" then
return resolvePath(call.path or call[1])
end
return resolvePath("")
end
function p.project(call)
return getPathFromCall(call)
end
function p.prefix(call)
local path = getPathFromCall(call)
if path ~= "" then
return path .. ":"
end
return ""
end
local function getEntityDataTitle(path)
path = resolvePath(path)
if path == "" then
return ""
end
return "Модуль:IanComradeBot/" .. path .. "/entity project.json/data"
end
local function loadEntityIds(path)
path = resolvePath(path)
if path == "" then
return nil
end
if entityCache[path] ~= nil then
return entityCache[path]
end
local title = getEntityDataTitle(path)
local ok, data = pcall(mw.loadData, title)
if not ok or type(data) ~= "table" then
entityCache[path] = {}
return entityCache[path]
end
entityCache[path] = data
return data
end
local function arrayContains(list, value)
if type(list) ~= "table" then
return false
end
for _, item in ipairs(list) do
if item == value then
return true
end
end
return false
end
function p.has(id, path)
if isFrameLike(id) then
local args = getArgs(id)
id = args[1] or args.id
path = args.path or args[2]
end
id = trim(id)
if id == "" then
return ""
end
path = resolvePath(path)
if path == "" then
return ""
end
local ids = loadEntityIds(path)
if arrayContains(ids, id) then
return true
end
return false
end
function p.get(rel, path)
if isFrameLike(rel) then
local args = getArgs(rel)
rel = args[1] or args.rel
path = args.path or args[2]
end
rel = trim(rel)
if rel == "" then
return ""
end
path = resolvePath(path)
local prefix = "Module:IanComradeBot/"
if path ~= "" then
prefix = prefix .. path .. "/"
end
return prefix .. rel .. "/data"
end
function p.git(call)
local path = getPathFromCall(call)
if path == "Goob" then
return "https://github.com/space-syndicate/Goob-Station/tree/master"
elseif path == "CM" then
return "???"
elseif path == "Fallout" then
return "https://github.com/Forge-Station/nuclear-14/tree/master"
elseif path == "Frontier" then
return "https://github.com/Forge-Station/Frontier/tree/master"
elseif path == "SW" then
return "???"
elseif path == "Wega" then
return "https://github.com/wega-team/ss14-weg/tree/master"
elseif path == "WL" then
return "https://github.com/corvax-team/ss14-wl/tree/master"
end
return "https://github.com/space-syndicate/space-station-14/tree/master"
end
return p