Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

syntax tuning

  • Loading branch information...
commit 0b86ba1f9bd9803e8eacf238287769c9057c6dd6 1 parent c3f4b02
@ben-lin ben-lin authored
View
26 Readme.md
@@ -80,8 +80,8 @@ returns the text ready to be compiled for the `compile` function
#### Example code
- var input = '<div>Hello, this is <?= it.name ?> :)</div>',
- compiled_text = thunder.compiled_text( input );
+ var input = '<div>Hello, this is <?= it.name ?> :)</div>';
+ var compiled_text = thunder.compiled_text( input );
console.log( compiled_text );
// var __t__='<div>Hello, this is ';__t__+= it.name ;__t__+=' :)</div>';return __t__;
@@ -108,8 +108,8 @@ returns the compiled function
#### Example code
- var input = '<div>Hello, this is <?= it.name ?> :)</div>',
- render = thunder.compile( input );
+ var input = '<div>Hello, this is <?= it.name ?> :)</div>';
+ var render = thunder.compile( input );
// it actually turns to the following function
// function ( locals ){
@@ -138,8 +138,8 @@ returns the cached compiled function
#### Example code
- var input = '<div>Hello, this is <?= it.name ?> :)</div>',
- render = thunder.cached( input );
+ var input = '<div>Hello, this is <?= it.name ?> :)</div>';
+ var render = thunder.cached( input );
// it actually turns to the following function and will be cached
// so that next time the text does not need to be compiled again
@@ -179,12 +179,14 @@ returns the output
#### Example code
var input = '<div>Hello, this is <?= it.name ?> :)</div>',
- locals = { name : 'Bibi' },
- options = {
- cached : true,
- compress : true
- },
- output = thunder.render( input, locals, options );
+ var locals = { name : 'Bibi' };
+
+ var options = {
+ cached : true,
+ compress : true
+ };
+
+ var output = thunder.render( input, locals, options );
console.log( output );
// <div>Hello, this is Bibi :)</div>
View
114 benchmarks/run.js
@@ -1,13 +1,13 @@
-var dot = require( 'dot' ),
- ejs = require( 'ejs' ),
- haml = require( 'haml' ),
- hamljs = require( 'hamljs' ),
- jade = require( 'jade' ),
- jqtpl = require( 'jqtpl' ),
- jst = require( 'jst' ),
- swig = require( 'swig' ),
- tenjin = require( 'tenjin' ),
- thunder = require( '../lib/thunder' );
+var dot = require( 'dot' );
+var ejs = require( 'ejs' );
+var haml = require( 'haml' );
+var hamljs = require( 'hamljs' );
+var jade = require( 'jade' );
+var jqtpl = require( 'jqtpl' );
+var jst = require( 'jst' );
+var swig = require( 'swig' );
+var tenjin = require( 'tenjin' );
+var thunder = require( '../lib/thunder' );
@@ -20,9 +20,9 @@ var shared_vars = {
header5 : 'Header5',
header6 : 'Header6',
list : [ '1', '2', '3', '4', '5', '6', '7', '8', '9', '10' ]
-},
+};
-shared_vars_escape = {
+var shared_vars_escape = {
title : '"THUNDER ROCKS"',
header1 : '<Header1>',
header2 : '<Header2>',
@@ -37,37 +37,35 @@ shared_vars_escape = {
var fs = require( 'fs' );
-var dot_tpl = fs.readFileSync( './templates/dot.html', 'utf8' ),
- dot_tpl_escape = fs.readFileSync( './templates/dot_escape.html', 'utf8' ),
+var dot_tpl = fs.readFileSync( './templates/dot.html', 'utf8' );
+var dot_tpl_escape = fs.readFileSync( './templates/dot_escape.html', 'utf8' );
- ejs_tpl = fs.readFileSync( './templates/ejs.ejs', 'utf8' ),
- ejs_tpl_escape = fs.readFileSync( './templates/ejs_escape.ejs', 'utf8' ),
+var ejs_tpl = fs.readFileSync( './templates/ejs.ejs', 'utf8' );
+var ejs_tpl_escape = fs.readFileSync( './templates/ejs_escape.ejs', 'utf8' );
- haml_tpl = fs.readFileSync( './templates/haml.haml', 'utf8' ),
- haml_tpl_escape = fs.readFileSync( './templates/haml_escape.haml', 'utf8' ),
-
- hamljs_tpl = fs.readFileSync( './templates/hamljs.haml', 'utf8' ),
- hamljs_tpl_escape = fs.readFileSync( './templates/hamljs_escape.haml', 'utf8' ),
+var haml_tpl = fs.readFileSync( './templates/haml.haml', 'utf8' );
+var haml_tpl_escape = fs.readFileSync( './templates/haml_escape.haml', 'utf8' );
- jade_tpl = fs.readFileSync( './templates/jade.jade', 'utf8' ),
- jade_tpl_escape = fs.readFileSync( './templates/jade_escape.jade', 'utf8' ),
+var hamljs_tpl = fs.readFileSync( './templates/hamljs.haml', 'utf8' );
+var hamljs_tpl_escape = fs.readFileSync( './templates/hamljs_escape.haml', 'utf8' );
- jqtpl_tpl = fs.readFileSync( './templates/jqtpl.html', 'utf8' ),
- jqtpl_tpl_escape = fs.readFileSync( './templates/jqtpl_escape.html', 'utf8' ),
+var jade_tpl = fs.readFileSync( './templates/jade.jade', 'utf8' );
+var jade_tpl_escape = fs.readFileSync( './templates/jade_escape.jade', 'utf8' );
- jst_tpl = fs.readFileSync( './templates/jst.html', 'utf8' ),
- jst_tpl_escape = fs.readFileSync( './templates/jst_escape.html', 'utf8' ),
-
- swig_tpl = fs.readFileSync( './templates/swig.html', 'utf8' ),
- swig_tpl_escape = fs.readFileSync( './templates/swig_escape.html', 'utf8' ),
-
- tenjin_tpl = fs.readFileSync( './templates/tenjin.html', 'utf8' ),
- tenjin_tpl_escape = fs.readFileSync( './templates/tenjin_escape.html', 'utf8' ),
+var jqtpl_tpl = fs.readFileSync( './templates/jqtpl.html', 'utf8' );
+var jqtpl_tpl_escape = fs.readFileSync( './templates/jqtpl_escape.html', 'utf8' );
+
+var jst_tpl = fs.readFileSync( './templates/jst.html', 'utf8' );
+var jst_tpl_escape = fs.readFileSync( './templates/jst_escape.html', 'utf8' );
- thunder_tpl = fs.readFileSync( './templates/thunder.html', 'utf8' ),
- thunder_tpl_escape = fs.readFileSync( './templates/thunder_escape.html', 'utf8' );
+var swig_tpl = fs.readFileSync( './templates/swig.html', 'utf8' );
+var swig_tpl_escape = fs.readFileSync( './templates/swig_escape.html', 'utf8' );
+var tenjin_tpl = fs.readFileSync( './templates/tenjin.html', 'utf8' );
+var tenjin_tpl_escape = fs.readFileSync( './templates/tenjin_escape.html', 'utf8' );
+var thunder_tpl = fs.readFileSync( './templates/thunder.html', 'utf8' );
+var thunder_tpl_escape = fs.readFileSync( './templates/thunder_escape.html', 'utf8' );
@@ -279,12 +277,11 @@ var engines = [
var compile_speed = function ( engines, method, count, title ){
- var i, j, k, key, engine, start, seconds;
-
- i = 0;
- j = engines.length;
+ var i = 0;
+ var j = engines.length;
+ var k, key, engine, start, seconds;
- for(; i < j; i++ ){
+ for( ; i < j; i++ ){
engine = engines[ i ];
key = engine.key;
start = new Date();
@@ -308,29 +305,28 @@ var compile_speed = function ( engines, method, count, title ){
var render_speed = function ( engines, method, shared_vars, count, title ){
- var i, j, k, engine, start, seconds, view;
-
- i = 0;
- j = engines.length;
-
- for(; i < j; i++ ){
- engine = engines[ i ];
- start = new Date();
- k = 0;
+ var i = 0;
+ var j = engines.length;
+ var k, engine, start, seconds, view;
+
+ for(; i < j; i++ ){
+ engine = engines[ i ];
+ start = new Date();
+ k = 0;
- console.log('\n' + title + ', render ' + count + ' times:\n');
- console.log( engine.name, 'running...' );
+ console.log('\n' + title + ', render ' + count + ' times:\n');
+ console.log( engine.name, 'running...' );
- for(; k < count; k++ ){
- view = engine[ 'render' + method ]( shared_vars );
- }
+ for(; k < count; k++ ){
+ view = engine[ 'render' + method ]( shared_vars );
+ }
- seconds = ( new Date() - start ) / 1000;
+ seconds = ( new Date() - start ) / 1000;
- // console.log( view );
- console.log( 'use: ' + seconds + ' sec, rps: ' + ( count / seconds ));
- console.log( '--------------------------------------------' );
- }
+ // console.log( view );
+ console.log( 'use: ' + seconds + ' sec, rps: ' + ( count / seconds ));
+ console.log( '--------------------------------------------' );
+ }
};
View
7 examples/complex/run.js
@@ -1,8 +1,7 @@
-var thunder, input, output;
+var thunder = require( '../../lib/thunder' );
+var input = require( 'fs' ).readFileSync( './input.html', 'utf8' );
-thunder = require( '../../lib/thunder' );
-input = require( 'fs' ).readFileSync( './input.html', 'utf8' );
-ouput = thunder.render( input ,{
+var ouput = thunder.render( input ,{
title : '[thunder] Rocks!',
header1 : "This is header 1",
header2 : 'This is header 2',
View
7 examples/express/app.js
@@ -3,10 +3,9 @@
* Module dependencies.
*/
-var express = require( 'express' ),
- routes = require( './routes' );
-
-var app = module.exports = express.createServer();
+var express = require( 'express' );
+var routes = require( './routes' );
+var app = module.exports = express.createServer();
// Configuration
View
8 examples/simple/run.js
@@ -1,6 +1,6 @@
-var thunder = require( '../../lib/thunder' ),
- input = '<div>Hello, this is <?= it.name ?> :)</div>',
- render = thunder.compile( input ),
- output = render({ name : 'Bibi' });
+var thunder = require( '../../lib/thunder' );
+var input = '<div>Hello, this is <?= it.name ?> :)</div>';
+var render = thunder.compile( input );
+var output = render({ name : 'Bibi' });
console.log( output );
View
2  index.js
@@ -1 +1 @@
-module.exports = require('./lib/thunder');
+module.exports = require( './lib/thunder' );
View
23 lib/thunder.js
@@ -6,7 +6,6 @@
* @fileoverview
* A lighting fast template parser for node.js.
*/
-
var cache = {};
// split `escape` into another obj speeds up the `no escape` parsing
@@ -34,18 +33,17 @@ module.exports = {
version : '0.0.1',
compiled_text : function ( input, options ){
- var arr, str, i, j, tmp;
-
- arr = ( options && options.compress === true ?
+ var arr = ( options && options.compress === true ?
// compress
input.replace( /\n\r\t|\s+/g, ' ' ) :
// chage the new line to unix version
input.replace( /\n\r|\r/g, '\n' )).
split( '<?' ).join( '?>\x1b' ).split( '?>' );
- str = '';
- i = 0;
- j = arr.length;
+ var str = '';
+ var i = 0;
+ var j = arr.length;
+ var tmp;
// string concat is faster than array `push`
for(; i < j; i++ ){
@@ -67,9 +65,8 @@ module.exports = {
},
compile : function ( input, options ){
- var str, fn;
-
- str = this.compiled_text( input, options );
+ var str = this.compiled_text( input, options );
+ var fn;
try{
// must save this new Function to fn,
@@ -85,7 +82,7 @@ module.exports = {
return fn( locals, escape.fn );
};
},
-
+
cached : function ( input, options ){
if( !cache[ input ]){
cache[ input ] = this.compile( input, options );
@@ -95,9 +92,9 @@ module.exports = {
},
render : function ( input, locals, options ){
- var method = options && options.cached === true ?
+ var method = options && options.cached === true ?
'cached' : 'compile';
-
+
return this[ method ]( input, options )( locals );
}
};
Please sign in to comment.
Something went wrong with that request. Please try again.