Skip to content

Commit

Permalink
Merge pull request #89 from bennorwood/master
Browse files Browse the repository at this point in the history
#83 Added pug template rendering capabilities.
  • Loading branch information
rquadling authored May 19, 2017
2 parents a2d1ec5 + c4f2414 commit 1049d8a
Show file tree
Hide file tree
Showing 14 changed files with 164 additions and 4 deletions.
30 changes: 30 additions & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,36 @@ module.exports = function (grunt) {
dest: 'tmp/process_all_jade.js'
},

process_pug: {
src: ['test/fixtures/process_pug.pug'],
dest: 'tmp/process_pug.js'
},

process_pug_custom: {
options: {
pug: {doctype: 'html'}
},
src: ['test/fixtures/process_pug_custom.pug'],
dest: 'tmp/process_pug_custom.js'
},

process_pug_with_include: {
options: {
pug: {}
},
src: ['test/fixtures/process_pug_with_include.pug'],
dest: 'tmp/process_pug_with_include.js'
},

process_all_pug: {
options: {
pug: {},
watch: true
},
src: ['test/fixtures/*.pug'],
dest: 'tmp/process_all_pug.js'
},

single_module: {
options: {
singleModule: true
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -321,3 +321,5 @@ In lieu of a formal styleguide, take care to maintain the existing coding style.
As of 0.3.7, this package is now administered by Richard Quadling who gives a big "Thank you" to Karl for his hard work.

0.3.8 Fix broken newlines (\r\r\n)

0.4.0 Added ability to render pug templates. Maintains Backwards compatibility. (#83)
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "grunt-html2js",
"description": "Compiles AngularJS templates to JavaScript",
"version": "0.3.8",
"version": "0.4.0",
"homepage": "https://github.com/rquadling/grunt-html2js",
"author": {
"name": "Karl Goldstein",
Expand Down Expand Up @@ -32,18 +32,19 @@
"test": "grunt test"
},
"devDependencies": {
"grunt-contrib-jshint": "~0.1.1",
"grunt": "~0.4.0",
"grunt-contrib-clean": "~0.4.0",
"grunt-contrib-jshint": "~0.1.1",
"grunt-contrib-nodeunit": "^0.3.3",
"grunt": "~0.4.0",
"grunt-conventional-changelog": "~1.0.0",
"load-grunt-tasks": "~0.2.0"
},
"peerDependencies": {
"grunt": ">=0.4.0"
},
"optionalDependencies": {
"jade": "^1.3.1"
"jade": "^1.3.1",
"pug": "^2.0.0-beta6"
},
"keywords": [
"gruntplugin"
Expand Down
10 changes: 10 additions & 0 deletions tasks/html2js.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ module.exports = function (grunt) {
var jadeExtension = /\.jade$/;
return jadeExtension.test(filepath);
}

function isPugTemplate(filepath) {
var pugExtension = /\.pug$/;
return pugExtension.test(filepath);
}

// return template content
var getContent = function (filepath, options) {
Expand All @@ -52,6 +57,10 @@ module.exports = function (grunt) {
var jade = require('jade');
options.jade.filename = filepath;
content = jade.render(content, options.jade);
} else if (isPugTemplate(filepath)) {
var pug = require('pug');
options.pug.filename = filepath;
content = pug.render(content, options.pug);
}

// Process files as templates if requested.
Expand Down Expand Up @@ -141,6 +150,7 @@ module.exports = function (grunt) {
htmlmin: {},
process: false,
jade: {pretty: true},
pug: { pretty: true },
singleModule: false,
existingModule: false,
watch: false,
Expand Down
21 changes: 21 additions & 0 deletions test/expected/process_all_pug.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
angular.module('templates-process_all_pug', ['../test/fixtures/process_pug.pug', '../test/fixtures/process_pug_custom.pug', '../test/fixtures/process_pug_with_include.pug', '../test/fixtures/pug_include.pug']);

angular.module("../test/fixtures/process_pug.pug", []).run(["$templateCache", function($templateCache) {
$templateCache.put("../test/fixtures/process_pug.pug",
"<p class=\"example\">Hello World!</p><div id=\"greeting\">Nice</div><div id=\"watch\">test</div>");
}]);

angular.module("../test/fixtures/process_pug_custom.pug", []).run(["$templateCache", function($templateCache) {
$templateCache.put("../test/fixtures/process_pug_custom.pug",
"<a href=\"href\">Great</a>");
}]);

angular.module("../test/fixtures/process_pug_with_include.pug", []).run(["$templateCache", function($templateCache) {
$templateCache.put("../test/fixtures/process_pug_with_include.pug",
"<h1>I'm an include!</h1>");
}]);

angular.module("../test/fixtures/pug_include.pug", []).run(["$templateCache", function($templateCache) {
$templateCache.put("../test/fixtures/pug_include.pug",
"<h1>I'm an include!</h1>");
}]);
21 changes: 21 additions & 0 deletions test/expected/process_all_pug_after_change.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
angular.module('templates-process_all_pug', ['../test/fixtures/process_pug.pug', '../test/fixtures/process_pug_custom.pug', '../test/fixtures/process_pug_with_include.pug', '../test/fixtures/pug_include.pug']);

angular.module("../test/fixtures/process_pug.pug", []).run(["$templateCache", function($templateCache) {
$templateCache.put("../test/fixtures/process_pug.pug",
"<p class=\"example\">Hello World!</p><div id=\"greeting\">Nice</div><div id=\"watch\">test</div>");
}]);

angular.module("../test/fixtures/process_pug_custom.pug", []).run(["$templateCache", function($templateCache) {
$templateCache.put("../test/fixtures/process_pug_custom.pug",
"<a href=\"href\">Great</a>");
}]);

angular.module("../test/fixtures/process_pug_with_include.pug", []).run(["$templateCache", function($templateCache) {
$templateCache.put("../test/fixtures/process_pug_with_include.pug",
"<h1>I'm an include!</h1>");
}]);

angular.module("../test/fixtures/pug_include.pug", []).run(["$templateCache", function($templateCache) {
$templateCache.put("../test/fixtures/pug_include.pug",
"<h1>I'm an include!</h1>");
}]);
7 changes: 7 additions & 0 deletions test/expected/process_pug.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
angular.module('templates-process_pug', ['../test/fixtures/process_pug.pug']);

angular.module("../test/fixtures/process_pug.pug", []).run(["$templateCache", function($templateCache) {
$templateCache.put("../test/fixtures/process_pug.pug",
"<p class=\"example\">Hello World!</p>\n" +
"<div id=\"greeting\">Nice</div>");
}]);
6 changes: 6 additions & 0 deletions test/expected/process_pug_custom.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
angular.module('templates-process_pug_custom', ['../test/fixtures/process_pug_custom.pug']);

angular.module("../test/fixtures/process_pug_custom.pug", []).run(["$templateCache", function($templateCache) {
$templateCache.put("../test/fixtures/process_pug_custom.pug",
"<a href>Great</a>");
}]);
6 changes: 6 additions & 0 deletions test/expected/process_pug_with_include.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
angular.module('templates-process_pug_with_include', ['../test/fixtures/process_pug_with_include.pug']);

angular.module("../test/fixtures/process_pug_with_include.pug", []).run(["$templateCache", function($templateCache) {
$templateCache.put("../test/fixtures/process_pug_with_include.pug",
"<h1>I'm an include!</h1>");
}]);
2 changes: 2 additions & 0 deletions test/fixtures/process_pug.pug
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
p.example Hello World!
#greeting Nice
1 change: 1 addition & 0 deletions test/fixtures/process_pug_custom.pug
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a(href) Great
1 change: 1 addition & 0 deletions test/fixtures/process_pug_with_include.pug
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include ./pug_include
1 change: 1 addition & 0 deletions test/fixtures/pug_include.pug
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
h1 I'm an include!
51 changes: 51 additions & 0 deletions test/html2js_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,57 @@ exports.html2js = {
test.done();
}, 1000);

},
process_pug: function (test) {
test.expect(1);

assertFileContentsEqual(test, 'tmp/process_pug.js',
'test/expected/process_pug.js',
'expected pug template to be processed');

test.done();
},
process_pug_with_custom_options: function (test) {
test.expect(1);

assertFileContentsEqual(test, 'tmp/process_pug_custom.js',
'test/expected/process_pug_custom.js',
'expected pug template to be processed with custom options');

test.done();
},
process_pug_with_include: function (test) {
test.expect(1);
assertFileContentsEqual(test, 'tmp/process_pug_with_include.js',
'test/expected/process_pug_with_include.js',
'expected pug template to be processed with custom options');

test.done();
},
process_all_pug: function (test) {
test.expect(1);

// This test is run with options.watch on
// We need to edit a fixture file to make sure it is watched and re-compiled
var file2Change = 'test/fixtures/process_pug.pug';
var contents = grunt.file.read(file2Change);
var newContents = contents + "\n#watch test";

// Write edited fixture file
grunt.file.write(file2Change, grunt.util.normalizelf(newContents));

// wait for the watch-change to process
setTimeout(function () {
// Check re-compiled with changes were added
assertFileContentsEqual(test, 'tmp/process_all_pug.js',
'test/expected/process_all_pug_after_change.js',
'expected pug template to be processed with custom options');

//reset fixture file to original contents
grunt.file.write(file2Change, grunt.util.normalizelf(contents));
test.done();
}, 1500);

},
single_module: function (test) {
test.expect(1);
Expand Down

0 comments on commit 1049d8a

Please sign in to comment.