Skip to content

Commit

Permalink
initial revision, after major overhall of library
Browse files Browse the repository at this point in the history
  • Loading branch information
cstivers78 committed Jan 13, 2012
0 parents commit 88db47f
Show file tree
Hide file tree
Showing 21 changed files with 2,031 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
@@ -0,0 +1,2 @@
.DS_Store*
node_modules
12 changes: 12 additions & 0 deletions README.md
@@ -0,0 +1,12 @@
# bliss

A template engine for Node.js, inspired by Microsoft Razor and Play! Framework template engines.

I became hooked on using Play! framework's Scala templates, which was inspired by Razor templates. I liked it so much, that I found I really wanted to use something similar on Node.js.


## Details

See [wiki](https://github.com/cstivers78/bliss/wiki) for details on use and syntax.

See [issues](https://github.com/cstivers78/bliss/issues) for details on bugs and features.
34 changes: 34 additions & 0 deletions bin/bliss-compile
@@ -0,0 +1,34 @@
#!/usr/local/bin/node
const _ = require('underscore');
const uglify = require('uglify-js');
const minifier = require('html-minifier');
const util = require('util');
const bliss = require('..');

const args = _.map(process.argv,_.identity);
const nodeExec = args.shift();
const nodeScript = args.shift();

var template = ast = code = undefined, compileStart = compileStop = 0;

compileStart = Date.now();
template = bliss.compileFile(args.shift(),{
context: {
_: _
}
});
compileStop = Date.now();

formattedSource = uglify.uglify.gen_code(uglify.parser.parse(template.toString()),{
beautify: true,
indent_start: 0,
indent_level: 2,
space_colon: true
});

console.log(formattedSource);
console.log('/*')
console.log(' * generated by:','Bliss','(https://github.com/cstivers78/bliss)');
console.log(' * generated on:',new Date());
console.log(' * compile time:',compileStop-compileStart,'ms');
console.log(' */')
51 changes: 51 additions & 0 deletions bin/bliss-render
@@ -0,0 +1,51 @@
#!/usr/local/bin/node
const _ = require('underscore');
const minifier = require('html-minifier');
const util = require('util');
const bliss = require('..');

const args = _.map(process.argv,_.identity);
const nodeExec = args.shift();
const nodeScript = args.shift();

try {
var template = output = undefined, compileStart = compileStop = renderStart = renderStop = 0;

compileStart = Date.now();
template = bliss.compileFile(args.shift());
compileStop = Date.now();

renderStart = Date.now();
output = template.apply(null,args);
renderStop = Date.now();

console.log(output)
console.log('<!--')
console.log(' generated by:','Bliss','(https://github.com/cstivers78/bliss)');
console.log(' generated on:',new Date());
console.log(' compile time:',compileStop-compileStart,'ms');
console.log(' render time:',renderStop-renderStart,'ms');
console.log('-->')
}
catch (thrown) {
console.error('')
console.error('[error]',thrown.toString(),'at',thrown.fileName+':'+thrown.lineNumber);
console.error('')
lines = template.toSource().split('\n');
start = thrown.lineNumber - 3 > 0 ? thrown.lineNumber - 3 : 0;
end = thrown.lineNumber + 2 < lines.length ? thrown.lineNumber+2 : lines.length;
for(l=start; l<=end; l++) {
line = lines[l];
if ( line === undefined )
continue;
if ( l == thrown.lineNumber-1) {
marker = _.map(lines[l].split(''),function(){return '-'}).join('')
console.error('[error] ----'+marker)
console.error('[error]',l+1,':',line);
console.error('[error] ----'+marker)
}
else {
console.error('[error]',l+1,':',line);
}
}
}
68 changes: 68 additions & 0 deletions lib/bliss.js
@@ -0,0 +1,68 @@
var Tokenizer, Writer, compile, compileFile, fs, path, render, tokenizer,
__slice = Array.prototype.slice;

fs = require('fs');

path = require('path');

Writer = require('./writer');

Tokenizer = require('./tokenizer');

tokenizer = new Tokenizer();

compile = function(source, options) {
var context, func, tmpl, tmplParams, tmplSource, writer, _ref;
if (options == null) options = {};
context = (_ref = options.context) != null ? _ref : options.context = {};
context.render = function() {
var args, dirname, exists, filename, filepath;
filename = arguments[0], args = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
dirname = path.dirname(options.filename);
filepath = path.resolve(dirname, filename);
exists = path.existsSync(filepath);
if (!exists) {
filepath = filepath + '.js.html';
exists = path.existsSync(filepath);
}
if (exists) {
return render.apply(null, [filepath].concat(__slice.call(args)));
} else {
throw 'ENOENT';
}
};
writer = new Writer();
writer.write(tokenizer.tokenize(source));
tmplParams = writer.parameters;
tmplSource = writer.source(context);
func = Function.apply(null, __slice.call(tmplParams).concat([tmplSource]));
tmpl = func.bind(context);
tmpl.toString = func.toString.bind(func);
tmpl.toSource = function() {
return source;
};
tmpl.name = options.filename;
return tmpl;
};

compileFile = function(filename, options) {
var source, template;
source = fs.readFileSync(filename, 'utf8');
options = {
filename: filename
};
return template = compile(source, options);
};

render = function() {
var args, filename, template;
filename = arguments[0], args = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
template = compileFile(filename);
return template.apply(null, args);
};

module.exports = {
compile: compile,
compileFile: compileFile,
render: render
};

0 comments on commit 88db47f

Please sign in to comment.