Skip to content

Commit

Permalink
improve filter with good styles, and more powerful
Browse files Browse the repository at this point in the history
  • Loading branch information
shaunlee committed Apr 30, 2011
1 parent 22b54d4 commit 5f9df54
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 35 deletions.
4 changes: 3 additions & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ via npm:
jst.render('Hello {{ it.name }}', {name: 'jst'});

// Filters
jst.render('Hello {{ e(it.name) }}', {name: '<strong>jst</strong>'});
jst.render('Hello {{ it.name|e }}', {name: '<strong>jst</strong>'});
jst.render('{{ it.entry|e|linebreaks }}', {entry: '...'});
jst.render('{{ it.value|add(100) }}', {value: 100});

// Client side
<script src="jst.js"></script>
Expand Down
34 changes: 14 additions & 20 deletions lib/filters.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,33 @@
// filters

const htmlCodes = {'&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;'},
htmlre = /[&<>"]/g,
htmlre = /&(?!\w+;)|<|>|"/g,
htmlEscape = function (src) { return htmlCodes[src]; },
linere = /(\r\n|\r|\n)/g,
filterCodes = {
'e(': 'jst_filter_escape(',
'br(': 'jst_filter_linebreaks(',
'md(': 'jst_filter_markdown(',
'_(': 'jst_filter_gettext('
},
filterre = /^(e|br|md|_)\(/g,
filterConvert = function(src) { return filterCodes[src]; };
linere = /(\r\n|\r|\n)/g;

exports.convert = function(src) {
return src.replace(filterre, filterConvert);
return src.split('|').reduce(function(varname, filter) {
return 'filters.' + filter + '(' + varname + ')';
});
}

// e(src)
jst_filter_escape = function(src) {
exports.e = exports.escape = function(src) {
return typeof src !== 'string' ? src : src.replace(htmlre, htmlEscape);
}

// br(src)
jst_filter_linebreaks = function(src) {
exports.linebreaks = function(src) {
return '<p>' + src.split(/\r\n|\n/g).join('</p><p>') + '</p>';
}

exports.linebreaksbr = function(src) {
return src.replace(linere, '<br>$1');
}

// md(src)
jst_filter_markdown = function(src) {
exports.md = exports.markdown = function(src) {
return src; // TODO:
}

// _(src)
jst_filter_gettext = function(src) {
return src; // TODO:
exports.add = function(value) {
return function(src) { return Number(value) + Number(src); };
}

3 changes: 1 addition & 2 deletions lib/i18n.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
// i18n

// _(ctx)
//jst_i18n_gettext = function(ctx) {
_ = function(ctx) {
exports.gettext = function(ctx) {
return ctx; // TODO:
}

25 changes: 14 additions & 11 deletions lib/jst.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@

var fs = require('fs'),
crypto = require('crypto'),
filters = require('./filters');
filters = require('./filters'),
i18n = require('./i18n');

exports.version = '0.0.6';
exports.version = '0.0.8';

var _cache = {},
_files = {},
Expand Down Expand Up @@ -77,7 +78,11 @@ var compile = exports.compile = function(ctx) {
code += 'return out;';
//console.log(code);

return new Function('it', code);
var fn = new Function('it, _, filters', code);

return function(args) {
return fn.call(this, args, i18n.gettext, filters);
}
}

var render = exports.render = function(ctx, args) {
Expand Down Expand Up @@ -105,7 +110,7 @@ var renderFile = exports.renderFile = function(filename, args, fn) {

_files[fk] = _files[fk] || {};

if (_files[fk].isEmpty() || _files[fk].ctime < stats.ctime) {
if (isempty(_files[fk]) || _files[fk].ctime < stats.ctime) {
fs.readFile(filename, 'utf8', function(err, ctx) {
if (err)
return fn(err);
Expand All @@ -125,12 +130,10 @@ var renderFile = exports.renderFile = function(filename, args, fn) {
});
}

if (typeof Object.prototype.isEmpty === 'undefined') {
Object.prototype.isEmpty = function() {
for (var prop in this)
if (this.hasOwnProperty(prop))
return false;
return true;
}
function isempty(o) {
for (var prop in o)
if (o.hasOwnProperty(prop))
return false;
return true;
}

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "jst",
"description": "Node JavaScript Template, A pretty high performance template engine",
"version": "0.0.6",
"version": "0.0.8",
"author": "Shaun Li <shonhen@gmail.com>",
"keywords": ["template", "engine", "jst"],
"main": "./lib/jst.js"
Expand Down

0 comments on commit 5f9df54

Please sign in to comment.