From 1f70838ebcbbd2484c63d489416692d37c3eec58 Mon Sep 17 00:00:00 2001 From: Jeremy Penner Date: Wed, 16 Feb 2011 09:41:24 -0800 Subject: [PATCH] add support for executing arbitrary javascript to templating language --- iffy.coffee | 35 +++++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/iffy.coffee b/iffy.coffee index 4210833..b9d980d 100644 --- a/iffy.coffee +++ b/iffy.coffee @@ -323,9 +323,9 @@ class Nev #[word text to display] -- links to a word, usually a noun, that the player can interact with in some way. #[word] == [word word] -# $[if (expr)]some text$[else]some other text$[endif] (else is optional) -# $[(expr)] inserts as a string the result of expr -#[[ outputs [, ]] outputs ] +# $(expr) inserts as a string the result of expr +# $[statements] executes some javascript (semicolons needed) +# [[ outputs [, ]] outputs ] TemplateFromStNev = (st, wst) -> class Xpd @@ -378,12 +378,22 @@ TemplateFromStNev = (st, wst) -> /\[\[/g, () -> MatchInText, /\]\]/g, () -> MatchInText, /\[/g, () -> EndText(); MatchInWord, - /\$\[/g, () -> EndText(); MatchInExpr) - MatchBracket = (cbracket, dgPush) -> + /\$\[/g, () -> EndText(); MatchInStmts, + /\$\(/g, () -> EndText(); MatchInExpr) + + EndMatchBracket = (cbracketSquareNew, cbracketParenNew, dgPush) -> + if cbracketSquareNew <= 0 && cbracketParenNew <= 0 + dgPush() + MatchInText + else + MatchBracket(cbracketSquareNew, cbracketParenNew, dgPush) + MatchBracket = (cbracketSquare, cbracketParen, dgPush) -> () -> Match(dgPush, # error? - /"/g, () -> MatchString(MatchBracket(cbracket, dgPush)), - /\[/g, () -> MatchBracket(cbracket + 1, dgPush), - /\]/g, () -> if cbracket == 0 then dgPush(); MatchInText else MatchBracket(cbracket - 1, dgPush)) + /"/g, () -> MatchString(MatchBracket(cbracketSquare, cbracketParen, dgPush)), + /\[/g, () -> MatchBracket(cbracketSquare + 1, cbracketParen, dgPush), + /\]/g, () -> EndMatchBracket(cbracketSquare - 1, cbracketParen, dgPush), + /\(/g, () -> MatchBracket(cbracketSquare, cbracketParen + 1, dgPush), + /\)/g, () -> EndMatchBracket(cbracketSquare, cbracketParen - 1, dgPush)) MatchString = (matchAfter) -> () -> @@ -404,8 +414,9 @@ TemplateFromStNev = (st, wst) -> JsStringLit(wst.gst.Link(stDisplay, ((ev) -> wst.gst.ShowMenu(ev, this, rgverb)), "class='iffy-word'")) else JsStringLit(stDisplay) - MatchInWord = MatchBracket(0, () -> PushText(true, StWord)) - MatchInExpr = MatchBracket(0, () -> PushText(true)) + MatchInWord = MatchBracket(1, 0, () -> PushText(true, StWord)) + MatchInExpr = MatchBracket(0, 1, () -> PushText(true)) + MatchInStmts = MatchBracket(1, 0, () -> PushText(false)) BuildRgxpd = (dgMatch) -> while dgMatch? @@ -413,7 +424,7 @@ TemplateFromStNev = (st, wst) -> BuildRgxpd(MatchInText) - rgstJs = ["var __p=[];with(obj){"] + rgstJs = ["var __p=[];with(wst){"] fExpr = false for xpd in rgxpd if xpd.fExpr != fExpr @@ -424,7 +435,7 @@ TemplateFromStNev = (st, wst) -> rgstJs.push(xpd.st) if fExpr then rgstJs.push(");") rgstJs.push("}return __p.join('');") - new Function("obj",rgstJs.join('')) + new Function("wst",rgstJs.join('')) class Word constructor: (@gst, @jWord) ->