Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
initial revision, after major overhall of library
- Loading branch information
0 parents
commit 88db47f
Showing
21 changed files
with
2,031 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
.DS_Store* | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(' */') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
}; |
Oops, something went wrong.