1 var util = require('util')
3 var INDENT_START = /[\{\[]/
4 var INDENT_END = /[\}\]]/
6 module.exports = function() {
10 var push = function(str) {
12 while (spaces.length < indent*2) spaces += ' '
13 lines.push(spaces+str)
16 var line = function(fmt) {
19 if (INDENT_END.test(fmt.trim()[0]) && INDENT_START.test(fmt[fmt.length-1])) {
21 push(util.format.apply(util, arguments))
25 if (INDENT_START.test(fmt[fmt.length-1])) {
26 push(util.format.apply(util, arguments))
30 if (INDENT_END.test(fmt.trim()[0])) {
32 push(util.format.apply(util, arguments))
36 push(util.format.apply(util, arguments))
40 line.toString = function() {
41 return lines.join('\n')
44 line.toFunction = function(scope) {
45 var src = 'return ('+line.toString()+')'
47 var keys = Object.keys(scope || {}).map(function(key) {
51 var vals = keys.map(function(key) {
55 return Function.apply(null, keys.concat(src)).apply(null, vals)
58 if (arguments.length) line.apply(null, arguments)