Permalink
Browse files

Bump mod-handlebars@0.1.0

  • Loading branch information...
0 parents commit d56d5bc415de34c3314c2e57deb8562b0be63b85 @yuanyan yuanyan committed Jul 14, 2013
Showing with 362 additions and 0 deletions.
  1. +1 −0 .gitattributes
  2. +52 −0 .gitignore
  3. +22 −0 MIT-LICENSE.txt
  4. +21 −0 README.md
  5. +196 −0 index.js
  6. +36 −0 package.json
  7. +14 −0 test/Modfile
  8. +1 −0 test/test.hbs
  9. +19 −0 test/test.js
@@ -0,0 +1 @@
+* text=auto
@@ -0,0 +1,52 @@
+# Include your project-specific ignores in this file
+# Read about how to use .gitignore: https://help.github.com/articles/ignoring-files
+
+# Numerous always-ignore extensions
+*.diff
+*.err
+*.orig
+*.log
+*.rej
+*.swo
+*.swp
+*.vi
+*~
+*.sass-cache
+
+# OS or Editor folders
+.DS_Store
+._*
+Thumbs.db
+.cache
+.project
+.settings
+.tmproj
+nbproject
+*.sublime-project
+*.sublime-workspace
+.idea
+
+# Dreamweaver added files
+_notes
+dwsync.xml
+
+# Komodo
+*.komodoproject
+.komodotools
+
+# Espresso
+*.esproj
+*.espressostorage
+
+# Rubinius
+*.rbc
+
+# Folders to ignore
+.hg
+.svn
+.CVS
+
+# Node
+npm-debug.log
+node_modules
+
@@ -0,0 +1,22 @@
+Copyright (c) 2013 yuanyan
+
+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.
@@ -0,0 +1,21 @@
+mod-handlebars
+===
+
+Compile Handlebars templates to JavaScript templates file
+
+
+## Usage
+
+```js
+module.exports = {
+ plugins: {
+ "handlebars": "mod-handlebars"
+ },
+ tasks: {
+ "handlebars": {
+ src: "test.hbs",
+ dest: "test.js"
+ }
+ }
+};
+```
196 index.js
@@ -0,0 +1,196 @@
+var fs = require('fs');
+var basename = require('path').basename;
+
+exports.summary = 'Compile Handlebars templates to JavaScript templates file';
+
+exports.usage = '<src> [options]';
+
+exports.options = {
+ "dest" : {
+ alias : 'd'
+ ,default : '<src>'
+ ,describe : 'destination file or directory'
+ },
+
+ 'amd': {
+ 'type': 'boolean',
+ 'description': 'Exports amd style (require.js)'
+ },
+
+ 'commonjs': {
+ 'type': 'string',
+ 'description': 'Exports CommonJS style, path to Handlebars module',
+ 'default': null
+ },
+
+ 'handlebarPath': {
+ 'type': 'string',
+ 'description': 'Path to handlebar.js (only valid for amd-style)',
+ 'default': ''
+ },
+
+ 'known': {
+ 'type': 'string',
+ 'description': 'Known helpers'
+ },
+
+ 'knownOnly': {
+ 'type': 'boolean',
+ 'description': 'Known helpers only'
+ },
+
+ 'namespace': {
+ 'type': 'string',
+ 'description': 'Template namespace',
+ 'default': 'Handlebars.templates'
+ },
+
+ 'simple': {
+ 'type': 'boolean',
+ 'description': 'Output template function only.'
+ },
+
+ 'root': {
+ 'type': 'string',
+ 'description': 'Template root. Base value that will be stripped from template names.'
+ },
+
+ 'partial' : {
+ 'type': 'boolean',
+ 'description': 'Compiling a partial template'
+ },
+
+ 'extension': {
+ 'type': 'string',
+ 'description': 'Template extension.',
+ 'default': 'handlebars|hbs'
+ },
+
+ "charset" : {
+ alias : 'c'
+ ,default : 'utf-8'
+ ,describe : 'file encoding type'
+ },
+
+ "postParse" : {
+ type: "function"
+ , describe: "post parse the handlebars template into it's AST"
+ }
+};
+
+exports.run = function (options) {
+ var src = options.src;
+ var dest = options.dest;
+
+ options.extension = options.extension.replace(/[\\^$*+?.():=!{}\-\[\]]/g, function(arg) {
+ return '\\' + arg;
+ }).split('|').map(function(ext){
+ return '\\.' + ext;
+ }).join('|');
+ options.extension = new RegExp( options.extension + '$');
+
+ exports.files.forEach(function(inputFile){
+ var outputFile = dest;
+
+ if(exports.file.isDirFormat(dest)){
+ outputFile = path.join(dest , path.basename(inputFile) );
+ outputFile = outputFile.replace(options.extension, '.js');
+ }
+
+ exports.compileHandlebars(inputFile, outputFile, options);
+ });
+};
+
+exports.compileHandlebars = function(inputFile, outputFile, options){
+ var Handlebars = require('handlebars');
+ // Convert the known list into a hash
+ var known = {};
+ if (options.known && !Array.isArray(options.known)) {
+ options.known = [options.known];
+ }
+ if (options.known) {
+ for (var i = 0, len = options.known.length; i < len; i++) {
+ known[options.known[i]] = true;
+ }
+ }
+
+
+ // template name
+ var templateName = inputFile;
+ // Clean the template name
+ if (!options.root) {
+ templateName = basename(templateName);
+ } else if (templateName.indexOf(root) === 0) {
+ templateName = templateName.substring(root.length+1);
+ }
+ templateName = templateName.replace(options.extension, '');
+
+ // output stack
+ var output = [];
+ if (!options.simple) {
+ if (options.amd) {
+ output.push('define([\'' + options.handlebarPath + 'handlebars\'], function(Handlebars) {\n');
+ } else if (options.commonjs) {
+ output.push('var Handlebars = require("' + options.commonjs + '");');
+ } else {
+ output.push('(function() {\n');
+ }
+ output.push(' var template = Handlebars.template, templates = ');
+ output.push(options.namespace);
+ output.push(' = ');
+ output.push(options.namespace);
+ output.push(' || {};\n');
+ }
+
+ // compile
+ var code = exports.file.read(inputFile);
+ var compilerOptions = {
+ knownHelpers: known,
+ knownHelpersOnly: options.knownOnly
+ };
+ // parse the handlebars template into it's AST
+ var ast = Handlebars.parse(code);
+ if(options.postParse) ast = options.postParse(ast);
+ var compiled = Handlebars.precompile(ast, compilerOptions);
+
+
+ if (options.simple) {
+ output.push(compiled + '\n');
+ } else if (options.partial) {
+ if(options.amd && (options._.length == 1 && !fs.statSync(options._[0]).isDirectory())) {
+ output.push('return ');
+ }
+ output.push('Handlebars.partials[\'' + templateName + '\'] = template(' + compiled + ');\n');
+ } else {
+ if(options.amd && (options._.length == 1 && !fs.statSync(options._[0]).isDirectory())) {
+ output.push('return ');
+ }
+ output.push('templates[\'' + templateName + '\'] = template(' + compiled + ');\n');
+ }
+
+
+ // Output the content
+ if (!options.simple) {
+ if (options.amd) {
+ if(options._.length > 1 || (options._.length == 1 && fs.statSync(options._[0]).isDirectory())) {
+ if(options.partial){
+ output.push('return Handlebars.partials;\n');
+ } else {
+ output.push('return templates;\n');
+ }
+ }
+ output.push('});');
+ } else if (!options.commonjs) {
+ output.push('})();');
+ }
+ }
+ output = output.join('');
+
+ if (outputFile) {
+ exports.file.write(outputFile, output);
+ exports.log(inputFile, '>', outputFile);
+ }
+
+ return output;
+
+}
@@ -0,0 +1,36 @@
+{
+ "name": "mod-handlebars",
+ "description": "Compile Handlebars templates to JavaScript templates file",
+ "version": "0.1.0",
+ "homepage": "https://github.com/modjs/handlebars",
+ "author": {
+ "name": "yuanyan",
+ "url": "https://github.com/yuanyan"
+ },
+ "repository": {
+ "type": "git",
+ "url": "git://github.com/modjs/handlebars.git"
+ },
+ "bugs": {
+ "url": "https://github.com/modjs/handlebars/issues"
+ },
+ "licenses": [
+ {
+ "type": "MIT",
+ "url": "https://github.com/modjs/handlebars/blob/master/MIT-LICENSE.txt"
+ }
+ ],
+ "bin": "",
+ "engines": {
+ "node": ">= 0.6.0"
+ },
+ "scripts": {
+ "test": ""
+ },
+ "dependencies": {
+ "handlebars": "~1.0.12"
+ },
+ "devDependencies": {
+ },
+ "keywords": []
+}
@@ -0,0 +1,14 @@
+// A sample Modfile
+// More info at https://github.com/modulejs/modjs/
+
+module.exports = {
+ plugins: {
+ "handlebars": "../index.js"
+ },
+ tasks: {
+ "handlebars": {
+ src: "test.hbs",
+ dest: "test.js"
+ }
+ }
+};
@@ -0,0 +1 @@
+<p>Hello, my name is {{name}}. I live in {{> partial}}</p>
@@ -0,0 +1,19 @@
+(function() {
+ var template = Handlebars.template, templates = Handlebars.templates = Handlebars.templates || {};
+templates['test'] = template(function (Handlebars,depth0,helpers,partials,data) {
+ this.compilerInfo = [4,'>= 1.0.0'];
+helpers = this.merge(helpers, Handlebars.helpers); partials = this.merge(partials, Handlebars.partials); data = data || {};
+ var buffer = "", stack1, functionType="function", escapeExpression=this.escapeExpression, self=this;
+
+
+ buffer += "<p>Hello, my name is ";
+ if (stack1 = helpers.name) { stack1 = stack1.call(depth0, {hash:{},data:data}); }
+ else { stack1 = depth0.name; stack1 = typeof stack1 === functionType ? stack1.apply(depth0) : stack1; }
+ buffer += escapeExpression(stack1)
+ + ". I live in ";
+ stack1 = self.invokePartial(partials.partial, 'partial', depth0, helpers, partials, data);
+ if(stack1 || stack1 === 0) { buffer += stack1; }
+ buffer += "</p>";
+ return buffer;
+ });
+})();

0 comments on commit d56d5bc

Please sign in to comment.