Skip to content

Commit

Permalink
Merge tag 'v0.11.2' of git://github.com/paularmstrong/swig
Browse files Browse the repository at this point in the history
* **Fixed** Update support for underscore@1.3.3 [gh-70] [gh-71]
  • Loading branch information
andris-silis committed Apr 13, 2012
2 parents e8cfdf4 + b40f61f commit 732d3b4
Show file tree
Hide file tree
Showing 16 changed files with 92 additions and 211 deletions.
32 changes: 31 additions & 1 deletion History.md
@@ -1,5 +1,35 @@
[0.11.2](https://github.com/paularmstrong/swig/tree/v0.11.2) / 2012-04-10
-------------------------------------------------------------------------

* **Fixed** Update support for underscore@1.3.3 [gh-70] [gh-71]

[Documentation](https://github.com/paularmstrong/swig/tree/v0.11.2/docs)

[0.11.1](https://github.com/paularmstrong/swig/tree/v0.11.1) / 2012-04-01
-------------------------------------------------------------------------

* **Fixed** Duplicate (string) tokens were being removed when extending a base template. [gh-67]

[Documentation](https://github.com/paularmstrong/swig/tree/v0.11.1/docs)

[0.11.0](https://github.com/paularmstrong/swig/tree/v0.11.0) / 2012-02-27
-------------------------------------------------------------------------

* **Added** Support for Windows style paths [gh-57]
* **Added** `ignore missing` tokens to include tag
* **Changed** include tag `with context` to only work if `context` is an object
* **Changed** `autoescape` tag controls no longer 'yes' or 'no'. Use `true` and `false`
* **Changed** parser is now passed into tags as an argument
* **Changed** don't require passing context object when rendering template
* **Fixed** dateformats `N` and `w` [gh-59]
* **Fixed** number changing to string after add filter or set from variable [gh-53] [gh-58]
* **Fixed** speed decrease caused by loop.cycle fixed
* **Fixed** Ensure set tag bubbles through extends and blocks

[Documentation](https://github.com/paularmstrong/swig/tree/v0.11.0/docs)

[0.10.0](https://github.com/paularmstrong/swig/tree/v0.10.0) / 2012-02-13
------------------------------------------------------------------------
-------------------------------------------------------------------------

* **Added** loop.index0, loop.revindex, loop.revindex0, and loop.cycle [gh-48]
* **Added** init config `extensions` for 3rd party extension access in custom tags [gh-44]
Expand Down
16 changes: 9 additions & 7 deletions Makefile
Expand Up @@ -6,14 +6,16 @@ all:
browser:
@scripts/browser.sh

tests := $(shell find . -name '*.test.js' ! -path "*node_modules/*" ! -path "*dist/*")
reporter = skip_passed
test:
@node tests/speed.js
@node scripts/runtests.js

testf:
@node_modules/nodeunit/bin/nodeunit ${file}
@node_modules/nodeunit/bin/nodeunit --reporter ${reporter} ${tests}

files := $(shell find . -name '*.js' ! -path "*node_modules/*" ! -path "*dist/*")
lint:
@node scripts/runlint.js
@node_modules/nodelint/nodelint ${files} --config=scripts/config-lint.js

speed:
@node tests/speed.js

.PHONY: all browser test lint
.PHONY: all browser test lint speed
15 changes: 7 additions & 8 deletions docs/custom-tags.md
Expand Up @@ -10,14 +10,13 @@ First, make sure to include your node.js file that declares your tags in the swi
Requirements <a name="requirements" href="#requirements">#</a>
------------

First, include the Swig parser and helpers.
First, include the helpers.

var parser = require('swig/lib/parser'),
helpers = require('swig/lib/helpers');
var helpers = require('swig/lib/helpers');

Define your tag and whether or not it requires an "end" tag:

exports.mytag = function (indent, parentBlock) {
exports.mytag = function (indent, parentBlock, parser) {
return 'output';
};
exports.mytag.ends = true;
Expand All @@ -27,15 +26,15 @@ A Really Simple Tag <a name="example" href="#example">#</a>

To parse a swig variable with or without filters into a variable token, eg. `bar` or `foo|lowercase`

exports.mytag = function (indent, parentBlock) {
exports.mytag = function (indent, parentBlock, parser) {
var myArg = parser.parseVariable(this.args[0]);
return 'output';
};
exports.mytag.ends = true;

Use a parsed variable token with `helpers.setVar()` to bind a variable in your current scope into the templates scope. The `setVar` method cleans up variable output, applies filters and escaping for clean output:

exports.mytag = function (indent, parentBlock) {
exports.mytag = function (indent, parentBlock, parser) {
var myArg = parser.parseVariable(this.args[0]),
output = '';
output += helpers.setVar(name, myArg);
Expand All @@ -45,7 +44,7 @@ Use a parsed variable token with `helpers.setVar()` to bind a variable in your c

To parse the inner content of a tag for outputting, use `parser.compile.apply(this, [indent, parentBlock])`:

exports.mytag = function (indent, parentBlock) {
exports.mytag = function (indent, parentBlock, parser) {
var myArg = parser.parseVariable(this.args[0]),
output = [];

Expand Down Expand Up @@ -87,7 +86,7 @@ To access a third-party library or method that is defined outside of your templa

Once you've added it, your custom tag can reference the `i18next` extension via the `_ext` object:

exports.trans = function (indent, parentBlock) {
exports.trans = function (indent, parentBlock, parser) {
var myArg = parser.parseVariable(this.args[0]),
output = [];
output.push(helpers.setVar('__myArg', myArg));
Expand Down
11 changes: 5 additions & 6 deletions index.js
Expand Up @@ -91,12 +91,11 @@ function createTemplate(data, id) {
template.render = function (context, parents) {
if (_config.allowErrors) {
return render.call(this, context, parents, _config.filters, _, _config.extensions);
} else {
try {
return render.call(this, context, parents, _config.filters, _, _config.extensions);
} catch (e) {
return new TemplateError(e);
}
}
try {
return render.call(this, context, parents, _config.filters, _, _config.extensions);
} catch (e) {
return new TemplateError(e);
}
};

Expand Down
5 changes: 3 additions & 2 deletions lib/dateformat.js
Expand Up @@ -112,14 +112,15 @@ exports.l = function (input) {
return _days.full[input.getDay()];
};
exports.N = function (input) {
return input.getDay();
var d = input.getDay();
return (d >= 1) ? d + 1 : 7;
};
exports.S = function (input) {
var d = input.getDate();
return (d % 10 === 1 && d !== 11 ? 'st' : (d % 10 === 2 && d !== 12 ? 'nd' : (d % 10 === 3 && d !== 13 ? 'rd' : 'th')));
};
exports.w = function (input) {
return input.getDay() - 1;
return input.getDay();
};
exports.z = function (input, offset, abbr) {
var year = input.getFullYear(),
Expand Down
13 changes: 6 additions & 7 deletions lib/filters.js
Expand Up @@ -99,9 +99,8 @@ exports.escape = exports.e = function (input, type) {
.replace(/>/g, '&gt;')
.replace(/"/g, '&quot;')
.replace(/'/g, '&#39;');
} else {
return input;
}
return input;
};

exports.first = function (input) {
Expand All @@ -119,15 +118,16 @@ exports.first = function (input) {
exports.join = function (input, separator) {
if (_.isArray(input)) {
return input.join(separator);
} else if (typeof input === 'object') {
}

if (typeof input === 'object') {
var out = [];
_.each(input, function (value, key) {
out.push(value);
});
return out.join(separator);
} else {
return input;
}
return input;
};

exports.json_encode = function (input) {
Expand Down Expand Up @@ -171,9 +171,8 @@ exports.replace = function (input, search, replacement, flags) {
exports.reverse = function (input) {
if (_.isArray(input)) {
return input.reverse();
} else {
return input;
}
return input;
};

exports.striptags = function (input) {
Expand Down
19 changes: 17 additions & 2 deletions lib/filters.test.js
Expand Up @@ -60,9 +60,9 @@ exports.date = function (test) {
testFormat('D', 'Tue');
testFormat('j', '6');
testFormat('l', 'Tuesday');
testFormat('N', '2');
testFormat('N', '3');
testFormat('S', 'th');
testFormat('w', '1');
testFormat('w', '2');
testFormat('z', '248');
testFilter(test, 'date("z", 480)', { v: makeDate(480, 2011, 0, 1) }, '0', 'z');
testFilter(test, 'date("z", 480)', { v: makeDate(480, 2011, 11, 31) }, '364', 'z');
Expand Down Expand Up @@ -112,6 +112,21 @@ exports.date = function (test) {
testFormat('r', 'Tue, 06 Sep 2011 16:05:02 GMT');
testFormat('U', '1315325102');

// More complete S ordinal testing
testFilter(test, 'date("S", 420)', { v: makeDate(420, 2011, 8, 1) }, 'st', 'S 1st');
testFilter(test, 'date("S", 420)', { v: makeDate(420, 2011, 8, 2) }, 'nd', 'S 2nd');
testFilter(test, 'date("S", 420)', { v: makeDate(420, 2011, 8, 3) }, 'rd', 'S 3rd');
testFilter(test, 'date("S", 420)', { v: makeDate(420, 2011, 8, 4) }, 'th', 'S 4th');
testFilter(test, 'date("w", 420)', { v: makeDate(420, 2011, 8, 4) }, '0', 'w 0');
testFilter(test, 'date("N", 420)', { v: makeDate(420, 2011, 8, 4) }, '7', 'N 7');
testFilter(test, 'date("S", 420)', { v: makeDate(420, 2011, 8, 10) }, 'th', 'S 10th');
testFilter(test, 'date("S", 420)', { v: makeDate(420, 2011, 8, 11) }, 'th', 'S 11th');
testFilter(test, 'date("S", 420)', { v: makeDate(420, 2011, 8, 12) }, 'th', 'S 12th');
testFilter(test, 'date("S", 420)', { v: makeDate(420, 2011, 8, 13) }, 'th', 'S 13th');
testFilter(test, 'date("S", 420)', { v: makeDate(420, 2011, 8, 21) }, 'st', 'S 21st');
testFilter(test, 'date("S", 420)', { v: makeDate(420, 2011, 8, 22) }, 'nd', 'S 22nd');
testFilter(test, 'date("S", 420)', { v: makeDate(420, 2011, 8, 23) }, 'rd', 'S 23rd');

test.done();
};

Expand Down
6 changes: 3 additions & 3 deletions lib/helpers.js
Expand Up @@ -116,9 +116,8 @@ function check(variable, context) {
output[output.length - 1] = _.last(output).replace(/\] !== "undefined"$/, '_' + prop + '] !== "undefined"');
chain = chain.replace(/\]$/, '_' + prop + ']');
return;
} else {
chain += '[___' + prop + ']';
}
chain += '[___' + prop + ']';
} else {
chain += '[' + prop + ']';
}
Expand Down Expand Up @@ -149,7 +148,8 @@ exports.escapeVarName = function (variable, context) {

if (exports.isLiteral(variable)) {
return variable;
} else if (typeof context === 'string' && context.length) {
}
if (typeof context === 'string' && context.length) {
variable = context + '.' + variable;
}

Expand Down
4 changes: 2 additions & 2 deletions lib/parser.js
Expand Up @@ -258,7 +258,7 @@ exports.parse = function (data, tags, autoescape) {
line: curline,
name: tagname,
compile: tags[tagname],
parent: _.uniq(stack[stack.length - 2]),
parent: _.uniq(stack[stack.length - 2] || []),
strip: {
before: stripBefore,
after: stripAfter,
Expand Down Expand Up @@ -347,7 +347,7 @@ exports.compile = function compile(indent, parentBlock) {

if (tokens.length && tokens[0].name === 'extends') {
this.blocks = _.extend({}, this.parent.blocks, this.blocks);
this.tokens = _.union(sets, this.parent.tokens);
this.tokens = sets.concat(this.parent.tokens);
}
sets = tokens = null;
}
Expand Down
7 changes: 7 additions & 0 deletions lib/parser.test.js
Expand Up @@ -396,5 +396,12 @@ exports.Compiling = testCase({
eval('var _output = "";' + parser.compile.call(template, ''));
}, 'block should throw if nested');
test.done();
},

'duplicate strings are not removed when extended': function (test) {
var dummy = swig.compile('"{{ e }}"{{ f }}"{{ g }}"{{ h }}', { filename: 'foo' }),
tpl = '{% extends "foo" %}';
test.strictEqual(swig.compile(tpl)({ e: 'e', f: 'f', g: 'g', h: 'h' }), '"e"f"g"h', 'render extended');
test.done();
}
});
2 changes: 0 additions & 2 deletions lib/tags/for.js
Expand Up @@ -40,7 +40,6 @@ module.exports = function (indent, parentBlock, parser) {
out = '(function () {\n' +
' var loop = {}, __loopKey, __loopIndex = 0, __loopLength = 0,' +
' __ctx_operand = _context["' + operand1 + '"],\n' +
' __ctx_cycle = (typeof loop_cycle !== "undefined") ? loop_cycle : null,\n' +
' loop_cycle = function() {\n' +
' var args = _.toArray(arguments), i = loop.index0 % args.length;\n' +
' return args[i];\n' +
Expand All @@ -65,7 +64,6 @@ module.exports = function (indent, parentBlock, parser) {
loopShared +
' }\n' +
' }\n' +
' loop_cycle = __ctx_cycle;\n' +
' _context["' + operand1 + '"] = __ctx_operand;\n' +
'})();\n';

Expand Down
4 changes: 2 additions & 2 deletions package.json
@@ -1,6 +1,6 @@
{
"name": "swig",
"version": "0.10.0",
"version": "0.11.2",
"description": "A fast django-like templating engine for node.js and browsers.",
"keywords": ["template", "templating", "html", "django", "express", "block"],
"repository": {
Expand Down Expand Up @@ -31,4 +31,4 @@
"bugs": {
"url": "https://github.com/paularmstrong/swig/issues"
}
}
}
5 changes: 0 additions & 5 deletions scripts/config-lint.js
@@ -1,8 +1,3 @@
module.exports = {
root: __dirname + '/../',
pathIgnore: ['*node_modules*', '*browser/underscore*', '*dist/browser/*', '*dist/test*']
};

var options = {
adsafe: false,
bitwise: true,
Expand Down
5 changes: 0 additions & 5 deletions scripts/config-test.js

This file was deleted.

35 changes: 0 additions & 35 deletions scripts/runlint.js

This file was deleted.

0 comments on commit 732d3b4

Please sign in to comment.