Skip to content

Commit

Permalink
toffee now supports postProcessing, say for markup conversion inside …
Browse files Browse the repository at this point in the history
…your code, etc.
  • Loading branch information
malgorithms committed Oct 29, 2013
1 parent f756eb7 commit ea03a72
Show file tree
Hide file tree
Showing 21 changed files with 257 additions and 147 deletions.
22 changes: 20 additions & 2 deletions lib/engine.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 11 additions & 2 deletions lib/toffee_lang.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions lib/view.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "toffee",
"description": "A NodeJs, Express 3.x, Express 2.x, and browser-side templating language based on CoffeeScript with slicker tokens and syntax.",
"version": "0.1.4",
"version": "0.1.5",
"directories": {
"lib": "./lib"
},
Expand All @@ -12,7 +12,8 @@
"coffee-script": "*",
"commander": "*",
"uglify-js": "*",
"mkdirp": "*"
"mkdirp": "*",
"highlight.js": "*"
},
"devDependencies": {
"jison" : "*",
Expand Down
19 changes: 18 additions & 1 deletion src/engine.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class engine
###
"options" contains the pub vars and may contain special items:
layout: path to a template expecting a body var (express 2.x style, but for use with express 3.x)
postProcess: a function which takes the string of output and post processes it (returning new string)
__toffee.dir: path to look relative to
__toffee.parent: parent file
__toffee.noInheritance: if true, don't pass variables through unless explicitly passed
Expand All @@ -67,6 +68,10 @@ class engine
if not options.additionalErrorHandler? then options.additionalErrorHandler = @additionalErrorHandler
if not options.autoEscape? then options.autoEscape = @autoEscape

# we only want to pass post_process into the layout
post_process = options.postProcess
options.postProcess = null

if options?.layout
layout_options = {}
layout_options[k] = v for k,v of options when k isnt "layout"
Expand All @@ -84,8 +89,20 @@ class engine
if err and @prettyPrintErrors
[err, res] = [null, err]

# post processing
if (not err) and (typeof(post_process) is "function")
[err, res] = @postProcess post_process, res

cb err, res

postProcess: (fn, res) ->
err = null
try
res = fn res
catch e
err = e
return [err, res]

runSync: (filename, options) ->
###
"options" the same as run() above
Expand Down Expand Up @@ -140,7 +157,7 @@ class engine

# we need to make a shallow copy of parent variables
reserved = {}
reserved[k] = true for k in ["passback", "load", "print", "partial", "snippet", "layout", "__toffee"]
reserved[k] = true for k in ["passback", "load", "print", "partial", "snippet", "layout", "__toffee", "postProcess"]
if not noInheritance
for k,v of parent_options when not local_vars?[k]?
if not reserved[k]?
Expand Down
4 changes: 3 additions & 1 deletion src/view.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ getBundleHeaders = (tab_level) ->
#{__}
#{__} # we need to make a shallow copy of parent variables
#{__} reserved = {}
#{__} reserved[k] = true for k in ["passback", "load", "print", "partial", "snippet", "layout", "__toffee"]
#{__} reserved[k] = true for k in ["passback", "load", "print", "partial", "snippet", "layout", "__toffee", "postProcess"]
#{__} if not options.__toffee.noInheritance
#{__} for k,v of parent_locals when not locals?[k]?
#{__} if not reserved[k]?
Expand Down Expand Up @@ -535,6 +535,8 @@ class view
___ = tabs 1 # guaranteed tabs
"""\n
#{__}#{___}__toffee.res = __toffee.out.join ""
#{__}#{___}if postProcess?
#{__}#{___}#{___}__toffee.res = postProcess __toffee.res
#{__}#{___}if (not __repress) then return __toffee.res else return ""
#{__}`true; } /* closing JS 'with' */ `
#{__}# sometimes we want to execute the whole thing in a sandbox
Expand Down
30 changes: 5 additions & 25 deletions test/cases/custom_escape/input.toffee
Original file line number Diff line number Diff line change
@@ -1,31 +1,11 @@
{#
x = '"Hello world"'
x = '"Hello world"<script>var x = 100;</script>'
y = '<td>'
z = 'click&clack'
w = [1, 2, {"place": "The Dreadfort"}]
#}<p>
default x = #{x}
default y = #{y}
default z = #{z}
default w = #{w}
</p>
<p>
raw x = #{raw x}
raw y = #{raw y}
raw z = #{raw z}
raw w = #{raw w}
</p>
<script>
x = #{json x}
y = #{json y}
z = #{json z}
w = #{json w}
</script>
<p>
{#
print " raw printed x = #{x}\n"
print " raw printed y = #{y}\n"
print " raw printed z = #{z}\n"
print " raw printed w = #{w}"
#}
custom x = #{x}
custom y = #{y}
custom z = #{z}
custom w = #{w}
</p>
26 changes: 4 additions & 22 deletions test/cases/custom_escape/output.toffee
Original file line number Diff line number Diff line change
@@ -1,24 +1,6 @@
<p>
default x = ["Hello world"]
default y = [<td>]
default z = [click&clack]
default w = [1,2,[object Object]]
</p>
<p>
raw x = "Hello world"
raw y = <td>
raw z = click&clack
raw w = 1,2,[object Object]
</p>
<script>
x = "\"Hello world\""
y = "\u003Ctd\u003E"
z = "click\u0026clack"
w = [1,2,{"place":"The Dreadfort"}]
</script>
<p>
raw printed x = "Hello world"
raw printed y = <td>
raw printed z = click&clack
raw printed w = 1,2,[object Object]
custom x = Helloworldscriptvarx100script
custom y = td
custom z = clickclack
custom w = 12objectObject
</p>
3 changes: 3 additions & 0 deletions test/cases/custom_escape/vars.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
escape: (s) -> "#{s}".replace /[^a-z0-9]/gi, ''
}
3 changes: 0 additions & 3 deletions test/cases/custom_escape/vars.js

This file was deleted.

1 change: 1 addition & 0 deletions test/cases/post_process/buncha_junk.toffee
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
T3246h354is345-i3245s345-534a534-h534i543d534d534e534n543-m534e543s543s543ag5e534.543
7 changes: 7 additions & 0 deletions test/cases/post_process/input.toffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#{greeting}, world.
#{partial './signature.toffee'}
{#
reverse = (s) -> (c for c in s by -1).join ""
clean = (s) -> (c for c in s when c.match /[a-z\-]/gi).join ""
#}
#{partial './buncha_junk.toffee', {postProcess: (s) -> reverse(clean(s))}}
4 changes: 4 additions & 0 deletions test/cases/post_process/output.toffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
This-is-a-hidden-message

.dlrow leurc ,eybdooG
.dlrow ,olleH
1 change: 1 addition & 0 deletions test/cases/post_process/signature.toffee
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Goodbye, cruel world.
4 changes: 4 additions & 0 deletions test/cases/post_process/vars.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
greeting: 'Hello'
postProcess: (s) -> (c for c in s by -1).join ''
}
Loading

0 comments on commit ea03a72

Please sign in to comment.