Compare commits

..

No commits in common. "master" and "v1.3.1" have entirely different histories.

23 changed files with 62 additions and 114 deletions

View file

@ -1,8 +1,5 @@
return { return {
_all = { _all = {
lpath = "?.lua;?/init.lua" lpath = "?.lua;?/init.lua"
}; }
core = {
recursive = false
};
} }

View file

@ -1,2 +0,0 @@
[*]
indent_style = tab

View file

@ -1,3 +0,0 @@
{
"workspace.checkThirdParty": false
}

30
glass-dev-1.rockspec Normal file
View file

@ -0,0 +1,30 @@
package = "glass"
version = "dev-1"
source = {
url = "git+https://github.com/darkwiiplayer/glass"
}
description = {
homepage = "https://github.com/darkwiiplayer/glass",
license = "Unlicense"
}
dependencies = {
"luafilesystem"
}
build = {
type = "builtin",
modules = {
glass = "glass.lua",
["glass.cosmo"] = "glass/cosmo.lua",
["glass.csv"] = "glass/csv.lua",
["glass.discount"] = "glass/discount.lua",
["glass.environment"] = "glass/environment.lua",
["glass.error"] = "glass/error.lua",
["glass.json"] = "glass/json.lua",
["glass.lua"] = "glass/lua.lua",
["glass.raw"] = "glass/raw.lua",
["glass.skooma.html"] = "glass/skooma/html.lua",
["glass.skooma.xml"] = "glass/skooma/xml.lua",
["glass.table"] = "glass/table.lua",
["glass.yaml"] = "glass/yaml.lua",
}
}

View file

@ -1,32 +0,0 @@
package = "glass"
version = "dev-2"
source = {
url = "git+https://github.com/darkwiiplayer/glass"
}
description = {
summary = "A library to load configuration files by accessing tables",
homepage = "https://github.com/darkwiiplayer/glass",
license = "Unlicense"
}
dependencies = {
"luafilesystem"
}
build = {
type = "builtin",
modules = {
glass = "glass.lua",
["glass.cosmo"] = "glass/cosmo.lua",
["glass.csv"] = "glass/csv.lua",
["glass.discount"] = "glass/discount.lua",
["glass.environment"] = "glass/environment.lua",
["glass.error"] = "glass/error.lua",
["glass.fennel"] = "glass/fennel.lua",
["glass.json"] = "glass/json.lua",
["glass.lua"] = "glass/lua.lua",
["glass.raw"] = "glass/raw.lua",
["glass.skooma.html"] = "glass/skooma/html.lua",
["glass.skooma.xml"] = "glass/skooma/xml.lua",
["glass.table"] = "glass/table.lua",
["glass.yaml"] = "glass/yaml.lua",
}
}

View file

@ -21,8 +21,8 @@ function __metatable:__index(index)
return config.bind(path, self.__loaders) return config.bind(path, self.__loaders)
else else
for _, loader in ipairs(self.__loaders) do for _, loader in ipairs(self.__loaders) do
local success, result = loader(path) local result = loader(path)
if success then if result then
rawset(self, index, result) rawset(self, index, result)
return result return result
end end

View file

@ -12,6 +12,8 @@ return function(name)
name = tostring(name) .. '.cosmo' name = tostring(name) .. '.cosmo'
local text = raw(name) local text = raw(name)
if text then if text then
return true, assert(cosmo.compile(text, name)) return assert(cosmo.compile(text, name))
else
return nil
end end
end end

View file

@ -10,6 +10,6 @@ local csv = require 'streamcsv'
return function(name) return function(name)
local file = io.open(name..'.csv') local file = io.open(name..'.csv')
if file then if file then
return true, csv.file(file) return csv.file(file)
end end
end end

View file

@ -14,8 +14,10 @@ return function(name)
local file = io.open(name) local file = io.open(name)
if file then if file then
local html = discount(file:read("*a")) local html = discount(file:read("*a"))
return true, function() return function()
return html return html
end end
else
return nil
end end
end end

View file

@ -4,5 +4,5 @@
--- Loads values from environment variables --- Loads values from environment variables
-- @treturn string The value of the environment variable -- @treturn string The value of the environment variable
return function(name) return function(name)
return true, os.getenv(name:match("[^/].+$")) return os.getenv(name:match("[^/].+$"))
end end

View file

@ -1,18 +0,0 @@
--- Loader for Fennel files
-- @module glass.fennel
local fennel = require "fennel"
local read = require 'glass.raw'
--- Loads and compiles a Fennel file and runs it.
-- Returns the result of running the Fennel file.
-- If running the code immediately is not desired,
-- it has to be returned as a function.
-- @return The result of the Fennel file after running it
-- @function load
return function(file)
local success, raw = read(file..'.fnl')
if success then
return true, fennel.eval(raw)
end
end

View file

@ -11,8 +11,8 @@ local read = require 'glass.raw'
-- @treturn table JSON-Data -- @treturn table JSON-Data
-- @function load -- @function load
return function(file) return function(file)
local success, raw = read(file..'.json') local raw = read(file..'.json')
if success then if raw then
return true, json.decode(raw) return json.decode(raw)
end end
end end

View file

@ -9,7 +9,5 @@
-- @function load -- @function load
return function(name) return function(name)
local f = loadfile(name..'.lua') local f = loadfile(name..'.lua')
if f then return f and f() or nil
return true, f()
end
end end

View file

@ -1,6 +1,7 @@
--- Loader for MoonHTML files --- Loader for MoonHTML files
-- @module glass.moonhtml -- @module glass.moonhtml
local template = require 'restia.template' local template = require 'restia.template'
--- Loads and compiles a moonhtml template. --- Loads and compiles a moonhtml template.
@ -10,7 +11,7 @@ return function(name)
name = tostring(name) .. '.moonhtml' name = tostring(name) .. '.moonhtml'
local file = io.open(name) local file = io.open(name)
if file then if file then
return true, assert(template.loadmoon(file:read("*a"), name)) return assert(template.loadmoon(file:read("*a"), name))
else else
return nil return nil
end end

View file

@ -6,10 +6,8 @@
-- @function load -- @function load
return function(path) return function(path)
local f = io.open(path) local f = io.open(path)
if not f then if not f then return end
return false
end
local result = f:read("*a") local result = f:read("*a")
f:close() f:close()
return true, result return result
end end

View file

@ -17,12 +17,9 @@ end})
-- @function load -- @function load
return function(name) return function(name)
name = tostring(name)..'.html.skooma' name = tostring(name)..'.html.skooma'
local template, err = loadfile(name, "t", env) local template = loadfile(name, "t", env)
if template == nil then
return nil, err
end
if setfenv then if setfenv then
setfenv(template, env) setfenv(template, env)
end end
return true, template return template
end end

View file

@ -17,12 +17,9 @@ end})
-- @function load -- @function load
return function(name) return function(name)
name = tostring(name)..'.xml.skooma' name = tostring(name)..'.xml.skooma'
local template, err = loadfile(name, "t", env) local template = loadfile(name, "t", env)
if template == nil then
return nil, err
end
if setfenv then if setfenv then
setfenv(template, env) setfenv(template, env)
end end
return true, template return template
end end

View file

@ -7,9 +7,6 @@
-- @treturn function A loader function to be used with `glass.bind` -- @treturn function A loader function to be used with `glass.bind`
return function(input) return function(input)
return function(name) return function(name)
local result = input[name:match("[^/].+$")] return input[name:match("[^/].+$")]
if result then
return true, result
end
end end
end end

View file

@ -11,11 +11,8 @@ local read = require 'glass.raw'
-- @treturn table YAML-Data -- @treturn table YAML-Data
-- @function load -- @function load
return function(file) return function(file)
local success, raw = read(file..'.yml') local raw = read(file..'.yml') or read(file..'.yaml')
if not success then if raw then
success, raw = read(file..'.yaml') return yaml.load(raw)
end
if success then
return true, yaml.load(raw)
end end
end end

View file

@ -31,7 +31,6 @@ Glass offers the following loaders out of the box:
* `csv` loads csv files (with header line) * `csv` loads csv files (with header line)
* `discount` loads markdown files\*. * `discount` loads markdown files\*.
* `environment` loads environment variables. * `environment` loads environment variables.
* `fennel` loads and executes fennel files.
* `json` loads a JSON file as a Lua table. (requires `cjson`) * `json` loads a JSON file as a Lua table. (requires `cjson`)
* `lua` loads and executes a Lua file. * `lua` loads and executes a Lua file.
* `moonhtml` loads a MoonHTML template and returns it as a function. * `moonhtml` loads a MoonHTML template and returns it as a function.
@ -54,8 +53,6 @@ file and attempts to load it into a Lua value.
Loaders will typically add an extension to the given file name before checking Loaders will typically add an extension to the given file name before checking
whether that file exists and can be loaded. whether that file exists and can be loaded.
A successful loader should return `true` followed by its result. When a loader cannot find the expected file, it should return `nil` to let glass
When a loader can't load a key, it should return `false` and an optional continue the loader chain. When a loader returns a truthy value, this will be
description of why. used and no further loaders will be tried.
When a loader encounters an error (file found but can't be parsed), it should
error.

View file

@ -1,6 +0,0 @@
describe 'fennel loader', ->
before_each -> export loader = require 'glass.fennel'
it 'loads Fennel files', ->
success, result = loader 'spec/fixtures/test'
assert.true success
assert.same { foo: 'bar', tab: {} }, result

View file

@ -1,2 +0,0 @@
{"foo" "bar"
"tab" []}

View file

@ -1,11 +1,9 @@
describe 'raw loader', -> describe 'raw loader', ->
before_each -> export loader = require 'glass.raw' before_each -> export loader = require 'glass.raw'
it 'loads files as plain text', -> it 'loads files as plain text', ->
assert.same {true, 'plain text\n'}, {loader 'spec/fixtures/test'} assert.same 'plain text\n', loader 'spec/fixtures/test'
describe 'lua loader', -> describe 'lua loader', ->
before_each -> export loader = require 'glass.lua' before_each -> export loader = require 'glass.lua'
it 'loads Lua files', -> it 'loads Lua files', ->
success, result = loader 'spec/fixtures/test' assert.same { foo: 'bar', tab: {} }, loader 'spec/fixtures/test'
assert.true success
assert.same { foo: 'bar', tab: {} }, result