Provide Support for Fennel

This commit does not alter the functionality of lovebird if fennel is not present.

Introduced a new function lovebird.loadstringAlt. By default it points to the same function as lovebird.loadstring, but if fennel is not nil it points to  a new function described below. loadstringAlt is only used in the "index" page.

loadstringAlt

If fennel is not nil in the global space, opt to use fennel.compile-string and loadstring in place of raw loadstring

If fennel is not nil, always print the result using fennel.view.
This commit is contained in:
Alexander Griffith 2021-05-08 03:39:07 -04:00
parent e84abe7b56
commit b7171e895d

View File

@ -9,9 +9,10 @@
local socket = require "socket" local socket = require "socket"
local lovebird = { _version = "0.4.3" } local lovebird = { _version = "0.4.4" }
lovebird.loadstring = loadstring or load lovebird.loadstring = loadstring or load
lovebird.loadstringAlt = lovebird.loadString
lovebird.inited = false lovebird.inited = false
lovebird.host = "*" lovebird.host = "*"
lovebird.buffer = "" lovebird.buffer = ""
@ -40,7 +41,7 @@ if req.parsedbody.input then
if str:find("^=") then if str:find("^=") then
str = "print(" .. str:sub(2) .. ")" str = "print(" .. str:sub(2) .. ")"
end end
xpcall(function() assert(lovebird.loadstring(str, "input"))() end, xpcall(function() assert(lovebird.loadstringAlt(str, "input"))() end,
lovebird.onerror) lovebird.onerror)
end end
?> ?>
@ -426,7 +427,14 @@ lovebird.pages["env.json"] = [[
} }
]] ]]
if (fennel) then
lovebird.loadstringAlt = function (str, other)
local compiled = fennel["compile-string"](str)
local ret = loadstring(compiled)
print (fennel.view(ret()))
return ret
end
end
function lovebird.init() function lovebird.init()
-- Init server -- Init server