Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

improve filter with good styles, and more powerful

  • Loading branch information...
commit 5f9df547c94bc4812b3f10711499a0192db8081c 1 parent 22b54d4
@shaunlee authored
View
4 Readme.md
@@ -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>
View
34 lib/filters.js
@@ -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); };
}
View
3  lib/i18n.js
@@ -1,8 +1,7 @@
// i18n
// _(ctx)
-//jst_i18n_gettext = function(ctx) {
-_ = function(ctx) {
+exports.gettext = function(ctx) {
return ctx; // TODO:
}
View
25 lib/jst.js
@@ -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 = {},
@@ -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) {
@@ -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);
@@ -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;
}
View
2  package.json
@@ -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"
Please sign in to comment.
Something went wrong with that request. Please try again.