Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add JSON tags

  • Loading branch information...
commit 7082246afe230917d7312fae9fbaca3675d8e898 1 parent 9609311
@amccollum authored
View
2  Cakefile
@@ -28,7 +28,7 @@ task 'test', 'Build and run the test suite', ->
'ln -sf ../node_modules node_modules',
'ln -sf .. node_modules/wings',
#'node_modules/.bin/ender build ender-vows ..',
- 'node_modules/.bin/vows *-test.js',
+ 'node_modules/.bin/vows --spec *-test.js',
'rm -f node_modules/wings node_modules/vows node_modules',
'cd ..',
]
View
3  lib/ender.js
@@ -1,5 +1,4 @@
-// Generated by CoffeeScript 1.3.3
-
+// Generated by CoffeeScript 1.6.3
(function($) {
var renderTemplate;
renderTemplate = require('wings').renderTemplate;
View
23 lib/wings.js
@@ -1,5 +1,4 @@
-// Generated by CoffeeScript 1.3.3
-
+// Generated by CoffeeScript 1.6.3
(function(wings) {
var escapeXML, isArray, parsePattern, renderRawTemplate, replaceBraces, restoreBraces;
wings.strict = false;
@@ -35,7 +34,7 @@
}
});
};
- parsePattern = /\{([:!])\s*([^}\s]*?)\s*\}([\S\s]+?)\{\/\s*\2\s*\}|\{(\#)[\S\s]+?\#\}|\{([@&]?)\s*([^}\s]*?)\s*\}/mg;
+ parsePattern = /\{([:!])\s*([^}\s]*?)\s*\}([\S\s]+?)\{\/\s*\2\s*\}|\{(\#)[\S\s]+?\#\}|\{([@&~]?)\s*([^}\s]*?)\s*\}/mg;
return renderRawTemplate = function(template, data, links) {
return template.replace(parsePattern, function(all, sectionOp, sectionName, sectionContent, commentOp, tagOp, tagName) {
var content, i, link, name, op, part, parts, rest, v, value, _i, _len, _ref;
@@ -45,7 +44,7 @@
switch (op) {
case ':':
value = data[name];
- if (!(value != null)) {
+ if (value == null) {
if (wings.strict) {
throw "Invalid section: " + (JSON.stringify(data)) + ": " + name;
} else {
@@ -71,7 +70,7 @@
break;
case '!':
value = data[name];
- if (!(value != null)) {
+ if (value == null) {
if (wings.strict) {
throw "Invalid inverted section: " + (JSON.stringify(data)) + ": " + name;
} else {
@@ -87,7 +86,7 @@
return '';
case '@':
link = links ? links[name] : null;
- if (!(link != null)) {
+ if (link == null) {
if (wings.strict) {
throw "Invalid link: " + (JSON.stringify(links)) + ": " + name;
} else {
@@ -97,6 +96,7 @@
link = link.call(data);
}
return renderRawTemplate(replaceBraces(link), data, links);
+ case '~':
case '&':
case '':
value = data;
@@ -105,7 +105,7 @@
_ref = rest.match(/^([^.]*)\.?(.*)$/), all = _ref[0], part = _ref[1], rest = _ref[2];
value = value[part];
}
- if (!(value != null)) {
+ if (value == null) {
if (wings.strict) {
throw "Invalid value: " + (JSON.stringify(data)) + ": " + name;
} else {
@@ -114,7 +114,14 @@
} else if (typeof value === 'function') {
value = value.call(data);
}
- return (op === '&' ? value : escapeXML(value));
+ if (op === '~') {
+ return JSON.stringify(value);
+ } else if (op === '&') {
+ return value;
+ } else {
+ return escapeXML(value);
+ }
+ break;
default:
throw "Invalid section op: " + op;
}
View
8 src/test/wings-test.coffee
@@ -7,7 +7,7 @@ equal = assert.equal
if not vows.add
vows.add = (name, batch) -> vows.describe(name).addBatch(batch).export(module)
-vows.add 'templates'
+vows.add 'templates',
'basics:':
'an empty template':
topic: t('')
@@ -71,6 +71,12 @@ vows.add 'templates'
'should escape the html reserved characters': (topic) ->
equal topic, 'This shouldn\'t produce html: <b>bolded</b>'
+ 'a template with JSON tags':
+ topic: t('This should produce json: {~ob}', { ob: ['a', 1, { 'b': 3 }]})
+
+ 'should produce json': (topic) ->
+ equal topic, 'This should produce json: ["a",1,{"b":3}]'
+
'a template with unescaped tags':
topic: t('This should produce html: {&html}', {html: '<b>bolded</b>'})
View
11 src/wings/wings.coffee
@@ -26,7 +26,7 @@
parsePattern = ///
\{([:!]) \s* ([^}\s]*?) \s* \} ([\S\s]+?) \{/ \s* \2 \s* \} | # sections
\{(\#) [\S\s]+? \#\} | # comments
- \{([@&]?) \s* ([^}\s]*?) \s* \} # tags
+ \{([@&~]?) \s* ([^}\s]*?) \s* \} # tags
///mg
renderRawTemplate = (template, data, links) ->
@@ -95,7 +95,7 @@
return renderRawTemplate(replaceBraces(link), data, links)
- when '&', '' # value tag
+ when '~', '&', '' # value tag
value = data
rest = name
while value and rest
@@ -111,7 +111,12 @@
else if typeof value == 'function'
value = value.call(data)
- return (if op == '&' then value else escapeXML(value))
+ if op == '~'
+ return JSON.stringify(value)
+ else if op == '&'
+ return value
+ else
+ return escapeXML(value)
else
throw "Invalid section op: #{op}"
Please sign in to comment.
Something went wrong with that request. Please try again.