add support for executing arbitrary javascript to templating language

This commit is contained in:
Jeremy Penner 2011-02-16 09:41:24 -08:00
parent 8f8ae6fcdf
commit 1f70838ebc

View file

@ -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 text to display] -- links to a word, usually a noun, that the player can interact with in some way.
#[word] == [word word] #[word] == [word word]
# $[if (expr)]some text$[else]some other text$[endif] (else is optional) # $(expr) inserts as a string the result of expr
# $[(expr)] inserts as a string the result of expr # $[statements] executes some javascript (semicolons needed)
#[[ outputs [, ]] outputs ] # [[ outputs [, ]] outputs ]
TemplateFromStNev = (st, wst) -> TemplateFromStNev = (st, wst) ->
class Xpd class Xpd
@ -378,12 +378,22 @@ TemplateFromStNev = (st, wst) ->
/\[\[/g, () -> MatchInText, /\[\[/g, () -> MatchInText,
/\]\]/g, () -> MatchInText, /\]\]/g, () -> MatchInText,
/\[/g, () -> EndText(); MatchInWord, /\[/g, () -> EndText(); MatchInWord,
/\$\[/g, () -> EndText(); MatchInExpr) /\$\[/g, () -> EndText(); MatchInStmts,
MatchBracket = (cbracket, dgPush) -> /\$\(/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? () -> Match(dgPush, # error?
/"/g, () -> MatchString(MatchBracket(cbracket, dgPush)), /"/g, () -> MatchString(MatchBracket(cbracketSquare, cbracketParen, dgPush)),
/\[/g, () -> MatchBracket(cbracket + 1, dgPush), /\[/g, () -> MatchBracket(cbracketSquare + 1, cbracketParen, dgPush),
/\]/g, () -> if cbracket == 0 then dgPush(); MatchInText else MatchBracket(cbracket - 1, dgPush)) /\]/g, () -> EndMatchBracket(cbracketSquare - 1, cbracketParen, dgPush),
/\(/g, () -> MatchBracket(cbracketSquare, cbracketParen + 1, dgPush),
/\)/g, () -> EndMatchBracket(cbracketSquare, cbracketParen - 1, dgPush))
MatchString = (matchAfter) -> MatchString = (matchAfter) ->
() -> () ->
@ -404,8 +414,9 @@ TemplateFromStNev = (st, wst) ->
JsStringLit(wst.gst.Link(stDisplay, ((ev) -> wst.gst.ShowMenu(ev, this, rgverb)), "class='iffy-word'")) JsStringLit(wst.gst.Link(stDisplay, ((ev) -> wst.gst.ShowMenu(ev, this, rgverb)), "class='iffy-word'"))
else else
JsStringLit(stDisplay) JsStringLit(stDisplay)
MatchInWord = MatchBracket(0, () -> PushText(true, StWord)) MatchInWord = MatchBracket(1, 0, () -> PushText(true, StWord))
MatchInExpr = MatchBracket(0, () -> PushText(true)) MatchInExpr = MatchBracket(0, 1, () -> PushText(true))
MatchInStmts = MatchBracket(1, 0, () -> PushText(false))
BuildRgxpd = (dgMatch) -> BuildRgxpd = (dgMatch) ->
while dgMatch? while dgMatch?
@ -413,7 +424,7 @@ TemplateFromStNev = (st, wst) ->
BuildRgxpd(MatchInText) BuildRgxpd(MatchInText)
rgstJs = ["var __p=[];with(obj){"] rgstJs = ["var __p=[];with(wst){"]
fExpr = false fExpr = false
for xpd in rgxpd for xpd in rgxpd
if xpd.fExpr != fExpr if xpd.fExpr != fExpr
@ -424,7 +435,7 @@ TemplateFromStNev = (st, wst) ->
rgstJs.push(xpd.st) rgstJs.push(xpd.st)
if fExpr then rgstJs.push(");") if fExpr then rgstJs.push(");")
rgstJs.push("}return __p.join('');") rgstJs.push("}return __p.join('');")
new Function("obj",rgstJs.join('')) new Function("wst",rgstJs.join(''))
class Word class Word
constructor: (@gst, @jWord) -> constructor: (@gst, @jWord) ->