├── .editorconfig ├── .gitattributes ├── .gitignore ├── .jshintrc ├── .travis.yml ├── README.md ├── bin └── ng-html2js ├── package.json ├── spec ├── ng-html2js_spec.js ├── support │ └── jasmine.json ├── test.tmpl └── testOutputs │ ├── expectedTestTmplJs │ ├── expectedTestTmplJsStrippedPrefix │ ├── expectedTestTmplJsWithModule │ ├── expectedTestTmplJsWithModuleAndModuleVar │ ├── expectedTestTmplJsWithModuleVar │ ├── expectedTestTmplJsWithoutBaseDir │ └── expectedTestTmplOutputFileJs └── src ├── html2js.js ├── singleModule.tmpl └── template.tmpl /.editorconfig: -------------------------------------------------------------------------------- 1 | # EditorConfig helps developers define and maintain consistent 2 | # coding styles between different editors and IDEs 3 | # editorconfig.org 4 | 5 | root = true 6 | 7 | [*] 8 | indent_style = space 9 | indent_size = 2 10 | end_of_line = lf 11 | charset = utf-8 12 | trim_trailing_whitespace = true 13 | insert_final_newline = true 14 | 15 | [*.md] 16 | trim_trailing_whitespace = false 17 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /node_modules/ 2 | /spec/testOutputFile 3 | -------------------------------------------------------------------------------- /.jshintrc: -------------------------------------------------------------------------------- 1 | { 2 | "node": true, 3 | "browser": true, 4 | "esnext": true, 5 | "bitwise": true, 6 | "camelcase": true, 7 | "curly": true, 8 | "eqeqeq": true, 9 | "immed": true, 10 | "indent": 2, 11 | "latedef": true, 12 | "newcap": true, 13 | "noarg": true, 14 | "quotmark": "single", 15 | "regexp": true, 16 | "undef": true, 17 | "unused": true, 18 | "strict": true, 19 | "trailing": true, 20 | "smarttabs": true 21 | } 22 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | language: node_js 3 | node_js: 4 | - "0.12" 5 | - "0.10" 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ng-html2js [![Analytics](https://ga-beacon.appspot.com/UA-2694988-7/ng-html2js/readme?pixel)](https://github.com/yaru22/ng-html2js) 2 | ========== 3 | [![Build Status](https://secure.travis-ci.org/yaru22/ng-html2js.png)](http://travis-ci.org/yaru22/ng-html2js) 4 | [![Dependency Status](https://gemnasium.com/yaru22/ng-html2js.png)](https://gemnasium.com/yaru22/ng-html2js) 5 | 6 | Standalone script to turn Angular template into js and put it in a module. 7 | 8 | 9 | Usage 10 | ----- 11 | ``` 12 | $ ng-html2js inputFile [outputFile] [-m moduleName] [--module-var ngModule] 13 | ``` 14 | 15 | If you specify only inputFile, it will display the result to the console. 16 | 17 | If you don't specify moduleName, inputFile will be the name of the module. 18 | ``` 19 | $ ng-html2js test/test.tmpl 20 | var module = angular.module('test/test.tmpl', []); 21 | module.run(['$templateCache', function($templateCache) { 22 | $templateCache.put('test/test.tmpl', 23 | '
\n' + 24 | ' hello world\n' + 25 | '
\n' + 26 | ' {{ item }} it\'s value is great\n' + 27 | '
\n' + 28 | '
\n' + 29 | ''); 30 | }]); 31 | ``` 32 | 33 | If you specify moduleName, the template will belong to that module. 34 | ``` 35 | $ ng-html2js test/test.tmpl -m foo --module-var ngModule 36 | var ngModule; 37 | try { 38 | ngModule = angular.module('foo'); 39 | } catch (e) { 40 | ngModule = angular.module('foo', []); 41 | } 42 | 43 | ngModule.run(['$templateCache', function ($templateCache) { 44 | $templateCache.put('test/test.tmpl', 45 | '
\n' + 46 | ' hello world\n' + 47 | '
\n' + 48 | ' {{ item }} it\'s value is great\n' + 49 | '
\n' + 50 | '
\n' + 51 | ''); 52 | }]); 53 | ``` 54 | 55 | 56 | License 57 | ------- 58 | This seed is released under permissive MIT License. 59 | -------------------------------------------------------------------------------- /bin/ng-html2js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | var fs = require('fs'); 4 | var optimist = require('optimist'); 5 | var path = require('path'); 6 | var html2js = require('../src/html2js'); 7 | 8 | // 9 | // Setup usage and parse arguments 10 | // 11 | 12 | var argv = optimist.usage('Usage: $0 ') 13 | // -m, --module option 14 | .alias('m', 'module') 15 | .string('m') 16 | .describe('m', 'If module name is provided, template will be packaged under this module.') 17 | // --module-var option 18 | .string('module-var') 19 | .describe('module-var', '--module-var is to be used with --module. If module-var is provided, it will pass module-var to immediately-invoked function expression (IIFE). Its default value is "module".') 20 | // -b, --basedir option 21 | .alias('b', 'basedir') 22 | .string('b') 23 | .describe('b', 'If basedir is provided, basedir will be removed from the templates\' path.') 24 | // -s, --strip-prefix option 25 | .alias('s', 'strip-prefix') 26 | .string('s') 27 | .describe('s', 'If strip-prefix is provided, strip-prefix will remove the given prefix from the filename') 28 | // -h, --help option 29 | .alias('h', 'help') 30 | .boolean('h') 31 | .describe('h', 'Shows usage message; what you are looking at.') 32 | .argv; 33 | 34 | if (argv._.length === 0 || argv.help) { 35 | console.log(optimist.help()); 36 | process.exit(); 37 | } 38 | 39 | var inputFile = argv._[0]; 40 | var outputFile = argv._[1]; 41 | var moduleName = argv.m; 42 | var moduleVar = argv['module-var']; 43 | var baseDir = argv.b; 44 | var prefixToStrip = argv.s; 45 | 46 | // 47 | // Main script 48 | // 49 | 50 | var wordsMatchLeftJustified = function(word1, word2) { 51 | return word1.slice(0, word2.length) === word2; 52 | }; 53 | 54 | var content = fs.readFileSync(inputFile, 'utf8'); 55 | var inputAlias = inputFile; 56 | if(baseDir){ 57 | baseDir = path.resolve(baseDir); 58 | inputAlias = inputAlias.replace(baseDir, ''); 59 | } 60 | if (prefixToStrip) { 61 | if(wordsMatchLeftJustified(inputAlias, prefixToStrip) && (prefixToStrip !== inputAlias)) { 62 | inputAlias = inputAlias.replace(prefixToStrip, ''); 63 | } 64 | } 65 | inputAlias = inputAlias.replace(/\\/g, '/'); 66 | var output = html2js(inputAlias, content, moduleName, moduleVar); 67 | 68 | if (outputFile) { 69 | fs.writeFileSync(outputFile, output, 'utf8'); 70 | } else { 71 | console.log(output); 72 | } 73 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "author": { 3 | "name": "Brian Park", 4 | "email": "yaru22@gmail.com" 5 | }, 6 | "name": "ng-html2js", 7 | "description": "Standalone script to turn Angular template into js and put it in a module.", 8 | "version": "3.0.0", 9 | "license": "MIT", 10 | "repository": { 11 | "type": "git", 12 | "url": "git@github.com:yaru22/ng-html2js.git" 13 | }, 14 | "keywords": [ 15 | "angular", 16 | "template", 17 | "html2js" 18 | ], 19 | "main": "./src/html2js.js", 20 | "dependencies": { 21 | "optimist": "*" 22 | }, 23 | "devDependencies": { 24 | "jasmine": "^2.3.2", 25 | "shelljs": "0.5.3" 26 | }, 27 | "bin": { 28 | "ng-html2js": "./bin/ng-html2js" 29 | }, 30 | "scripts": { 31 | "test": "`npm bin`/jasmine" 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /spec/ng-html2js_spec.js: -------------------------------------------------------------------------------- 1 | require('shelljs/global'); 2 | 3 | describe('ng-html2js', function() { 4 | it('converts an html file to a javascript object', function() { 5 | var returnValue = exec("./bin/ng-html2js spec/test.tmpl", {silent: true}); 6 | var fileContents = cat("spec/testOutputs/expectedTestTmplJs"); 7 | expect(returnValue.output).toEqual(fileContents); 8 | }); 9 | 10 | it('can save the output to a file', function() { 11 | exec("./bin/ng-html2js spec/test.tmpl spec/testOutputFile", {silent: true}); 12 | var testFileContents = cat("spec/testOutputs/expectedTestTmplOutputFileJs"); 13 | var outputFileContents = cat("spec/testOutputFile"); 14 | expect(testFileContents).toEqual(outputFileContents); 15 | }); 16 | 17 | describe('choosing a module name', function() { 18 | it('can be performed by providing the flag "-m "', function() { 19 | var returnValue = exec("./bin/ng-html2js spec/test.tmpl -m foo", {silent: true}); 20 | var fileContents = cat("spec/testOutputs/expectedTestTmplJsWithModule"); 21 | expect(returnValue.output).toEqual(fileContents); 22 | }); 23 | 24 | it('can be performed by providing the flag "--module "', function() { 25 | var returnValue = exec("./bin/ng-html2js spec/test.tmpl --module foo", {silent: true}); 26 | var fileContents = cat("spec/testOutputs/expectedTestTmplJsWithModule"); 27 | expect(returnValue.output).toEqual(fileContents); 28 | }); 29 | }); 30 | 31 | describe('choosing a module variable', function() { 32 | it('can be performed by providing the flag "--module-var "', function() { 33 | var returnValue = exec("./bin/ng-html2js spec/test.tmpl --module-var ngModule", {silent: true}); 34 | var fileContents = cat("spec/testOutputs/expectedTestTmplJsWithModuleVar"); 35 | expect(returnValue.output).toEqual(fileContents); 36 | }); 37 | }); 38 | 39 | describe('choosing a module variable and a module name', function() { 40 | it('can be performed by providing the -m and --module-var flags', function() { 41 | var returnValue = exec("./bin/ng-html2js spec/test.tmpl -m foo --module-var ngModule", {silent: true}); 42 | var fileContents = cat("spec/testOutputs/expectedTestTmplJsWithModuleAndModuleVar"); 43 | expect(returnValue.output).toEqual(fileContents); 44 | }); 45 | 46 | it('can be performed by providing the --module and --module-var flags', function() { 47 | var returnValue = exec("./bin/ng-html2js spec/test.tmpl --module foo --module-var ngModule", {silent: true}); 48 | var fileContents = cat("spec/testOutputs/expectedTestTmplJsWithModuleAndModuleVar"); 49 | expect(returnValue.output).toEqual(fileContents); 50 | }); 51 | }); 52 | 53 | describe('stripping part of the path from the template name using the -b or --basedir flag', function() { 54 | it('can be performed by providing the flag "-b "', function() { 55 | var returnValue = exec("./bin/ng-html2js `pwd`/spec/test.tmpl -b `pwd`/spec", {silent: true}); 56 | var fileContents = cat("spec/testOutputs/expectedTestTmplJsWithoutBaseDir"); 57 | expect(returnValue.output).toEqual(fileContents); 58 | }); 59 | 60 | it('can be performed by providing the flag "--basedir "', function() { 61 | var returnValue = exec("./bin/ng-html2js `pwd`/spec/test.tmpl --basedir `pwd`/spec", {silent: true}); 62 | var fileContents = cat("spec/testOutputs/expectedTestTmplJsWithoutBaseDir"); 63 | expect(returnValue.output).toEqual(fileContents); 64 | }); 65 | 66 | describe('when a user provides an absolute path to the input file and an absolute subpath to be stripped from the file name', function() { 67 | describe('when the user excludes the trailing slash from the subpath to be stripped', function() { 68 | it('strips the provided subpath completely from the file name', function() { 69 | var returnValue = exec("./bin/ng-html2js `pwd`/spec/test.tmpl -b `pwd`/spec", {silent: true}); 70 | var fileContents = cat("spec/testOutputs/expectedTestTmplJsWithoutBaseDir"); 71 | expect(returnValue.output).toEqual(fileContents); 72 | }); 73 | }); 74 | 75 | describe('when the user includes the trailing slash in the subpath to be stripped', function() { 76 | it('strips the subpath but leaves the trailing slash in the file name', function() { 77 | var returnValue = exec("./bin/ng-html2js `pwd`/spec/test.tmpl -b `pwd`/spec/", {silent: true}); 78 | var fileContents = cat("spec/testOutputs/expectedTestTmplJsWithoutBaseDir"); 79 | expect(returnValue.output).toEqual(fileContents); 80 | }); 81 | }); 82 | }); 83 | 84 | describe('when the user provides a relative path to the input file and an absolute subpath to be stripped from the file name', function() { 85 | it('does not strip anything from the file name', function() { 86 | var returnValue = exec("./bin/ng-html2js spec/test.tmpl -b `pwd`/spec/", {silent: true}); 87 | var fileContents = cat("spec/testOutputs/expectedTestTmplJs"); 88 | expect(returnValue.output).toEqual(fileContents); 89 | }); 90 | }); 91 | 92 | describe('when the user provides an absolute path to the input file and a relative subpath to be stripped from the file name', function() { 93 | it('strips the provided relative subpath from the file name but keeps the trailing slash', function() { 94 | var returnValue = exec("./bin/ng-html2js `pwd`/spec/test.tmpl -b spec/", {silent: true}); 95 | var fileContents = cat("spec/testOutputs/expectedTestTmplJsWithoutBaseDir"); 96 | expect(returnValue.output).toEqual(fileContents); 97 | }); 98 | }); 99 | 100 | describe('when the user provides a relative subpath to the input file and a relative subpath to be stripped from the file name', function() { 101 | it('does not strip the subpath from the file name', function() { 102 | var returnValue = exec("./bin/ng-html2js spec/test.tmpl -b spec/", {silent: true}); 103 | var fileContents = cat("spec/testOutputs/expectedTestTmplJs"); 104 | expect(returnValue.output).toEqual(fileContents); 105 | }); 106 | }); 107 | }); 108 | 109 | describe('stripping an arbitrary subpath from the name of the template file', function() { 110 | it('can be performed by providing the flag "-s "', function() { 111 | var returnValue = exec("./bin/ng-html2js spec/test.tmpl -s spec/", {silent: true}); 112 | var fileContents = cat("spec/testOutputs/expectedTestTmplJsStrippedPrefix"); 113 | expect(returnValue.output).toEqual(fileContents); 114 | }); 115 | 116 | it('can be performed by providing the flag "--strip-prefix "', function() { 117 | var returnValue = exec("./bin/ng-html2js spec/test.tmpl -s spec/", {silent: true}); 118 | var fileContents = cat("spec/testOutputs/expectedTestTmplJsStrippedPrefix"); 119 | expect(returnValue.output).toEqual(fileContents); 120 | }); 121 | 122 | describe('when the arbitrary prefix to strip does not match characters starting from the beginning of the input file name', function() { 123 | it('does not strip the prefix', function() { 124 | var returnValue = exec("./bin/ng-html2js spec/test.tmpl -s pec/", {silent: true}); 125 | var fileContents = cat("spec/testOutputs/expectedTestTmplJs"); 126 | expect(returnValue.output).toEqual(fileContents); 127 | }); 128 | }); 129 | 130 | describe('when the prefix does not match at all', function() { 131 | it('does not strip the prefix', function() { 132 | var returnValue = exec("./bin/ng-html2js spec/test.tmpl -s bananagrams", {silent: true}); 133 | var fileContents = cat("spec/testOutputs/expectedTestTmplJs"); 134 | expect(returnValue.output).toEqual(fileContents); 135 | }); 136 | }); 137 | 138 | describe('when the prefix to strip is a complete match of the input file name', function() { 139 | it('does not strip the prefix', function() { 140 | var returnValue = exec("./bin/ng-html2js spec/test.tmpl -s spec/test.tmpl", {silent: true}); 141 | var fileContents = cat("spec/testOutputs/expectedTestTmplJs"); 142 | expect(returnValue.output).toEqual(fileContents); 143 | }); 144 | }); 145 | }); 146 | }); 147 | -------------------------------------------------------------------------------- /spec/support/jasmine.json: -------------------------------------------------------------------------------- 1 | { 2 | "spec_dir": "spec", 3 | "spec_files": [ 4 | "**/*[sS]pec.js" 5 | ], 6 | "helpers": [ 7 | "helpers/**/*.js" 8 | ] 9 | } 10 | -------------------------------------------------------------------------------- /spec/test.tmpl: -------------------------------------------------------------------------------- 1 |
2 | hello world 3 |
4 | {{ item }} it's value is great 5 |
6 |
7 | -------------------------------------------------------------------------------- /spec/testOutputs/expectedTestTmplJs: -------------------------------------------------------------------------------- 1 | var module = angular.module('spec/test.tmpl', []); 2 | module.run(['$templateCache', function($templateCache) { 3 | $templateCache.put('spec/test.tmpl', 4 | '
\n' + 5 | ' hello world\n' + 6 | '
\n' + 7 | ' {{ item }} it\'s value is great\n' + 8 | '
\n' + 9 | '
\n' + 10 | ''); 11 | }]); 12 | 13 | -------------------------------------------------------------------------------- /spec/testOutputs/expectedTestTmplJsStrippedPrefix: -------------------------------------------------------------------------------- 1 | var module = angular.module('test.tmpl', []); 2 | module.run(['$templateCache', function($templateCache) { 3 | $templateCache.put('test.tmpl', 4 | '
\n' + 5 | ' hello world\n' + 6 | '
\n' + 7 | ' {{ item }} it\'s value is great\n' + 8 | '
\n' + 9 | '
\n' + 10 | ''); 11 | }]); 12 | 13 | -------------------------------------------------------------------------------- /spec/testOutputs/expectedTestTmplJsWithModule: -------------------------------------------------------------------------------- 1 | var module; 2 | try { 3 | module = angular.module('foo'); 4 | } catch (e) { 5 | module = angular.module('foo', []); 6 | } 7 | 8 | module.run(['$templateCache', function ($templateCache) { 9 | $templateCache.put('spec/test.tmpl', 10 | '
\n' + 11 | ' hello world\n' + 12 | '
\n' + 13 | ' {{ item }} it\'s value is great\n' + 14 | '
\n' + 15 | '
\n' + 16 | ''); 17 | }]); 18 | 19 | -------------------------------------------------------------------------------- /spec/testOutputs/expectedTestTmplJsWithModuleAndModuleVar: -------------------------------------------------------------------------------- 1 | var ngModule; 2 | try { 3 | ngModule = angular.module('foo'); 4 | } catch (e) { 5 | ngModule = angular.module('foo', []); 6 | } 7 | 8 | ngModule.run(['$templateCache', function ($templateCache) { 9 | $templateCache.put('spec/test.tmpl', 10 | '
\n' + 11 | ' hello world\n' + 12 | '
\n' + 13 | ' {{ item }} it\'s value is great\n' + 14 | '
\n' + 15 | '
\n' + 16 | ''); 17 | }]); 18 | 19 | -------------------------------------------------------------------------------- /spec/testOutputs/expectedTestTmplJsWithModuleVar: -------------------------------------------------------------------------------- 1 | var ngModule = angular.module('spec/test.tmpl', []); 2 | ngModule.run(['$templateCache', function($templateCache) { 3 | $templateCache.put('spec/test.tmpl', 4 | '
\n' + 5 | ' hello world\n' + 6 | '
\n' + 7 | ' {{ item }} it\'s value is great\n' + 8 | '
\n' + 9 | '
\n' + 10 | ''); 11 | }]); 12 | 13 | -------------------------------------------------------------------------------- /spec/testOutputs/expectedTestTmplJsWithoutBaseDir: -------------------------------------------------------------------------------- 1 | var module = angular.module('/test.tmpl', []); 2 | module.run(['$templateCache', function($templateCache) { 3 | $templateCache.put('/test.tmpl', 4 | '
\n' + 5 | ' hello world\n' + 6 | '
\n' + 7 | ' {{ item }} it\'s value is great\n' + 8 | '
\n' + 9 | '
\n' + 10 | ''); 11 | }]); 12 | 13 | -------------------------------------------------------------------------------- /spec/testOutputs/expectedTestTmplOutputFileJs: -------------------------------------------------------------------------------- 1 | var module = angular.module('spec/test.tmpl', []); 2 | module.run(['$templateCache', function($templateCache) { 3 | $templateCache.put('spec/test.tmpl', 4 | '
\n' + 5 | ' hello world\n' + 6 | '
\n' + 7 | ' {{ item }} it\'s value is great\n' + 8 | '
\n' + 9 | '
\n' + 10 | ''); 11 | }]); 12 | -------------------------------------------------------------------------------- /src/html2js.js: -------------------------------------------------------------------------------- 1 | /** 2 | * DISCLAIMER: Core logic was copied from https://github.com/karma-runner/karma-ng-html2js-preprocessor. 3 | * Since karma-ng-html2js-preprocessor was designed specifically for karma, 4 | * I modified it to create a standalone script. 5 | */ 6 | 7 | /* globals module */ 8 | 'use strict'; 9 | 10 | var fs = require('fs'); 11 | var path = require('path'); 12 | var util = require('util'); 13 | 14 | 15 | 16 | // 17 | // Constants 18 | // 19 | 20 | var TMPL = fs.readFileSync(path.join(__dirname, './template.tmpl'), 'utf-8'); 21 | var SINGLE_MODULE_TMPL = fs.readFileSync(path.join(__dirname, './singleModule.tmpl'), 'utf-8'); 22 | 23 | 24 | // 25 | // Helper 26 | // 27 | 28 | var escapeContent = function(content) { 29 | return content.replace(/\\/g, '\\\\').replace(/'/g, '\\\'').replace(/\r?\n/g, '\\n\' +\n \''); 30 | }; 31 | 32 | 33 | // 34 | // Main script 35 | // 36 | 37 | module.exports = function (fileName, content, moduleName, moduleVar) { 38 | var escapedContent = escapeContent(content); 39 | 40 | var output = null; 41 | moduleVar = moduleVar || 'module'; 42 | if (moduleName) { 43 | output = util.format(SINGLE_MODULE_TMPL, 44 | moduleVar, 45 | moduleVar, moduleName, 46 | moduleVar, moduleName, 47 | moduleVar, 48 | fileName, escapedContent); 49 | } else { 50 | output = util.format(TMPL, moduleVar, fileName, moduleVar, fileName, escapedContent); 51 | } 52 | 53 | return output; 54 | }; 55 | -------------------------------------------------------------------------------- /src/singleModule.tmpl: -------------------------------------------------------------------------------- 1 | var %s; 2 | try { 3 | %s = angular.module('%s'); 4 | } catch (e) { 5 | %s = angular.module('%s', []); 6 | } 7 | 8 | %s.run(['$templateCache', function ($templateCache) { 9 | $templateCache.put('%s', 10 | '%s'); 11 | }]); 12 | -------------------------------------------------------------------------------- /src/template.tmpl: -------------------------------------------------------------------------------- 1 | var %s = angular.module('%s', []); 2 | %s.run(['$templateCache', function($templateCache) { 3 | $templateCache.put('%s', 4 | '%s'); 5 | }]); 6 | --------------------------------------------------------------------------------