Permalink
Browse files

version 0.21 with snippets

  • Loading branch information...
malgorithms committed Jul 5, 2012
1 parent 8e9693c commit fb6e54dd48518a1a6df0f34248cf373e158171ec
View
@@ -1,6 +1,6 @@
# expose the render function
eclass = require('./lib/engine').engine
-e = new eclass { maxCacheAge: Infinity }
+e = new eclass { maxCacheAge: 2000 }
exports.expressEngine = e
exports.render = e.run
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View
@@ -3,7 +3,7 @@ fs = require 'fs'
obj =
name: "toffee"
description: """An Express 3.x and 2.x templating language based on CoffeeScript with slicker tokens and syntax. Built at OkCupid."""
- version: "0.0.18"
+ version: "0.0.21"
directories: {"lib" : "./lib"}
main: "index.js"
author: "Chris Coyne <ccoyne77@gmail.com>"
View
@@ -1,7 +1,7 @@
{
"name": "toffee",
"description": "An Express 3.x and 2.x templating language based on CoffeeScript with slicker tokens and syntax. Built at OkCupid.",
- "version": "0.0.18",
+ "version": "0.0.21",
"directories": {
"lib": "./lib"
},
View
@@ -19,30 +19,43 @@ class engine
"options" contains the pub vars
may also contain special items:
__dir: path to look relative to
+ layout: path to a template expecting a body var (express 2.x style, but for use with express 3.x)
###
+
[err, res] = @runSync filename, options
+
+ # if we got an error but want to pretty-print by faking ok result
if err and @prettyPrintErrors
- cb null, err
- else
- cb err, res
-
+ [err, res] = [null, err]
+
+ # if we're using a layout, pub into that
+ if (not err) and options?.layout
+ options.body = res
+ [err, res] = @runSync options.layout, options
+ if err and @prettyPrintErrors
+ [err, res] = [null, err]
+
+ cb err, res
+
+
runSync: (filename, options) ->
###
returns [err, res];
"options" the same as run() above
###
options = options or {}
options.__dir = options.__dir or process.cwd()
- filename = "#{options.__dir}/#{filename}" if filename.charAt(0) isnt "/"
+ filename = "#{options.__dir}/#{filename}" if filename[0] isnt "/"
realpath = filename
pwd = path.dirname realpath
@_resetCache() if Date.now() - @lastCacheReset > @maxCacheAge
- v = @viewCache[filename] or @_loadAndCache filename, options
+ v = @viewCache[realpath] or @_loadAndCache realpath, options
if v
- options.__parent = filename
+ options.__parent = realpath
options.include = options.include or (fname, lvars) => @_fn_include fname, lvars, realpath, options
options.partial = options.partial or (fname, lvars) => @_fn_partial fname, lvars, realpath, options
+ options.snippet = options.snippet or (fname, lvars) => @_fn_snippet fname, lvars, realpath, options
options.print = options.print or (txt) => @_fn_print txt, options
[err, res] = v.run options
return [err, res]
@@ -57,8 +70,11 @@ class engine
options.__parent = parent_realpath
# we need to make a shallow copy of parent variables
- for k,v of parent_options when (k[0...2] isnt "__") and not local_keys[k]?
- options[k] = v
+ if not options.__no_inheritance
+ for k,v of parent_options when not local_keys[k]?
+ if k[0...2] isnt "__"
+ if not (k in ["print", "partial", "include", "snippet"])
+ options[k] = v
[err, res] = @runSync filename, options
@@ -94,7 +110,12 @@ class engine
else
res
- _fn_partial: (fname, lvars, realpath, options) ->
+ _fn_snippet: (fname, lvars, realpath, options) =>
+ lvars = if lvars? then lvars else {}
+ lvars.__no_inheritance = true
+ @_inlineInclude fname, lvars, realpath, options
+
+ _fn_partial: (fname, lvars, realpath, options) =>
@_inlineInclude fname, lvars, realpath, options
_fn_print: (txt, options) ->
View
@@ -123,18 +123,18 @@ class view
ind = indent_level# - indent_baseline
res += "\n#{@_space ind}__toffee.lineno = #{obj[2]}"
res += "\n#{@_space ind}__toffee.state = states.TOFFEE"
- res += "\n#{@_space ind}__toffee.indent_baseline = #{indent_baseline}"
- res += "\n#{@_space ind}__toffee.indent_level = #{indent_level}"
+ # res += "\n#{@_space ind}__toffee.indent_baseline = #{indent_baseline}"
+ # res += "\n#{@_space ind}__toffee.indent_level = #{indent_level}"
lines = obj[1].split "\n"
for line, i in lines
if not line.match /#/
if i
res += "\n#{@_space ind}__toffee.lineno = #{obj[2]+i}"
lbreak = if i isnt lines.length - 1 then "\n" else ""
- res += "\n#{@_space ind}__toffee.out.push " + '"""' + @_escapeForStr(line + lbreak) + '"""'
+ res += "\n#{@_space ind}__toffee.out.push " + @_quoteStr(line + lbreak)
else
- res += "\n#{@_space ind}__toffee.out.push " + '"""' + @_escapeForStr(lines[(i)...].join "\n") + '"""'
+ res += "\n#{@_space ind}__toffee.out.push " + @_quoteStr(lines[(i)...].join "\n")
break
#res += "\n#{@_space indent_level}__toffee.out.push " + '"""' + @_escapeForStr(obj[1]) + '"""'
@@ -152,6 +152,26 @@ class view
return [res, i_delta]
+ _quoteStr: (s) ->
+ ###
+ returns a triple-quoted string, dividing into single quoted
+ start and stops, if the string begins with double quotes, since
+ coffee doesn't want to let us escape those.
+ ###
+ lead = ""
+ follow = ""
+ while s.length and (s[0] is '"')
+ s = s[1...]
+ lead += '"'
+ while s.length and (s[-1...] is '"')
+ s = s[...-1]
+ follow += '"'
+ res = ''
+ if lead.length then res += "\'#{lead}\' + "
+ res += '"""' + @_escapeForStr(s) + '"""'
+ if follow.length then res += "+ \'#{follow}\'"
+ res
+
_escapeForStr: (s) ->
###
escapes a string so it can make it into coffeescript
@@ -173,14 +193,15 @@ class view
# or null, if the region doesn't have any real code in it
res = null
lines = coffee.split "\n"
- if lines.length isnt 0
- for line in lines
- if not line.match /^[ ]*$/
+ if lines.length
+ for line, i in lines
+ # if it has characters or it's the last chance, use it
+ if (not line.match /^[ ]*$/) or i is (lines.length - 1)
res = line.match(/[ ]*/)[0].length
break
- if not res
- res = coffee.length - 1
- #console.log "====\n#{coffee}\n#{res}\n---"
+ if not res?
+ res = coffee.length
+ #console.log "====\n?#{coffee.replace /[ ]/g, '.'}?\n#{res}\n---"
return res
_getIndentationDelta: (coffee, baseline) ->
@@ -0,0 +1,5 @@
+a
+{#
+ say_hi()
+#}
+b
@@ -0,0 +1,9 @@
+{#
+ say_hi = ->
+ -{:hi:}
+#}
+1
+2
+#{partial "child.toffee", say_hi: say_hi}
+3
+4
Oops, something went wrong.

0 comments on commit fb6e54d

Please sign in to comment.