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

Нет описания правки
Нет описания правки
 
(не показаны 2 промежуточные версии этого же участника)
Строка 3: Строка 3:


local entityCache = {}
local entityCache = {}
local namespaces = {
Goob = true,
CM = true,
Fallout = true,
Frontier = true,
SW = true,
Wega = true,
WL = true,
}


local function trim(value)
local function trim(value)
    return mw.text.trim(value or "")
return mw.text.trim(value or "")
end
 
local function getCurrentGitNamespace()
local title = mw.title.getCurrentTitle()
if not title then
return ""
end
 
local ns = trim(title.nsText)
if namespaces[ns] then
return ns
end
 
return ""
end
end


local function normalizePath(path)
local function normalizePath(path)
    path = trim(path)
path = trim(path)


    if path == "Corvax" then
if path == "" or path == "Corvax" then
        return ""
return getCurrentGitNamespace()
    end
end


    return path
return path
end
end


local function resolvePath(path)
local function resolvePath(path)
    path = trim(path)
path = trim(path)
    if path ~= "" then
if path ~= "" then
        return normalizePath(path)
return normalizePath(path)
    end
end


    local frame = mw.getCurrentFrame()
local frame = mw.getCurrentFrame()
    if not frame then
if not frame then
        return ""
return ""
    end
end


    local args = getArgs(frame)
local args = getArgs(frame)
    path = trim(args.path or "")
path = trim(args.path or "")


    if path == "" then
if path == "" then
        path = trim(frame:preprocess("{{#var:JsonPath}}") or "")
path = trim(frame:preprocess("{{#var:JsonPath}}") or "")
    end
end


    return normalizePath(path)
return normalizePath(path)
end
end


local function isFrameLike(value)
local function isFrameLike(value)
    return type(value) == "table" and type(value.getParent) == "function"
return type(value) == "table" and type(value.getParent) == "function"
end
end


local function getPathFromCall(call)
local function getPathFromCall(call)
    if type(call) == "string" then
if type(call) == "string" then
        return resolvePath(call)
return resolvePath(call)
    end
end


    if isFrameLike(call) then
if isFrameLike(call) then
        local args = getArgs(call)
local args = getArgs(call)
        return resolvePath(args.path or args[1])
return resolvePath(args.path or args[1])
    end
end


    if type(call) == "table" then
if type(call) == "table" then
        return resolvePath(call.path or call[1])
return resolvePath(call.path or call[1])
    end
end


    return resolvePath("")
return resolvePath("")
end
end


function p.project(call)
function p.project(call)
    return getPathFromCall(call)
return getPathFromCall(call)
end
end


function p.prefix(call)
function p.prefix(call)
    local path = getPathFromCall(call)
local path = getPathFromCall(call)


    if path ~= "" then
if path ~= "" then
        return path .. ":"
return path .. ":"
    end
end


    return ""
return ""
end
end


Строка 99: Строка 123:


local function getEntityDataTitle(path)
local function getEntityDataTitle(path)
    path = resolvePath(path)
path = resolvePath(path)
    if path == "" then
if path == "" then
        return ""
return ""
    end
end


    return "Модуль:IanComradeBot/" .. path .. "/entity project.json/data"
return "Модуль:IanComradeBot/" .. path .. "/entity project.json/data"
end
end


local function loadEntityIds(path)
local function loadEntityIds(path)
    path = resolvePath(path)
path = resolvePath(path)
    if path == "" then
if path == "" then
        return nil
return nil
    end
end


    if entityCache[path] ~= nil then
if entityCache[path] ~= nil then
        return entityCache[path]
return entityCache[path]
    end
end


    local title = getEntityDataTitle(path)
local title = getEntityDataTitle(path)
    local ok, data = pcall(mw.loadData, title)
local ok, data = pcall(mw.loadData, title)


    if not ok or type(data) ~= "table" then
if not ok or type(data) ~= "table" then
        entityCache[path] = {}
entityCache[path] = {}
        return entityCache[path]
return entityCache[path]
    end
end


    entityCache[path] = data
entityCache[path] = data
    return data
return data
end
end


local function arrayContains(list, value)
local function arrayContains(list, value)
    if type(list) ~= "table" then
if type(list) ~= "table" then
        return false
return false
    end
end


    for _, item in ipairs(list) do
for _, item in ipairs(list) do
        if item == value then
if item == value then
            return true
return true
        end
end
    end
end


    return false
return false
end
end


function p.has(id, path)
function p.has(id, path)
    if isFrameLike(id) then
if isFrameLike(id) then
        local args = getArgs(id)
local args = getArgs(id)
        id = args[1] or args.id
id = args[1] or args.id
        path = args.path or args[2]
path = args.path or args[2]
    end
end


    id = trim(id)
id = trim(id)
    if id == "" then
if id == "" then
        return ""
return ""
    end
end


    path = resolvePath(path)
path = resolvePath(path)
    if path == "" then
if path == "" then
        return ""
return ""
    end
end


    local ids = loadEntityIds(path)
local ids = loadEntityIds(path)
    if arrayContains(ids, id) then
if arrayContains(ids, id) then
        return true
return true
    end
end


    return false
return false
end
end


function p.get(rel, path)
function p.get(rel, path)
    if isFrameLike(rel) then
if isFrameLike(rel) then
        local args = getArgs(rel)
local args = getArgs(rel)
        rel = args[1] or args.rel
rel = args[1] or args.rel
        path = args.path or args[2]
path = args.path or args[2]
    end
end
 
rel = trim(rel)
if rel == "" then
return ""
end
 
path = resolvePath(path)


    rel = trim(rel)
local prefix = "Module:IanComradeBot/"
    if rel == "" then
if path ~= "" then
        return ""
prefix = prefix .. path .. "/"
    end
end


    path = resolvePath(path)
return prefix .. rel .. "/data"
end


    local prefix = "Module:IanComradeBot/"
function p.getJson(rel, path)
    if path ~= "" then
local modulePath = p.get(rel, path)
        prefix = prefix .. path .. "/"
if modulePath == "" then
    end
return ""
end


    return prefix .. rel .. "/data"
local jsonPath = modulePath:gsub("^Module:IanComradeBot/", "Участник:IanComradeBot/", 1)
return (jsonPath:gsub("/data$", ""))
end
end


function p.git(call)
function p.git(call)
    local path = getPathFromCall(call)
local path = getPathFromCall(call)


    if path == "Goob" then
if path == "Goob" then
        return "https://github.com/space-syndicate/Goob-Station/tree/master"
return "https://github.com/space-syndicate/Goob-Station/tree/master"
    elseif path == "CM" then
elseif path == "CM" then
        return "???"
return "https://github.com/Forge-Station/Colonial-Marine/tree/master"
    elseif path == "Fallout" then
elseif path == "Fallout" then
        return "https://github.com/Forge-Station/nuclear-14/tree/master"
return "https://github.com/Forge-Station/nuclear-14/tree/master"
    elseif path == "Frontier" then
elseif path == "Frontier" then
        return "https://github.com/Forge-Station/Frontier/tree/master"
return "https://github.com/Forge-Station/Frontier/tree/master"
    elseif path == "SW" then
elseif path == "SW" then
        return "???"
return "???"
    elseif path == "Wega" then
elseif path == "Wega" then
        return "https://github.com/wega-team/ss14-weg/tree/master"
return "https://github.com/wega-team/ss14-weg/tree/master"
    elseif path == "WL" then
elseif path == "WL" then
        return "https://github.com/corvax-team/ss14-wl/tree/master"
return "https://github.com/corvax-team/ss14-wl/tree/master"
    end
end


    return "https://github.com/space-syndicate/space-station-14/tree/master"
return "https://github.com/space-syndicate/space-station-14/tree/master"
end
end


return p
return p