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

add processContent option #50

Merged
merged 2 commits into from
Apr 7, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}

function findPartials(tree) {
var partials = [];
Expand Down Expand Up @@ -68,6 +71,7 @@ function getOptions(opts) {
var compiler = defaultCompiler;
var precompiler = defaultPrecompiler;
var traverse = defaultTraverse;
var processContent = defaultProcessContent;

opts = opts || {};

Expand All @@ -87,13 +91,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 @@ -102,6 +111,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 = "// hbsfy compiled Handlebars template\n";
Expand All @@ -110,6 +120,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, '');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know this is just the test, but isn't this kind of transformation dangerous? What about <pre> tags?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will remove all spaces in <pre> tag for sure 😬. But the regular expression can be easily modified due your requirements.

}
}))
.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>");
});