Skip to content
This repository has been archived by the owner on Nov 20, 2020. It is now read-only.

Commit

Permalink
support for callable selectors that are not functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabio Mascarenhas committed Oct 22, 2009
1 parent 0769f3a commit adfeebc
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
16 changes: 11 additions & 5 deletions src/cosmo.lua
Expand Up @@ -14,6 +14,7 @@ local compiled_template = [[
local compile = compile
local getmetatable = getmetatable
local setmetatable = setmetatable
local is_callable = ...
local function prepare_env(env, parent)
local meta = getmetatable(env)
if meta and meta.__index then
Expand Down Expand Up @@ -78,13 +79,11 @@ local compiled_template = [[
]==],
[==[
$if_args[===[
if type(selector) == 'function' then
selector = selector($args, false)
end
selector = selector($args, false)
insert(out, tostring(selector))
]===],
[===[
if type(selector) == 'function' then
if is_callable(selector) then
insert(out, tostring(selector()))
else
insert(out, tostring(selector))
Expand Down Expand Up @@ -140,14 +139,21 @@ local function compile_template_application(chunkname, selector, args, first_sub
return ta
end

local function is_callable(f)
if type(f) == "function" then return true end
local meta = getmetatable(f)
if meta and meta.__call then return true end
return false
end

local function compile_template(chunkname, compiled_parts)
local template_code = interpreter.fill(compiled_template, { parts = compiled_parts })
local template_func, err = loadstring(template_code, chunkname)
if not template_func then
error("syntax error when compiling template: " .. err)
else
setfenv(template_func, _M)
return template_func()
return template_func(is_callable)
end
end

Expand Down
13 changes: 9 additions & 4 deletions src/cosmo/fill.lua
Expand Up @@ -3,6 +3,13 @@ local grammar = require "cosmo.grammar"

module(..., package.seeall)

local function is_callable(f)
if type(f) == "function" then return true end
local meta = getmetatable(f)
if meta and meta.__call then return true end
return false
end

local function get_selector(env, selector)
selector = string.sub(selector, 2, #selector)
local parts = {}
Expand Down Expand Up @@ -63,12 +70,10 @@ local function fill_template_application(state, selector, args, first_subtemplat
selector = loadstring("local env = (...); return " .. selector)(env) or function () return '' end
if #subtemplates == 0 then
if args and args ~= "" then
if type(selector) == 'function' then
selector = selector(loadstring("local env = (...); return " .. args)(env), false)
end
selector = selector(loadstring("local env = (...); return " .. args)(env), false)
insert(out, tostring(selector))
else
if type(selector) == 'function' then
if is_callable(selector) then
insert(out, tostring(selector()))
else
insert(out, tostring(selector))
Expand Down

0 comments on commit adfeebc

Please sign in to comment.