Skip to content

Commit

Permalink
First version
Browse files Browse the repository at this point in the history
The first version of the package.
  • Loading branch information
Mark Lagendijk committed Jan 4, 2014
0 parents commit 39b38fe
Show file tree
Hide file tree
Showing 15 changed files with 583 additions and 0 deletions.
17 changes: 17 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# http://editorconfig.org
root = true

[*]
indent_style = tab
indent_size = 4
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.md]
trim_trailing_whitespace = false

[test/fixtures/*]
insert_final_newline = false
trim_trailing_whitespace = false
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.DS_Store
node_modules/
temp/
.idea/
21 changes: 21 additions & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"node": true,
"esnext": true,
"bitwise": true,
"camelcase": true,
"curly": true,
"eqeqeq": true,
"immed": true,
"indent": 4,
"latedef": true,
"newcap": true,
"noarg": true,
"quotmark": "double",
"regexp": true,
"undef": true,
"unused": true,
"strict": true,
"trailing": true,
"smarttabs": true,
"white": true
}
7 changes: 7 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
language: node_js
node_js:
- '0.8'
- '0.10'
before_install:
- currentfolder=${PWD##*/}
- if [ "$currentfolder" != 'generator-gulp-plugin' ]; then cd .. && eval "mv $currentfolder generator-gulp-plugin" && cd generator-gulp-plugin; fi
20 changes: 20 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Copyright 2014 Mark Lagendijk

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
59 changes: 59 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
(PLUGIN AUTHOR: Please read [Plugin README conventions](https://github.com/wearefractal/gulp/wiki/Plugin-README-Conventions), then delete this line)

# gulp-ng-html2js [![NPM version][npm-image]][npm-url] [![Build Status][travis-image]][travis-url] [![Dependency Status][depstat-image]][depstat-url]

> ng-html2js plugin for [gulp](https://github.com/wearefractal/gulp)
## Usage

First, install `gulp-ng-html2js` as a development dependency:

```shell
npm install --save-dev gulp-ng-html2js
```

Then, add it to your `gulpfile.js`:

```javascript
var ngHtml2Js = require("gulp-ng-html2js");

gulp.src("./partials/*.html")
.pipe(ngHtml2Js({
moduleName: "MyAwesomePartials",
prefix: "/partials"
}))
.pipe(gulp.dest("./dist/partials"));
```

## API

### ngHtml2Js(options)

#### options.moduleName
Type: `String`

The name of the generated AngularJS module. Uses the file url if omitted.

#### options.prefix
Type: `String`

The prefix which should be prepended to the file path to generate the file url.

#### options.stripPrefix
Type: `String`

The prefix which should be subtracted from the file path to generate the file url.


## License

[MIT License](http://en.wikipedia.org/wiki/MIT_License)

[npm-url]: https://npmjs.org/package/gulp-ng-html2js
[npm-image]: https://badge.fury.io/js/gulp-ng-html2js.png

[travis-url]: http://travis-ci.org/marklagendijk/gulp-ng-html2js
[travis-image]: https://secure.travis-ci.org/marklagendijk/gulp-ng-html2js.png?branch=master

[depstat-url]: https://david-dm.org/marklagendijk/gulp-ng-html2js
[depstat-image]: https://david-dm.org/marklagendijk/gulp-ng-html2js.png
98 changes: 98 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
var util = require("util");
var es = require("event-stream");

var TEMPLATE = 'angular.module(\'%s\', []).run(function($templateCache) {\n' +
' $templateCache.put(\'%s\',\n \'%s\');\n' +
'});\n';

var SINGLE_MODULE_TPL = '(function(module) {\n' +
'try {\n' +
' module = angular.module(\'%s\');\n' +
'} catch (e) {\n' +
' module = angular.module(\'%s\', []);\n' +
'}\n' +
'module.run(function($templateCache) {\n' +
' $templateCache.put(\'%s\',\n \'%s\');\n' +
'});\n' +
'})();\n';

/**
* @param [options] - The plugin options
* @param [options.moduleName] - The name of the module which will be generated. When omitted the fileUrl will be used.
* @param [options.stripPrefix] - The prefix which should be stripped from the file path
* @param [options.prefix] - The prefix which should be added to the start of the url
* @returns {stream}
*/
module.exports = function(options){
"use strict";

function ngHtml2Js(file, callback){
if(file.isStream()){
return callback(new Error("gulp-ng-html2js: Streaming not supported"));
}

if(file.isBuffer()){
var filePath = getFileUrl(file, options);
file.contents = new Buffer(generateModuleDeclaration(filePath, String(file.contents), options));
file.path = file.path.replace(".html", ".js");
}

return callback(null, file);
}

/**
* Generates the Javascript code containing the AngularJS module which puts the HTML file into the $templateCache.
* @param fileUrl - The url with which the HTML will be registered in the $templateCache.
* @param contents - The contents of the HTML file.
* @param [options] - The plugin options
* @param [options.moduleName] - The name of the module which will be generated. When omitted the fileUrl will be used.
* @returns {string} - The generated Javascript code.
*/
function generateModuleDeclaration(fileUrl, contents, options){
var escapedContent = escapeContent(contents);
if(options && options.moduleName){
return util.format(SINGLE_MODULE_TPL, options.moduleName, options.moduleName, fileUrl, escapedContent);
}
else{
return util.format(TEMPLATE, fileUrl, fileUrl, escapedContent);
}
}

/**
* Generates the url of a file.
* @param file - The file for which a url should be generated
* @param [options] - The plugin options
* @param [options.stripPrefix] - The prefix which should be stripped from the file path
* @param [options.prefix] - The prefix which should be added to the start of the url
* @returns {string}
*/
function getFileUrl(file, options){
// Start with the relative file path
var url = file.relative;

// Replace '\' with '/' (Windows)
url = url.replace(/\\/g, '/');

// Remove the stripPrefix
if(options && options.stripPrefix && url.indexOf(options.stripPrefix) === 0){
url = url.replace(options.stripPrefix, '');
}
// Add the prefix
if(options && options.prefix){
url = options.prefix + url;
}

return url;
}

/**
* Escapes the content of an string so it can be used in a Javascript string declaration
* @param {string} content
* @returns {string}
*/
function escapeContent(content){
return content.replace(/\\/g, '\\\\').replace(/'/g, '\\\'').replace(/\r?\n/g, '\\n\' +\n \'');
}

return es.map(ngHtml2Js);
};
43 changes: 43 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"name": "gulp-ng-html2js",
"version": "0.1.0",
"description": "A Gulp plugin which generates Javascript code which loads the HTML files of your AngularJS app into the $templateCache.",
"keywords": [
"gulpplugin",
"ng-html2js",
"angular",
"html2js",
"angularjs"
],
"homepage": "https://github.com/marklagendijk/gulp-ng-html2js",
"bugs": "https://github.com/marklagendijk/gulp-ng-html2js/issues",
"author": {
"name": "Mark Lagendijk",
"url": "https://github.com/marklagendijk"
},
"main": "./index.js",
"repository": {
"type": "git",
"url": "git://github.com/marklagendijk/gulp-ng-html2js.git"
},
"scripts": {
"test": "mocha"
},
"dependencies": {
"event-stream": "*"
},
"devDependencies": {
"mocha": "~1.14.0",
"should": "~2.1.0",
"gulp-util": "~2.2.0"
},
"engines": {
"node": ">=0.8.0",
"npm": ">=1.2.10"
},
"licenses": [
{
"type": "MIT"
}
]
}
36 changes: 36 additions & 0 deletions test/expected/example.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
angular.module('fixtures/example.html', []).run(function($templateCache) {
$templateCache.put('fixtures/example.html',
'<!doctype html>\n' +
'<html>\n' +
' <head>\n' +
' <title>Example</title>\n' +
'\n' +
' <meta charset="utf-8" />\n' +
' <meta http-equiv="Content-type" content="text/html; charset=utf-8" />\n' +
' <meta name="viewport" content="width=device-width, initial-scale=1" />\n' +
' <style type="text/css">\n' +
' body {\n' +
' margin: 0;\n' +
' padding: 0;\n' +
' font-family: "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;\n' +
' background-color: #f0f0f2;\n' +
'\n' +
' }\n' +
' </style>\n' +
' <script type="text/javascript">\n' +
' function someInlineScript(){\n' +
' alert("This is some \'inline script")\n' +
' }\n' +
' </script>\n' +
' </head>\n' +
' <body>\n' +
' <ul>\n' +
' <li ng-repeat="item in items">{{ item.name }}</li>\n' +
' </ul>\n' +
' <ul>\n' +
' <li ng-repeat=\'thingy in thingies\'>{{ thingy.name }}</li>\n' +
' </ul>\n' +
' </body>\n' +
'</html>\n' +
'');
});
43 changes: 43 additions & 0 deletions test/expected/exampleWithModuleName.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
(function(module) {
try {
module = angular.module('myAwesomePartials');
} catch (e) {
module = angular.module('myAwesomePartials', []);
}
module.run(function($templateCache) {
$templateCache.put('fixtures/example.html',
'<!doctype html>\n' +
'<html>\n' +
' <head>\n' +
' <title>Example</title>\n' +
'\n' +
' <meta charset="utf-8" />\n' +
' <meta http-equiv="Content-type" content="text/html; charset=utf-8" />\n' +
' <meta name="viewport" content="width=device-width, initial-scale=1" />\n' +
' <style type="text/css">\n' +
' body {\n' +
' margin: 0;\n' +
' padding: 0;\n' +
' font-family: "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;\n' +
' background-color: #f0f0f2;\n' +
'\n' +
' }\n' +
' </style>\n' +
' <script type="text/javascript">\n' +
' function someInlineScript(){\n' +
' alert("This is some \'inline script")\n' +
' }\n' +
' </script>\n' +
' </head>\n' +
' <body>\n' +
' <ul>\n' +
' <li ng-repeat="item in items">{{ item.name }}</li>\n' +
' </ul>\n' +
' <ul>\n' +
' <li ng-repeat=\'thingy in thingies\'>{{ thingy.name }}</li>\n' +
' </ul>\n' +
' </body>\n' +
'</html>\n' +
'');
});
})();
36 changes: 36 additions & 0 deletions test/expected/exampleWithPrefix.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
angular.module('/partials/fixtures/example.html', []).run(function($templateCache) {
$templateCache.put('/partials/fixtures/example.html',
'<!doctype html>\n' +
'<html>\n' +
' <head>\n' +
' <title>Example</title>\n' +
'\n' +
' <meta charset="utf-8" />\n' +
' <meta http-equiv="Content-type" content="text/html; charset=utf-8" />\n' +
' <meta name="viewport" content="width=device-width, initial-scale=1" />\n' +
' <style type="text/css">\n' +
' body {\n' +
' margin: 0;\n' +
' padding: 0;\n' +
' font-family: "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;\n' +
' background-color: #f0f0f2;\n' +
'\n' +
' }\n' +
' </style>\n' +
' <script type="text/javascript">\n' +
' function someInlineScript(){\n' +
' alert("This is some \'inline script")\n' +
' }\n' +
' </script>\n' +
' </head>\n' +
' <body>\n' +
' <ul>\n' +
' <li ng-repeat="item in items">{{ item.name }}</li>\n' +
' </ul>\n' +
' <ul>\n' +
' <li ng-repeat=\'thingy in thingies\'>{{ thingy.name }}</li>\n' +
' </ul>\n' +
' </body>\n' +
'</html>\n' +
'');
});
Loading

0 comments on commit 39b38fe

Please sign in to comment.