Skip to content

Commit

Permalink
Added a processContent option
Browse files Browse the repository at this point in the history
  • Loading branch information
yuku committed Dec 29, 2012
1 parent 5b469ca commit e933f45
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 9 deletions.
10 changes: 10 additions & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,16 @@ module.exports = function(grunt) {
files: {
"tmp/ns_nested_this.js": ["test/fixtures/template.html"]
}
},
process_content: {
options: {
processContent: function (src) {
return src.replace(/(^\s+|\s+$)/gm, '');
}
},
files: {
"tmp/process_content.js": ["test/fixtures/indent_template.html"]
}
}
},

Expand Down
28 changes: 22 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,22 @@ options: {
}
```

#### processContent
Type: ```function```

This option accepts a function which takes one argument (the file content) and
returns a string which will be used as template string.
The example below strips whitespace characters from the beginning and the end of
each line.

```javascript
options: {
processContent: function(src) {
return src.replace(/(^\s+|\s+$)/gm, '');
}
}
```

### Usage Examples

```js
Expand All @@ -119,14 +135,14 @@ jst: {

## Release History

* 2012-10-11   v0.3.1   Rename grunt-contrib-lib dep to grunt-lib-contrib.
* 2012-08-22   v0.3.0   Options no longer accepted from global config key.
* 2012-08-15   v0.2.3   Support for nested namespaces.
* 2012-08-11   v0.2.2   Added processName functionality & escaping single quotes in filenames.
* 2012-08-09   v0.2.0   Refactored from grunt-contrib into individual repo.
* 2012-10-12   v0.3.1   Rename grunt-contrib-lib dep to grunt-lib-contrib.
* 2012-08-23   v0.3.0   Options no longer accepted from global config key.
* 2012-08-16   v0.2.3   Support for nested namespaces.
* 2012-08-12   v0.2.2   Added processName functionality & escaping single quotes in filenames.
* 2012-08-10   v0.2.0   Refactored from grunt-contrib into individual repo.

---

Task submitted by [Tim Branyen](http://tbranyen.com)

*This file was generated on Wed Nov 28 2012 08:40:57.*
*This file was generated on Sat Dec 29 2012 22:17:11.*
16 changes: 16 additions & 0 deletions docs/jst-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,19 @@ options: {
amdWrapper: true
}
```

## processContent
Type: ```function```

This option accepts a function which takes one argument (the file content) and
returns a string which will be used as template string.
The example below strips whitespace characters from the beginning and the end of
each line.

```javascript
options: {
processContent: function(src) {
return src.replace(/(^\s+|\s+$)/gm, '');
}
}
```
5 changes: 3 additions & 2 deletions tasks/jst.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ module.exports = function(grunt) {
var helpers = require('grunt-lib-contrib').init(grunt);
var options = this.options({
namespace: 'JST',
templateSettings: {}
templateSettings: {},
processContent: function (src) { return src; }
});

// assign filename transformation functions
Expand All @@ -33,7 +34,7 @@ module.exports = function(grunt) {

var files = this.file.src;
var output = files.map(function(file) {
var src = grunt.file.read(file);
var src = options.processContent(grunt.file.read(file));

try {
compiled = _.template(src, false, options.templateSettings).source;
Expand Down
11 changes: 11 additions & 0 deletions test/expected/process_content.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
this["JST"] = this["JST"] || {};

this["JST"]["test/fixtures/indent_template.html"] = function(obj){
var __p='';var print=function(){__p+=Array.prototype.join.call(arguments, '')};
with(obj||{}){
__p+='<div>\n<div>\n<div>\n'+
( name )+
'\n</div>\n</div>\n</div>';
}
return __p;
};
7 changes: 7 additions & 0 deletions test/fixtures/indent_template.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<div>
<div>
<div>
<%= name %>
</div>
</div>
</div>
6 changes: 5 additions & 1 deletion test/jst_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ exports['jst'] = {

var expect, result;

test.expect(7);
test.expect(8);

expect = grunt.file.read("test/expected/jst.js");
result = grunt.file.read("tmp/jst.js");
Expand Down Expand Up @@ -36,6 +36,10 @@ exports['jst'] = {
result = grunt.file.read("tmp/pretty_amd.js");
test.equal(expect, result, "should make the AMD wrapper output pretty");

expect = grunt.file.read("test/expected/process_content.js");
result = grunt.file.read("tmp/process_content.js");
test.equal(expect, result, "should convert file content");

test.done();
}
};

0 comments on commit e933f45

Please sign in to comment.