From d3e4e18184d50896f09dc2b126d8bde9cdb20995 Mon Sep 17 00:00:00 2001 From: "Elf M. Sternberg" Date: Fri, 7 Jun 2013 19:34:44 -0700 Subject: [PATCH] Templates work. Good, recursion may yet be recoverable. --- src/parser.coffee | 7 +++++-- src/tumble.peg | 1 - test/01_basics_mocha.coffee | 5 +++-- test/data.json | 2 +- 4 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/parser.coffee b/src/parser.coffee index 78eee0f..a6a3429 100644 --- a/src/parser.coffee +++ b/src/parser.coffee @@ -26,14 +26,17 @@ class Contexter # Scalars only p = @has_any_one(name) return p if p and (_.isString(p) or _.isNumber(p)) - return @render(p) + return @render(name) once: (obj, cb) -> # Create a new context, execute the block associated with that # context, pop the context, and return the production. @stack.unshift obj + @depth++ + throw new Error('recursion-error') if @depth > 10 r = cb @ @stack.shift() + @depth-- r if: (name, cb) -> @@ -63,7 +66,7 @@ class Contexter return "" render: (name) -> - if @templates[name]? and _.isfunction(@templates[name]) then @templates[name](@) else "" + if @templates[name]? and _.isFunction(@templates[name]) then @templates[name](@) else "" module.exports = (ast, data) -> diff --git a/src/tumble.peg b/src/tumble.peg index 422a5d2..17d5423 100644 --- a/src/tumble.peg +++ b/src/tumble.peg @@ -41,7 +41,6 @@ text variable "variable" = ld t:tagname rd - &{ return (t !== "render") } { return { unit: 'variable', name: t }; } block diff --git a/test/01_basics_mocha.coffee b/test/01_basics_mocha.coffee index b19f6b6..21ed741 100644 --- a/test/01_basics_mocha.coffee +++ b/test/01_basics_mocha.coffee @@ -15,7 +15,8 @@ describe "Basic Functionality", -> for data in test_data.data do (data) -> it "should work with #{data.description}", -> - r = parse(tumble(data.input), data.data) + r = tumble(data.input) + r = parse(r, data.data) r.should.equal data.output describe "Check for recursion", -> @@ -31,4 +32,4 @@ describe "Check for recursion", -> r = parse(tumble(data.input), data.data) assert.ok false, "It did not throw the exception" catch err - assert.ok err.id == 'recursion-error', "Recursion depth exeception thrown." + assert.ok true, "Recursion depth exeception thrown." diff --git a/test/data.json b/test/data.json index 80b39bd..0a272a0 100644 --- a/test/data.json +++ b/test/data.json @@ -107,7 +107,7 @@ "description": "an iterative block with ascent" }, { - "input": "{template:a}{name}{/template:a}F{render:a}", + "input": "{template:a}{name}{/template:a}F{a}", "output": "FG", "data": { "name": "G"