Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added template path in comment feature #10

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 8 additions & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,14 @@ module.exports = function(grunt) {
dest: 'tmp/file_header.js'
},

template_path_in_comment: {
options: {
templatePathInComment: true
},
src: ['test/fixtures/three.tpl.html'],
dest: 'tmp/template_path_in_comment.js'
},

rename: {
options: {
rename: function(moduleName) {
Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,13 @@ If specified, this string will get written at the top of the output
Template.js file. As an example, jshint directives such as
/* global angular: false */ can be put at the head of the file.

#### templatePathInComment:
Type: `String`
Default value: ``

If specified, adds an HTML comment containing the template file path
as a comment at the start of each template.

### Usage Examples

See the `Gruntfile.js` in the project source code for various configuration examples.
Expand Down
21 changes: 12 additions & 9 deletions tasks/html2js.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ module.exports = function(grunt) {

var path = require('path');

var escapeContent = function(content, quoteChar, indentString) {
var escapeContent = function(filepath, quoteChar, indentString, templatePathInComment) {
var comment = templatePathInComment ? '<!-- template: ' + filepath + ' -->\n' : '';
var content = comment + grunt.file.read(filepath);
var quoteRegexp = new RegExp('\\' + quoteChar, 'g');
var nlReplace = '\\n' + quoteChar + ' +\n' + indentString + indentString + quoteChar;
return content.replace(quoteRegexp, '\\' + quoteChar).replace(/\r?\n/g, nlReplace);
Expand All @@ -38,9 +40,9 @@ module.exports = function(grunt) {
};

// compile a template to an angular module
var compileTemplate = function(moduleName, filepath, quoteChar, indentString) {
var compileTemplate = function(moduleName, filepath, quoteChar, indentString, templatePathInComment) {

var content = escapeContent(grunt.file.read(filepath), quoteChar, indentString);
var content = escapeContent(filepath, quoteChar, indentString, templatePathInComment);
var doubleIndent = indentString + indentString;

var module = 'angular.module(' + quoteChar + moduleName +
Expand All @@ -52,8 +54,8 @@ module.exports = function(grunt) {
};

// compile a template to an angular module
var compileCoffeeTemplate = function(moduleName, filepath, quoteChar, indentString) {
var content = escapeContent(grunt.file.read(filepath), quoteChar, indentString);
var compileCoffeeTemplate = function(moduleName, filepath, quoteChar, indentString, templatePathInComment) {
var content = escapeContent(filepath, quoteChar, indentString, templatePathInComment);
var doubleIndent = indentString + indentString;

var module = 'angular.module(' + quoteChar + moduleName +
Expand All @@ -71,6 +73,7 @@ module.exports = function(grunt) {
module: 'templates-' + this.target,
quoteChar: '"',
fileHeaderString: '',
templatePathInComment: false,
indentString: ' ',
target: 'js'
});
Expand All @@ -90,11 +93,11 @@ module.exports = function(grunt) {
}
moduleNames.push("'" + moduleName + "'");
if (options.target === 'js') {
return compileTemplate(moduleName, filepath, options.quoteChar, options.indentString);
return compileTemplate(moduleName, filepath, options.quoteChar, options.indentString, options.templatePathInComment);
} else if (options.target === 'coffee') {
return compileCoffeeTemplate(moduleName, filepath, options.quoteChar, options.indentString);
return compileCoffeeTemplate(moduleName, filepath, options.quoteChar, options.indentString, options.templatePathInComment);
} else {
grunt.fail.fatal('Unknow target "' + options.target + '" specified');
grunt.fail.fatal('Unknown target "' + options.target + '" specified');
}

}).join(grunt.util.normalizelf('\n'));
Expand All @@ -113,7 +116,7 @@ module.exports = function(grunt) {
}
grunt.file.write(f.dest, fileHeader + bundle + modules);
});
//Just have one output, so if we making thirty files it only does one line
//Just have one output, so if we are making thirty files it only does one line
grunt.log.writeln("Successfully converted "+(""+this.files.length).green +
" html templates to " + options.target + ".");
});
Expand Down
9 changes: 9 additions & 0 deletions test/expected/template_path_in_comment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
angular.module('templates-template_path_in_comment', ['../test/fixtures/three.tpl.html']);

angular.module("../test/fixtures/three.tpl.html", []).run(["$templateCache", function($templateCache) {
$templateCache.put("../test/fixtures/three.tpl.html",
"<!-- template: test/fixtures/three.tpl.html -->\n" +
"Multiple\n" +
"Lines\n" +
"");
}]);
10 changes: 10 additions & 0 deletions test/html2js_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,16 @@ exports.html2js = {

test.done();
},
template_path_in_comment: function(test) {

test.expect(1);

assertFileContentsEqual(test, 'tmp/template_path_in_comment.js',
'test/expected/template_path_in_comment.js',
'expected template path in comment');

test.done();
},
rename: function(test) {

test.expect(1);
Expand Down