Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Adding include functionality #1

Merged
merged 1 commit into from

4 participants

@kristoferjoseph

Added include functionality

@julien julien merged commit 3989c95 into from
@julien
Collaborator

Thanks @kristoferjoseph this is a great addition. Will merge and publish to npm

@dciccale

Avoid using replace to include other files, check the issue (and the PR) here:

PR:
dciccale/grunt-processhtml#25
issue:
dciccale/grunt-processhtml#24

feel free to copy paste the solution

@dciccale

Also you could try implementing the recurse option to also pre-process included files https://github.com/dciccale/grunt-processhtml/blob/master/lib/blocktypes.js#L55

@julien
Collaborator
@julien
Collaborator

Fixed now

@shilov

It seems the path is relative to node_modules/gulp-processhtml rather than the file that's being processed.

Collaborator

Thanks @shilov, saved me some time, sorry for the late reply; 0.0.6 should fix the problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 7, 2014
  1. @kristoferjoseph
This page is out of date. Refresh to see the latest.
Showing with 72 additions and 15 deletions.
  1. +14 −8 index.js
  2. +1 −0  test/fixtures/styles.css
  3. +57 −7 test/main.js
View
22 index.js
@@ -4,10 +4,11 @@ var through = require('through')
, gutil = require('gulp-util')
, PluginError = gutil.PluginError
, File = gutil.File
+ , fs = require('fs')
, transformer;
-transformer = {
+transformer = {
replace: function (content, section, line, asset) {
return content.replace(line, section.indent + asset);
},
@@ -31,12 +32,17 @@ transformer = {
js: function (content, section, line) {
return transformer.replace(content, section, line, '<script src="' + section.asset + '"></script>');
},
-
+
remove: function (content, section, line) {
var escaped = line.replace(/([.?*+\^=!:$\[\]\\(){}|\-])/g, '\\$1')
, regReplace = new RegExp(escaped.replace(/^\s*|[\r\n]+\s*/g, '\\s*').replace(/\n{1}$/g, '\\n'));
return content.replace(regReplace, '');
- }
+ },
+
+ include: function (content, section, line, asset) {
+ var file = fs.readFileSync(path.join(__dirname, section.asset), 'utf8');
+ return content.replace(line, section.indent + file.toString().trim());
+ }
};
function findSections(content, marker) {
@@ -52,7 +58,7 @@ function findSections(content, marker) {
, buildStart
, buildEnd
, attr;
-
+
regStart = new RegExp('<!--\\s*' + marker + ':(\\[?[\\w-]+\\]?)(?::(\\w+))?(?:\\s*([^\\s]+)\\s*-->)*');
regEnd = new RegExp('(?:<!--\\s*)*\\/' + marker + '\\s*-->');
lines = content.replace(/\r\n/g, '\n').split(/\n/);
@@ -62,7 +68,7 @@ function findSections(content, marker) {
line = lines[i];
buildStart = line.match(regStart);
buildEnd = regEnd.test(line);
-
+
if (buildStart) {
inside = true;
attr = buildStart[1].match(/(?:\[([\w-]+)\])*/)[1];
@@ -83,7 +89,7 @@ function findSections(content, marker) {
if (inside && buildEnd) {
inside = false;
sections.push(section);
- }
+ }
}
return sections;
}
@@ -123,7 +129,7 @@ module.exports = function(fileName, opt){
if (!firstFile) {
firstFile = file;
}
-
+
contents = file.contents.toString('utf8');
sections = findSections(contents, opt.marker);
@@ -145,7 +151,7 @@ module.exports = function(fileName, opt){
if (buffer.length === 0) {
return this.emit('end');
}
-
+
joinedContents = buffer.join(opt.newLine);
joinedPath = path.join(firstFile.base, fileName);
View
1  test/fixtures/styles.css
@@ -0,0 +1 @@
+body { background: teal; }
View
64 test/main.js
@@ -48,7 +48,7 @@ describe('gulp-processhtml', function() {
newFilePath.should.equal(expectedFilePath);
newFile.relative.should.equal('processed.html');
-
+
Buffer.isBuffer(newFile.contents).should.equal(true);
String(newFile.contents).should.equal([
'<!doctype html>',
@@ -61,8 +61,8 @@ describe('gulp-processhtml', function() {
'</body>',
'</html>'
].join('\n'));
-
-
+
+
done();
});
stream.write(fakeFile);
@@ -96,7 +96,7 @@ describe('gulp-processhtml', function() {
should.exist(newFile.path);
should.exist(newFile.relative);
should.exist(newFile.contents);
-
+
Buffer.isBuffer(newFile.contents).should.equal(true);
String(newFile.contents).should.equal([
'<!doctype html>',
@@ -106,7 +106,7 @@ describe('gulp-processhtml', function() {
'</body>',
'</html>'
].join('\n'));
-
+
done();
});
stream.write(fakeFile);
@@ -140,7 +140,7 @@ describe('gulp-processhtml', function() {
should.exist(newFile.path);
should.exist(newFile.relative);
should.exist(newFile.contents);
-
+
Buffer.isBuffer(newFile.contents).should.equal(true);
String(newFile.contents).should.equal([
'<!doctype html>',
@@ -151,7 +151,7 @@ describe('gulp-processhtml', function() {
'</body>',
'</html>'
].join('\n'));
-
+
done();
});
stream.write(fakeFile);
@@ -159,7 +159,57 @@ describe('gulp-processhtml', function() {
});
+ it('should include files', function(done) {
+ var stream = processhtml('processed.html')
+ , fakeFile = new File({
+ cwd: './',
+ base: './',
+ path: './file.html',
+ contents: new Buffer([
+ '<!doctype html>',
+ '<html>',
+ '<head>',
+ ' <style>',
+ ' <!-- build:include test/fixtures/styles.css -->',
+ ' <!-- Me thinks it should replace whatever is in between the build tags -->',
+ ' <div>FOO</div>',
+ ' <!-- /build -->',
+ ' </style>',
+ '</head>',
+ '<body>',
+ '</body>',
+ '</html>'
+ ].join('\n'))
+ });
+ stream.on('data', function (newFile) {
+ var newFilePath
+ , expectedFilePath;
+
+ should.exist(newFile);
+ should.exist(newFile.path);
+ should.exist(newFile.relative);
+ should.exist(newFile.contents);
+
+ Buffer.isBuffer(newFile.contents).should.equal(true);
+ String(newFile.contents).should.equal([
+ '<!doctype html>',
+ '<html>',
+ '<head>',
+ ' <style>',
+ ' body { background: teal; }',
+ ' </style>',
+ '</head>',
+ '<body>',
+ '</body>',
+ '</html>'
+ ].join('\n'));
+
+ done();
+ });
+ stream.write(fakeFile);
+ stream.end();
+ });
});
});
Something went wrong with that request. Please try again.