Skip to content

Commit

Permalink
indent WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
nicocube committed Jun 26, 2015
1 parent 082f94e commit c5657e9
Show file tree
Hide file tree
Showing 3 changed files with 161 additions and 62 deletions.
79 changes: 60 additions & 19 deletions lib/llama.js
Expand Up @@ -9,14 +9,14 @@
* See the License for the specific language governing permissions and limitations under the License.
*/

module.exports = function (mlf) {
module.exports = function (mlf, extend) {
'use strict'

var formatter = {
block : function (tagName, attr, elements) {
return this.open_block(tagName, attr) + this.children(elements) + this.close_block(tagName)
block : function (tagName, attr, elements, ctx) {
return this.open_block(tagName, attr, ctx) + this.children(elements, ctx) + this.close_block(tagName, ctx)
},
open_block: function (tagName, attr) { return '<' + tagName + (typeof attr !== 'undefined' ? this.tag_attr(attr) : '') + '>' },
open_block: function (tagName, attr, ctx) { return ctx.indent_compute() + '<' + tagName + (typeof attr !== 'undefined' ? this.tag_attr(attr) : '') + '>' },
tag_attr: function(attr) {
var ks = Object.keys(attr);
if (ks.length === 0) return '';
Expand All @@ -26,8 +26,8 @@ module.exports = function (mlf) {
return k + '="' + (typeof v === 'function' ? v(this) : v) + '"'
}).get().join(' ')
},
close_block: function (tagName) { return '</' + tagName + '>' },
children : function (elements) { return mlf(elements).map(parse).get().join('') }
close_block: function (tagName, ctx) { return '</' + tagName + '>'+ ctx.indent_close_compute() },
children : function (elements, ctx) { ctx.depth += 1; var s = mlf(elements).map(parse).get().join(''); ctx.depth -= 1; return s }
}

function parse(o) {
Expand All @@ -46,8 +46,8 @@ module.exports = function (mlf) {
var attr, v = Array.prototype.slice.call(arguments)
if (typeof v[0] === 'object') attr = v.shift()
return function() {
return formatter.block(tagName, attr, mlf(v).map(parse).get())
}
return formatter.block(tagName, attr, mlf(v).map(parse).get(), this.options)
}.bind(this)
}
}

Expand All @@ -56,8 +56,8 @@ module.exports = function (mlf) {
var attr, v = Array.prototype.slice.call(arguments)
if (typeof v[0] === 'object') attr = v.shift()
return function() {
return formatter.open_block(tagName, attr)
}
return formatter.open_block(tagName, attr, this.options)
}.bind(this)
}
}

Expand All @@ -75,28 +75,69 @@ module.exports = function (mlf) {
}
}
}
var that = this
return function() {
return function() {
//console.log('each',k, arr)
return formatter.children(
mlf(arr).map(function(e) {
var c = {}
c[k] = e
that._.push(c)
this._.push(c)
//console.log('k: "'+k+'" e: '+e)
//console.log('k: "'+k+'" e: '+e, v.map(function(f){if (typeof f === 'function') return f.toString(); else return f }))
var r = formatter.children(mlf(v).map(parse).get())
that._.pop()
var r = formatter.children(mlf(v).map(parse).get(),this.options)
this._.pop()
return r
}).get()
)
}
}.bind(this)).get()
,this.options)
}.bind(this)
}.bind(this)
}

var optionDerivator = {
indent : function(options) {
if (! 'depth' in options || typeof options.depth !== 'number') options.depth = 0
if (! 'sep' in options || typeof options.sep !== 'string') options.sep = ' '
if (! 'indent_compute' in options || typeof options.indent_compute !== 'function') options.indent_compute = function() {
if (this.depth != 0 && this.indent > 0) {
var i = 0, l = this.depth * this.indent, r = '', s = this.sep;
for (;i < l; i+=1) r+=s;
return r
//return '[<'+this.depth+']'+r
}
return ''
//return '[<'+this.depth+']'
}
if (! 'indent_close_compute' in options || typeof options.indent_close_compute !== 'function') options.indent_close_compute = function() {
return '\n';
/*
if (this.depth != 0 && this.indent > 0) {
var i = 0, l = (this.depth - 1) * this.indent, r = '\n', s = this.sep;
for (;i < l; i+=1) r+=s;
//return r
//return '['+this.depth+'>]'+r
}
//return ''
//return '['+this.depth+'>]'
*/
}
}
}

function context(context) {
function derive(options) {
return function(k) {
if (k in optionDerivator) optionDerivator[k](options)
}
}

function deriveOptions(options) {
Object.keys(options).forEach(derive(options))
}

function context(context, options) {
this._ = []
this.options = extend({indent:0}, options||{})
deriveOptions(this.options)
if (typeof context !== 'undefined') this._.push(context)
}
function register(dol) {
Expand Down Expand Up @@ -148,7 +189,7 @@ module.exports = function (mlf) {
return 'tags.'+param+'('
}
})
body = 'return function(context) {\n tags.context(context);return (function(){'+body+'})()();\n }'
body = 'return function(context, options) {\n tags.context(context, options);return (function(){'+body+'})()();\n }'
//console.log(body)
var f = new Function('tags', body)
res = f(tags)
Expand Down
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -18,7 +18,8 @@
"node": "0.10.*"
},
"dependencies": {
"mlf": "0.1.*"
"mlf": "0.1.*",
"node.extend": "^1.1.5"
},
"devDependencies": {
"jasmine-node": "1.14.*",
Expand Down
141 changes: 99 additions & 42 deletions spec/llama.spec.js
@@ -1,74 +1,131 @@
/*
* Copyright 2015 Nicolas Lochet Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and limitations under the License.
*/


var llama = require( __dirname + '/../lib/llama.js')(require('mlf'))

var llama = require( __dirname + '/../lib/llama.js')(require('mlf'), require('node.extend'))

describe("template testing", function() {
"use strict"

it("Hello world!", function() {
var template = llama(function() {
return h1("Hello World!");

xdescribe("no options", function() {
it("Hello world!", function() {
var template = llama(function() {
return h1("Hello World!");
})

var res = template()
expect(res).toEqual('<h1>Hello World!</h1>')

})

var res = template()
expect(res).toEqual('<h1>Hello World!</h1>')
it("simple each", function() {
var template = llama(function() {
return ul(each([1,2,3], li('plip:', $()) ))
})

})
it("simple each", function() {
var template = llama(function() {
return ul(each([1,2,3], li('plip:', $()) ))
var res = template()
expect(res).toEqual('<ul><li>plip:1</li><li>plip:2</li><li>plip:3</li></ul>')
})

var res = template()
expect(res).toEqual('<ul><li>plip:1</li><li>plip:2</li><li>plip:3</li></ul>')
})
it("embeded each", function() {
var template = llama(function() {
return div(
each({i: [1,2,3]},
div('i:', $i()),
each({j: [1,2,3]},
div('j:', $i(), '*', $j())
it("embeded each", function() {
var template = llama(function() {
return div(
each({i: [1,2,3]},
div('i:', $i()),
each({j: [1,2,3]},
div('j:', $i(), '*', $j())
)
)
)
)
})

var res = template()
expect(res).toEqual('<div><div>i:1</div><div>j:1*1</div><div>j:1*2</div><div>j:1*3</div><div>i:2</div><div>j:2*1</div><div>j:2*2</div><div>j:2*3</div><div>i:3</div><div>j:3*1</div><div>j:3*2</div><div>j:3*3</div></div>')

})

var res = template()
expect(res).toEqual('<div><div>i:1</div><div>j:1*1</div><div>j:1*2</div><div>j:1*3</div><div>i:2</div><div>j:2*1</div><div>j:2*2</div><div>j:2*3</div><div>i:3</div><div>j:3*1</div><div>j:3*2</div><div>j:3*3</div></div>')
it("complex template", function() {
var template = llama(function() {
return div({id:'plop', _:'paf pouf'}, h1("Hello ", $name(), "!"),
ul(each([1,2,3],
li('plip:', $())
)),
div(
each({i: [1,2,3]},
div('i:', $i()),
each({j: [1,2,3]},
div('j:', $i(), '*', $j())
)
)
)
)
})

var res = template({name: 'Plop'})
expect(res).toEqual('<div id="plop" class="paf pouf"><h1>Hello Plop!</h1><ul><li>plip:1</li><li>plip:2</li><li>plip:3</li></ul><div><div>i:1</div><div>j:1*1</div><div>j:1*2</div><div>j:1*3</div><div>i:2</div><div>j:2*1</div><div>j:2*2</div><div>j:2*3</div><div>i:3</div><div>j:3*1</div><div>j:3*2</div><div>j:3*3</div></div></div>')

})
})
it("complex template", function() {
var template = llama(function() {
return div({id:'plop', _:'paf pouf'}, h1("Hello ", $name(), "!"),
ul(each([1,2,3],
li('plip:', $())
)),
div(

describe("indent option", function() {
it("Hello world!", function() {
var template = llama(function() {
return h1("Hello World!");
})

var res = template({},{indent:4})
expect(res).toEqual('<h1>Hello World!</h1>')

})
it("simple each", function() {
var template = llama(function() {
return ul(each([1,2,3], li('plip:', $()) ))
})

var res = template({},{indent:4})
expect(res).toEqual('<ul>\n <li>plip:1</li>\n <li>plip:2</li>\n <li>plip:3</li>\n</ul>')
})
xit("embeded each", function() {
var template = llama(function() {
return div(
each({i: [1,2,3]},
div('i:', $i()),
each({j: [1,2,3]},
div('j:', $i(), '*', $j())
)
)
)
)
})

var res = template({},{indent:4})
expect(res).toEqual('<div>\n <div>i:1</div>\n <div>j:1*1</div>\n <div>j:1*2</div>\n <div>j:1*3</div>\n <div>i:2</div>\n <div>j:2*1</div>\n <div>j:2*2</div>\n <div>j:2*3</div>\n <div>i:3</div>\n <div>j:3*1</div>\n <div>j:3*2</div>\n <div>j:3*3</div>\n</div>')

})

var res = template({name: 'Plop'})
expect(res).toEqual('<div id="plop" class="paf pouf"><h1>Hello Plop!</h1><ul><li>plip:1</li><li>plip:2</li><li>plip:3</li></ul><div><div>i:1</div><div>j:1*1</div><div>j:1*2</div><div>j:1*3</div><div>i:2</div><div>j:2*1</div><div>j:2*2</div><div>j:2*3</div><div>i:3</div><div>j:3*1</div><div>j:3*2</div><div>j:3*3</div></div></div>')
xit("complex template", function() {
var template = llama(function() {
return div({id:'plop', _:'paf pouf'}, h1("Hello ", $name(), "!"),
ul(each([1,2,3],
li('plip:', $())
)),
div(
each({i: [1,2,3]},
div('i:', $i()),
each({j: [1,2,3]},
div('j:', $i(), '*', $j())
)
)
)
)
})

var res = template({name: 'Plop'},{indent:4})
expect(res).toEqual('<div id="plop" class="paf pouf">\n <h1>Hello Plop!</h1>\n <ul>\n <li>plip:1</li>\n <li>plip:2</li>\n <li>plip:3</li>\n </ul>\n <div>\n <div>i:1</div>\n <div>j:1*1</div>\n <div>j:1*2</div>\n <div>j:1*3</div>\n <div>i:2</div>\n <div>j:2*1</div>\n <div>j:2*2</div>\n <div>j:2*3</div>\n <div>i:3</div>\n <div>j:3*1</div>\n <div>j:3*2</div>\n <div>j:3*3</div>\n </div>\n</div>')

})
})
})

0 comments on commit c5657e9

Please sign in to comment.