summaryrefslogblamecommitdiff
path: root/docgen/util.lua
blob: 97e2fb047669bbb166928813c6295473a4e5cb31 (plain) (tree)
















































                                                                           

                               
                                                               






















                                                                
        
local cutil = require("cutil")
local ffi = require("ffi")

local M = {}

local doc_root, root_ns

local path_sep, rep_sep = "/", "\\"
if ffi.os == "Windows" then
    path_sep, rep_sep = rep_sep, path_sep
end

M.path_join = function(...)
    return (table.concat({ ... }, path_sep):gsub(rep_sep, path_sep))
end

M.path_to_nspace = function(p)
    return p:gsub(rep_sep, ":"):gsub(path_sep, ":"):lower()
end

M.nspace_to_path = function(ns)
    return ns:gsub(":", path_sep):gsub(rep_sep, path_sep):lower()
end

M.make_page = function(path, ext)
    return M.path_join(doc_root, path .. "." .. ext)
end

M.get_root_ns = function()
    return root_ns
end

M.mkdir_r = function(dirn)
    assert(cutil.file_mkpath(M.path_join(doc_root, dirn)))
end

M.mkdir_p = function(path)
    M.mkdir_r(path:match("(.+)" .. path_sep .. "([^" .. path_sep .. "]+)"))
end

M.rm_root = function()
    cutil.file_rmrf(doc_root)
end

M.init = function(root, rns)
    doc_root = root:gsub(rep_sep, path_sep)
    root_ns = rns
end

M.filter = function(list, pred)
    local ret = {}
    if type(list) == "table" and getmetatable(list) == nil then
        for i, v in ipairs(list) do
            if pred(v) then
                ret[#ret + 1] = v
            end
        end
    else
        for v in list do
            if pred(v) then
                ret[#ret + 1] = v
            end
        end
    end
    return ret
end

M.filter_prop = function(list, prop)
    return M.filter(list, function(v) return v[prop](v) end)
end

M.filter_prop_not = function(list, prop)
    return M.filter(list, function(v) return not v[prop](v) end)
end

return M