File: load.lua

package info (click to toggle)
luakit 2012.09.13-r1-8
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 1,160 kB
  • ctags: 1,276
  • sloc: ansic: 6,086; makefile: 153; ruby: 79; sh: 38
file content (54 lines) | stat: -rw-r--r-- 1,365 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
-- Get lua environment
local io = require "io"
local assert = assert
local string = string
local print = print
local setmetatable = setmetatable
local type = type

-- Get luakit environment
local capi = {
    luakit = luakit
}

module("lousy.load")

-- Keep loaded resources in memory
local data = {}

local function load_resource(path, memorize)
    -- Have we already loaded this resource?
    if memorize and data[path] then
        return data[path]
    end
    -- Attempt to open & read resource
    local file = io.open(path)
    if file then
        -- Read resource
        local dat = file:read("*a")
        file:close()
        -- Memorize if asked
        if memorize then data[path] = dat end
        -- Return file contents
        return dat
    end
end

local function search_load(path, memorize)
    assert(type(path) == "string", "invalid path")
    memorize = not not memorise

    if string.sub(path, 1, 1) ~= "/" then
        -- Can we search relative paths?
        if capi.luakit.dev_paths then
            local dat = load_resource("./"..path, memorize)
            if dat then return dat end
        end
        path = capi.luakit.install_path.."/"..path
    end

    return assert(load_resource(path, memorize),
        "unable to load resource: " .. path)
end

setmetatable(_M, { __call = function (_, ...) return search_load(...) end })