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,8 +323,8 @@ 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
# $(expr) inserts as a string the result of expr
# $[statements] executes some javascript (semicolons needed)
# [[ outputs [, ]] outputs ]
TemplateFromStNev = (st, wst) ->
@ -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) ->