Skip to content
This repository has been archived by the owner on Oct 19, 2023. It is now read-only.

Commit

Permalink
Merge pull request #50 from romanyanke/process-content
Browse files Browse the repository at this point in the history
add processContent option
  • Loading branch information
kirbysayshi committed Apr 7, 2016
2 parents 214683b + 9c718d0 commit 391cd55
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 1 deletion.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,19 @@ require.extensions['.hbs'] = function (module, filename) {
}
```

### Process output HTML string

This option accepts a function which takes one argument (the template file content) and returns a string which will be used as the source for the precompiled template object. The example below removes leading and trailing spaces to shorten templates.

```
hbsfy.configure({
processContent: function(content) {
content = content.replace(/^[\x20\t]+/mg, '').replace(/[\x20\t]+$/mg, '');
content = content.replace(/^[\r\n]+/, '').replace(/[\r\n]*$/, '\n');
return content;
}
});
```

## Changelog

Expand Down
13 changes: 12 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ var defaultExtensions = {
handlebar: true,
handlebars: true
};
var defaultProcessContent = function(content) {
return content;
}

var MARKER = "// hbsfy compiled Handlebars template\n";

Expand Down Expand Up @@ -70,6 +73,7 @@ function getOptions(opts) {
var compiler = defaultCompiler;
var precompiler = defaultPrecompiler;
var traverse = defaultTraverse;
var processContent = defaultProcessContent;

opts = opts || {};

Expand All @@ -89,13 +93,18 @@ function getOptions(opts) {
if (opts.t || opts.traverse) {
traverse = opts.t || opts.traverse;
}

if (opts.pc || opts.processContent) {
processContent = opts.pc || opts.processContent;
}
}

return xtend({}, opts, {
extensions: extensions,
precompiler: precompiler,
compiler: compiler,
traverse: traverse
traverse: traverse,
processContent: processContent
});
}

Expand All @@ -104,6 +113,7 @@ function compile(file, opts) {
var compiler = options.compiler;
var precompiler = options.precompiler;
var traverse = options.traverse;
var processContent = options.processContent;

var js;
var compiled = MARKER;
Expand All @@ -112,6 +122,7 @@ function compile(file, opts) {

// Kill BOM
file = file.replace(/^\uFEFF/, '');
file = processContent(file);

if (traverse) {
parsed = precompiler.parse(file);
Expand Down
3 changes: 3 additions & 0 deletions test/indentation.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<header>
<h1>Hi {{name}}!</h1>
</header>
32 changes: 32 additions & 0 deletions test/indentation_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*jshint node: true*/

var fs = require("fs");
var assert = require("assert");

var hbsfy = require("hbsfy");
var Handlebars = require("hbsfy/runtime");


var templatePath = __dirname + "/indentation.hbs";
var exported = __dirname + "/compiled.js";

try {
fs.unlinkSync(exported);
} catch (err) { }

fs.createReadStream(templatePath)
.pipe(hbsfy(templatePath, {
processContent: function(content) {
return content
.replace(/^[\x20\t]+/mg, '')
.replace(/[\x20\t]+$/mg, '')
.replace(/[\r\n]/g, '');
}
}))
.pipe(fs.createWriteStream(exported))
.on("close", function() {
var template = require(exported);
var res = template({ name: "Bob" });
assert.equal(res, "<header><h1>Hi Bob!</h1></header>");
});

0 comments on commit 391cd55

Please sign in to comment.